I am having trouble installing psycopg2. I get the following error when I try to pip install psycopg2
:
Error: pg_config executable not found.
P
apt-get build-dep python-psycopg2
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.
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.
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"
.
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, I solved it using the homebrew package manager
brew install postgresql