How to install psycopg2 with pg_config error?
PythonPostgresqlPsycopg2Python Problem Overview
I've tried to install psycopg2 (PostgreSQL Database adapater) from this site, but when I try to install after I cd into the package and write
python setup.py install
I get the following error:
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'.
I've also tried 'sudo pip install psycopg2' and I got the same message.
After reading through the docs, it asks to look at the setup.cfg file (which is below):
[build_ext]
define=
# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information
# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=
# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1
# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=
# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0
# Statically link against the postgresql client library.
#static_libpq=1
# Add here eventual extra libraries required to link the module.
#libraries=
However, I'm not sure if I'm suppose to edit this file, since the documentation states the following:
then take a look at the setup.cfg file.
Some of the options available in setup.cfg are also available as command line arguments of the build_ext sub-command. For instance you can specify an alternate pg_config version using:
$ python setup.py build_ext --pg-config /path/to/pg_config build
Use python setup.py build_ext --help to get a list of the options supported.
I've gotten the list of options supported but I'm not sure where to go from there
Python Solutions
Solution 1 - Python
Debian/Ubuntu
Python 2
sudo apt install libpq-dev python-dev
Python 3
sudo apt install libpq-dev python3-dev
Additional
If none of the above solve your issue, try
sudo apt install build-essential
or
sudo apt install postgresql-server-dev-all
With pip
Install the psycopg2-binary
PyPI package instead, it has Python wheels for Linux and Mac OS.
pip install psycopg2-binary
Solution 2 - Python
I was getting this issue because I hadn't yet installed PostgreSQL on my machine yet.
For Mac just a simple brew install postgresql
fixed the problem.
Solution 3 - Python
If you need to install without compiling:
pip install psycopg2-binary
https://www.psycopg.org/docs/install.html#binary-install-from-pypi > Note: The psycopg2-binary package is meant for beginners to start > playing with Python and PostgreSQL without the need to meet the build > requirements. If you are the maintainer of a publish package depending > on psycopg2 you shouldn’t use ‘psycopg2-binary’ as a module > dependency. For production use you are advised to use the source distribution.
Solution 4 - Python
If you are on Ubuntu or any other debian-based distro, try
sudo apt-get install python3-psycopg2
Otherwise, you need to find and install the Postgresql client packages for your distribution. psycopg2 installation from source
Solution 5 - Python
Upgrading pip worked for me: pip install --upgrade pip
Solution 6 - Python
On Macbook with M1 i had to install postgresql
brew install postgresql
(If you don't have brew: https://brew.sh)
then run the install again
python3 -m pip install psycopg2-binary
Solution 7 - Python
For OSX with Macports, you can install sudo port install py38-psycopg2
for Python 3.8. You can search for your version of Python with port search psycopg2
. At the time of writing, the versions range from 2.7 to 3.9 all up to date with version 2.8.6 of psycopg2.
Note: This probably will not help in a venv.
Edit: So to find your pg_config for a venv, run the command port contents postgresql13 | grep pg_config
where postgresql13
is the version of the package of postgresql installed from the above port command. You are looking for a path such as /opt/local/lib/postgresql13/bin/pg_config
. Export that to your PATH variable with a command such as export PATH=/opt/local/lib/postgresql13/bin/:$PATH
.
Refer to this answer for Homebrew.
Solution 8 - Python
On Fedora (tested and built from source on Fedora 35)
pg_config
is present in libpq5-devel
.
Now try these steps:
sudo dnf install libpq5-devel
python setup.py build
sudo python setup.py install
Solution 9 - Python
For mac os
install postgresql with : brew install postgresql
then
install psycopg2 with : pip install psycopg2
worked for me
Solution 10 - Python
For people building postgres
and psycopg2
from source like me, another solution is here:
sudo su
export PATH=/usr/local/pgsql/bin:$PATH #or path to your pg_config
Now setup.py from psycopg2 could find pg_config correctly.
python3 setup.py install
or if you just want to use pip3
, pip3 install psycopg2
should work too.