1005

I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2:

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH

or specify the full executable path with the option:



    python setup.py build_ext --pg-config /path/to/pg_config build ...



or with the pg_config option in 'setup.cfg'.

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build/psycopg2

But the problem is pg_config is actually in my PATH; it runs without any problem:

$ which pg_config
/usr/pgsql-9.1/bin/pg_config

I tried adding the pg_config path to the setup.cfg file and building it using the source files I downloaded from their website (http://initd.org/psycopg/) and I get the following error message!

Error: Unable to find 'pg_config' file in '/usr/pgsql-9.1/bin/'

But it is actually THERE!!!

I am baffled by these errors. Can anyone help please?

By the way, I sudo all the commands. Also I am on RHEL 5.5.

William Jackson
  • 1,030
  • 8
  • 22
user1448207
  • 10,185
  • 3
  • 11
  • 5

49 Answers49

987

pg_config is in postgresql-devel (libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)

deepwell
  • 17,897
  • 10
  • 31
  • 37
TilmanBaumann
  • 10,458
  • 2
  • 12
  • 10
511

On Mac OS X, I solved it using the homebrew package manager

brew install postgresql
sacuL
  • 42,057
  • 8
  • 58
  • 83
azalea
  • 8,068
  • 3
  • 30
  • 40
  • 36
    Excellent! This solved the problem for me, but newbies should be aware that you need to install the [Homebrew](http://brew.sh/) package manager first in order for the `brew` command to work. – seane Dec 02 '14 at 20:49
  • Worked for me also, even though I did the brew install after the pgadmin install. – Khanal Jan 27 '16 at 21:34
  • 12
    Doesn't that install the entire database? – zmbq Feb 29 '16 at 14:56
  • @zmbq Yes, it will. Instead of installing postgresql and setting up paths, symlinks, etc manually, I would recommend using a package manager such as homebrew. – azalea Feb 29 '16 at 15:38
  • 9
    I need the extra `brew link postgresql` – vincentlcy Mar 10 '16 at 08:56
  • 1
    To add to @seane's comment, use **[this walkthrough](https://coolestguidesontheplanet.com/installing-homebrew-on-os-x-el-capitan-10-11-package-manager-for-unix-apps/)** to install `homebrew`. – Srini Jul 20 '16 at 21:34
  • This was what worked for me, thank you a lot. It is important to note that there is indeed an important connection between psycopg2 and postgresql that I was not aware of before this. – William Karlsson Jul 29 '19 at 20:40
  • Worked for me! (Mac OS) -- needed to add the following in my `.zshrc` --> `export PATH=$HOME/bin:/usr/local/bin:$PATH` – Yaakov Bressler Aug 04 '20 at 17:20
293

Have you installed python-dev? If you already have, try also installing libpq-dev

sudo apt-get install libpq-dev python-dev

From the article: How to install psycopg2 under virtualenv

phoenix
  • 3,988
  • 1
  • 29
  • 33
thegauraw
  • 4,462
  • 2
  • 18
  • 14
97

Also on OSX. Installed Postgress.app from http://postgresapp.com/ but had the same issue.

I found pg_config in that app's contents and added the dir to $PATH.

It was at /Applications/Postgres.app/Contents/Versions/latest/bin. So this worked: export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH".

Boris
  • 7,044
  • 6
  • 62
  • 63
Tayler
  • 2,387
  • 16
  • 20
54

You can install pre-compiled binaries on any platform with pip or conda:

python -m pip install psycopg2-binary

or

conda install psycopg2

Please be advised that the psycopg2-binary pypi page recommends building from source in production:

The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources

To use the package built from sources, use python -m pip install psycopg2. That process will require several dependencies (documentation) (emphasis mine):

  • A C compiler.
  • The Python header files. They are usually installed in a package such as python-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.
  • The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them.
  • The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config --version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH:
$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH

You only need pg_config to compile psycopg2, not for its regular usage.

Once everything is in place it’s just a matter of running the standard:

$ pip install psycopg2

or, from the directory containing the source code:

$ python setup.py build
$ python setup.py install
jakub
  • 11,042
  • 1
  • 21
  • 33
  • On Ubuntu, I needed to install the libpq-dev library first and then just run the pip install command again: `apt install libpq-dev` – Benjamin Apr 03 '20 at 21:28
  • That solved it on a Mac OS X without needing to install postgresql altogether – Oren Yosifon Jul 18 '20 at 18:21
  • Thank you for pointing out the documentation's recommendation to build from source. I second this suggestion--devs should use the later half of this answer and avoid the pre-packaged binary, even for most development purposes. – Jason R Stevens CFA Dec 25 '20 at 03:52
51

On alpine, the library containing pg_config is postgresql-dev. To install, run:

apk add postgresql-dev
rkoval
  • 800
  • 7
  • 12
44

This is what worked for me on CentOS, first install:

sudo yum install postgresql postgresql-devel python-devel

On Ubuntu just use the equivilent apt-get packages.

sudo apt-get install postgresql postgresql-dev python-dev

And now include the path to your postgresql binary dir with you pip install, this should work for either Debain or RHEL based Linux:

sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2

Make sure to include the correct path. Thats all :)

radtek
  • 26,590
  • 9
  • 126
  • 97
  • 1
    On Ubuntu it's `sudo apt-get install postgresql postgresql-dev python-dev`, and this worked for me. – modulitos Dec 15 '14 at 23:12
  • 2
    also works on Amazon Linux AMI as of Dec 2016. Also make sure `gcc` is installed and python3-devel. – villasv Dec 08 '16 at 15:23
37
apt-get build-dep python-psycopg2
matiu
  • 6,454
  • 3
  • 40
  • 46
36

You should add python requirements used in Postgres on Ubuntu. Run:

sudo apt-get install libpq-dev python-dev
Kishore Bhosale
  • 521
  • 4
  • 6
34

Just to sum up, I also faced exactly same problem. After reading a lot of stackoverflow posts and online blogs, the final solution which worked for me is this:

1) PostgreSQL(development or any stable version) should be installed before installing psycopg2.

2) The pg_config file (this file normally resides in the bin folder of the PostgreSQL installation folder) PATH had to be explicitly setup before installing psycopg2. In my case, the installation PATH for PostgreSQL is:

/opt/local/lib/postgresql91/

so in order to explicitly set the PATH of pg_config file, I entered following command in my terminal:

PATH=$PATH:/opt/local/lib/postgresql91/bin/

This command ensures that when you try to pip install psycopg2, it would find the PATH to pg_config automatically this time.

I have also posted a full error with trace and its solution on my blog which you may want to refer. Its for Mac OS X but the pg_config PATH problem is generic and applicable to Linux also.

Ali Raza Bhayani
  • 2,445
  • 21
  • 20
  • 7
    for me on OS X the path looked like this `PATH="/Applications/Postgres.app/Contents/Versions/9.3/bin:$PATH"` – andilabs Dec 09 '14 at 14:26
  • @andi Yea, the installation path of Postgres may differ but the installation recipe for psycopg2 which I mentioned above remains the same. Hope it helped. – Ali Raza Bhayani Dec 10 '14 at 10:55
31

sudo apt-get install libpq-dev works for me on Ubuntu 15.4

novasaint
  • 692
  • 8
  • 13
  • If you use newer Ubuntu, still use aforementioned installation instruction. See [pg_config](http://manpages.ubuntu.com/manpages/xenial/man1/pg_config.1.html) – palik May 12 '17 at 07:48
23

On Linux Mint sudo apt-get install libpq-dev worked for me.

mthecreator
  • 734
  • 1
  • 7
  • 17
21

For those running OS X, this solution worked for me:

1) Install Postgres.app:

http://www.postgresql.org/download/macosx/

2) Then open the Terminal and run this command, replacing where it says {{version}} with the Postgres version number:

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{{version}}/bin

e.g.

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin

Milo
  • 1,007
  • 11
  • 16
  • Or symlink it to the latest version `sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config` – Subin Sebastian Aug 04 '19 at 06:56
20

UPDATE /etc/yum.repos.d/CentOS-Base.repo, [base] and [updates] sections
ADD exclude=postgresql*

curl -O http://yum.postgresql.org/9.1/redhat/rhel-6-i386/pgdg-centos91-9.1-4.noarch.rpmr  
rpm -ivh pgdg-centos91-9.1-4.noarch.rpm

yum install postgresql  
yum install postgresql-devel

PATH=$PATH:/usr/pgsql-9.1/bin/

pip install psycopg2
thefourtheye
  • 206,604
  • 43
  • 412
  • 459
Mayank
  • 4,741
  • 1
  • 14
  • 15
20

You have to install libpq-dev/postgresql-libs, which is the header files and static library for compiling C programs to link with the libpq library in order to communicate with a PostgreSQL database backend.

On Arch this will run:

$ sudo pacman -S postgresql-libs

On Debian and Ubuntu:

$ sudo apt-get install libpq-dev

On Mac OS X:

$ brew install postgresql

On Red Hat/CentOS/Fedora:

$ sudo yum install postgresql-devel
Mahrez BenHamad
  • 977
  • 8
  • 13
18

Try to add it to PATH:

PATH=$PATH:/usr/pgsql-9.1/bin/ ./pip install psycopg2
Marboni
  • 2,234
  • 3
  • 21
  • 35
  • `-bash: cd: /usr/pgsql-x.x/bin/: No such file or directory` – pylover May 04 '13 at 08:32
  • @pylover, you need to use correct path to you Postgres bin directory. Are you sure that `/usr/pgsql-x.x/bin/` is correct? – Marboni May 05 '13 at 16:42
  • It would also be sufficient just to add the folder where all important `pg_` files live. For postgres.app this path on Mac OS X is `/Applications/Postgres.app/Contents/Versions/9.3/bin`. `PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin` Please update your *version** if a new version got released! – Tim Daubenschütz Mar 17 '15 at 12:58
  • This worked for me on OS X El Capitan: `PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/` – David Hariri Feb 13 '16 at 14:51
17

Ali's solution worked for me but I was having trouble finding the bin folder location. A quick way to find the path on Mac OS X is to open psql (there's a quick link in the top menu bar). This will open a separate terminal window and on the second line the path of your Postgres installation will appear like so:

My-MacBook-Pro:~ Me$ /Applications/Postgres93.app/Contents/MacOS/bin/psql ; exit;

Your pg_config file is in that bin folder. Therefore, before installing psycopg2 set the path of the pg_config file:

PATH=$PATH:/Applications/Postgres93.app/Contents/MacOS/bin/

or for newer version:

PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin

Then install psycopg2.

olicarbo
  • 150
  • 5
user2498172
  • 261
  • 3
  • 3
13

I'm going to leave this here for the next unfortunate soul who can't get around this problem despite all the provided solutions. Simply use sudo pip3 install psycopg2-binary

DUDANF
  • 1,890
  • 6
  • 25
  • This didn't work for me. Only `brew install postgresql` fixed it. – tsando Nov 11 '19 at 16:53
  • You might have to `pip install psycopg2-binary` for me: pip -> python2, pip3 -> python3. But so long as you got it working, it was one of many solutions! – DUDANF Nov 11 '19 at 16:54
13

Simply run the following:

sudo apt install libpq-dev

Fixed the issue for me

Aric Kuter
  • 190
  • 1
  • 9
12

You need to upgrade your pip before installing psycopg2. Use this command

pip install --upgrade pip
Aswin
  • 443
  • 5
  • 14
10

On Mac OS X and If you are using Postgres App (http://postgresapp.com/):

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

No need to specify version of Postgres in this command. It will be always pointed to latest.

and do

pip install psycopg2

P.S: If Changes doesn't reflect you may need to restart the Terminal/Command prompt

Source

Dinesh Sunny
  • 3,923
  • 3
  • 26
  • 25
10

Installing python-psycopg2 solved it for me on Arch Linux:

pacman -S python-psycopg2
Ronan Boiteau
  • 8,035
  • 6
  • 32
  • 47
9

On Windows, You may want to install the Windows port of Psycopg, which is recommended in psycopg's documentation.

azalea
  • 8,068
  • 3
  • 30
  • 40
9

Just solved the problem in Cent OS 7 by:

export PATH=$PATH:/usr/pgsql-9.5/bin

make sure your PostgreSql version matches the right version above.

Morfat Mosoti
  • 162
  • 2
  • 8
9

On MacOS, the simplest solution will be to symlink the correct binary, that is under the Postgres package.

sudo ln -s /Applications/Postgres.app/Contents/Versions/latest/bin/pg_config /usr/local/bin/pg_config

This is fairly harmless, and all the applications will be able to use it system wide, if required.

Subin Sebastian
  • 15,526
  • 6
  • 51
  • 56
6

Here, for OS X completeness: if you install PostgreSQL from MacPorts, pg_config will be in /opt/local/lib/postgresql94/bin/pg_config.

When you installed MacPorts, it already added /opt/local/bin to your PATH.

So, this will fix the problem: $ sudo ln -s /opt/local/lib/postgresql94/bin/pg_config /opt/local/bin/pg_config

Now pip install psycopg2 will be able to run pg_config without issues.

Gabi
  • 1,153
  • 15
  • 19
6

This was partly suggested before, adding it here for clarity.

From the documentation at https://www.psycopg.org/docs/install.html.
they suggest running: $ pip install psycopg2-binary

That solved the issue for me.

Kobi Barac
  • 81
  • 2
  • 4
5

sudo yum install postgresql-devel (centos6X)

pip install psycopg2==2.5.2

user2387567
  • 135
  • 2
  • 3
5

To those on macOS Catalina using the zsh shell who have also installed the postgres app:

Open your ~/.zshrc file, and add the following line:

export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"

Then close all your terminals, reopen them, and you'll have resolved your problem.

If you don't want to close your terminals, simply enter source ~/.zshrc in whatever terminal you'd like to keep working on.

qarthandso
  • 1,760
  • 1
  • 20
  • 35
5

For mac users, extend your path variable to include PostgreSQL like this export PATH=$PATH:/Library/PostgreSQL/12/bin.

Uzzi Emuchay
  • 169
  • 2
  • 7
5

run the following command to resolve pg_config executable not found:-

sudo apt-get install --reinstall libpq-dev
Tejsingh
  • 139
  • 1
  • 3
4

I recommend that you try to use Postgres.app. (http://postgresapp.com) This way you can easily turn Postgres on and off on your Mac. Once you do, add the path to Postgres to your .profile file by appending the following:

PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"

Only after you added Postgres to your path you can try to install psycopg2 either within a virtual environment (using pip) or into your global site-packages.

Umar Asghar
  • 2,332
  • 1
  • 21
  • 21
4

If you're trying to add a psycopg2 dependency in your virtual environment that will allow your python project to connect to your docker or kubernetes pod that's running postgresql separately, on your Mac and are not really interested in installing postgresql on your Mac just so that you can get pg_config, here's what you do instead:

Install psycopg2_binary as suggested in the error output from the pipenv install and everything will work out just fine.

Dhiraj Gupta
  • 7,159
  • 4
  • 43
  • 50
3

I am pretty sure you've experienced the same "problem" i did, therefore I'll offer you the extremely easy solution...

In your case, the actual path that you need to add to $PATH (or as a command param) is:

/usr/pgsql-9.1/bin/pg_config

not

/usr/pgsql-9.1/bin

E.g. if you run the python setup.py script afterwards, you would run it like this:

python setup.py build_ext --pg-config /usr/pgsql-9.1/bin/pg_config build

Probably too late, but still the easiest solution.

LATER EDIT:

Under further test I found out that if you initially add the path to pg_config in the form

/usr/pgsql-9.1/bin

(without /pg_config after ...../bin) and run the pip install command it will work.

However, if you then decide to follow the indication to run python setup.py, you will have to specify the path with /pg_config after ...../bin, i.e.

python setup.py build_ext --pg-config /usr/pgsql-9.1/bin/pg_config build
BogdanSorlea
  • 442
  • 2
  • 5
  • 15
  • An alternative to adding the pgsql/bin directory to the PATH that I've used a fair bit is to symlink `/usr/pgsql-9.6/bin/pg_config` (or whatever it's called on your system) to `/usr/bin/pg_config` - that way it's available in the PATH you've already got. – Ralph Bolton Jan 22 '20 at 09:50
3

This is how I managed to install psycopg2

$ wget http://initd.org/psycopg/tarballs/PSYCOPG-2-5/psycopg2-2.5.3.tar.gz
$ tar -xzf psycopg2-2.5.3.tar.gz
$ cd psycopg2-2.5.3
$ pip install .
Matt
  • 24,656
  • 6
  • 74
  • 72
TomNg
  • 1,699
  • 2
  • 16
  • 25
3

There is a lack of answers for windows users. Here is how I solved this issue.

First add the PostgreSQL bin folder into the path variable like this

set PATH=%PATH%;C:\Program Files\PostgreSQL\11\bin

Substitute \11\ with your version of Postgres

next run

pip install pyscopg2

this at least got me to a compiling error.

Update: it looks to be an issue with the psycopg2 library and it’s dependencies not playing nice with python 3.8. https://github.com/psycopg/psycopg2/issues/990

Daniel Butler
  • 1,817
  • 1
  • 14
  • 24
2

for CentOS/RedHat make sure that /etc/alternatives/pgsql-pg_config is a non-broken symlink

Hedde van der Heide
  • 19,278
  • 13
  • 58
  • 93
1

On Gentoo You have to execute the following

# Install postgresql client libraries
sudo emerge postgresql-base
czerasz
  • 12,044
  • 8
  • 48
  • 62
  • For me, the issue was that the links that `eselect postgresql set [...]` creates were only partially created / broken. I `rm`ed the ones that looked related (see `ls /usr/include/*p*g*` and `*p*q*`) and then recreated them with `eselect postgresql set 9.5`. – Pistos Oct 12 '17 at 17:54
1

For anyone experiencing this issue working on a Django application and deploying to AWS, the method described in this tutorial worked for me: https://realpython.com/deploying-a-django-app-and-postgresql-to-aws-elastic-beanstalk/

Here is the author's explanation for when you get the following error:

Error: pg_config executable not found.

The problem is that we tried to install psycopy2 (the Postgres Python bindings), but we need the Postgres client drivers to be installed as well. Since they are not installed by default, we need to install them first.

eb will read custom .config files from a folder called “.ebextensions” at the root level of your project. These .config files allow you to install packages, run arbitrary commands and/or set environment variables. Files in the “.ebextensions” directory should conform to either JSON or YAML syntax and are executed in alphabetical order.

The first thing to do is install some packages so that our pip install command will complete successfully. To do this, let’s create a file called .ebextensions/01_packages.config:

packages:
  yum:
    git: []
    postgresql93-devel: []
    libjpeg-turbo-devel: []

EC2 instances run Amazon Linux, which is a Redhat flavor, so we can use yum to install the packages that we need. For now, we are just going to install three packages - git, the Postgres client, and libjpeg for Pillow.

kashgo
  • 71
  • 1
  • 5
1

a simple solution can be

pip install psycopg2==2.7.5
Josh Anish
  • 51
  • 5
1

For MAC users -

After adding the postgresql package through HomeBrew, you might have to add the openssl path variable -

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

Then you can again pip install the psycopg2 package which should work.

Kartik Shandilya
  • 3,179
  • 5
  • 18
  • 36
0

I found that this page provided the best instructions for installing PostgreSQL on my mac and that the pip install command worked perfectly afterwards:

https://www.codefellows.org/blog/three-battle-tested-ways-to-install-postgresql

traggatmot
  • 1,201
  • 3
  • 22
  • 42
0

I got fixed this by running the following command.

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2

Reference : https://stackoverflow.com/a/39800677/4270698

Kumar KS
  • 767
  • 7
  • 17
0

If you are using mac then make sure you have installed Postgresql in your machine.

0

You can use the binary instead, I ran into the issue in dockerizing the application when Django tried to install it as part of its dependencies, to resolve that, I used this setup for requirements.txt where the binary is installed first and then django does not try to install it again: psycopg2-binary django gunicorn

Avi Kapuya
  • 1,020
  • 1
  • 11
  • 17
0

if you have recently updated python or changed default python (let's say from 3.6 to 3.8). The following code

sudo apt-get install python-dev   OR    sudo apt-get install python3-dev

will be installing/working for the previous python version.

so if you want this command to work for the recently updated/changed python version try mentioning that specific version like python3.8 in command like

sudo apt-get install python3.8-dev  

try above with following

pip install wheel
export PATH=/path/to/compiled/postgresql/bin:"$PATH"
sudo apt-get install libpq-dev
sudo apt-get install python3.x-dev    **Change x with your version, eg python3.8**
pip install psycopg2-binary
pip install psycopg2
0

The following steps solved the issue for me (ubuntu 18.04)

  1. install postgres (refer this if not installed)
  2. install libpq-dev

sudo apt-get install libpq-dev

-2

On MAC Mojave, if you have installed it using brew, then do brew info <your postgres>

It will show you all environment variables that you should set, simply copy-paste those commands, restart your shell and thats it!

Ravindra
  • 89
  • 1
  • 8
-3

A quick understanding of how pip works pip -> python2 and pip3 -> python3, so if you're looking to fix this on python 3 you can simply sudo pip3 install psycopg2 this should work, probably.

Precious Tom
  • 395
  • 2
  • 15