pg_config executable not found

Viewed 1038354

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.

54 Answers

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

For ubuntu users, this is the solution:

sudo apt install libpq-dev

It worked for me.

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

I had this issue because I didn't had a postgres install. If you have brew install run

brew install postgresql

This should fix the issue.

Simply run the following:

sudo apt install libpq-dev

Fixed the issue for me

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

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.

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

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.

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.

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

sudo apt-get install --reinstall libpq-dev

a simple solution can be

pip install psycopg2==2.7.5

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

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.

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

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.

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

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

I have really big issue with installation of this library on Oracle Linux 7.9. And whatever I was doing I wasn't able to install psycopg2 via pip. So I decide to install it via yum with this command:

yum install http://mirror.centos.org/centos/7/os/x86_64/Packages/python-psycopg2-2.5.1-4.el7.x86_64.rpm

With this command the psycopg2 was installed and I was able create postgres user, DB via ansible. (I know this is like sixtieth answer but I hope this answer can still help somebody).

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.

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.

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

For those who don't have Postgresql locally installed and wish to run it as a Docker container. You should use psycopg2-binary.

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

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

Using postgresql-devel for redhat/ fedora series resolved the issue.

yum install postgresql-devel

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!

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.

Related