How to install psycopg2 with pg_config error?
Asked Answered
P

15

91

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

Playpen answered 30/1, 2016 at 16:25 Comment(1)
Does this answer your question? pg_config executable not foundElenoraelenore
M
19

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

Moramorabito answered 30/1, 2016 at 17:16 Comment(3)
With this is, how could you standardize the installation with requirements.txt?Hartshorn
I am on Ubuntu Buster, installed this python-psycopg2 package, still didn't work. Btw, this package is for python2.x, for python 3.x, the package name is python3-psycopg2. No dice on either. I am running python 3.8Supercargo
This is almost certainly the wrong way to solve the problem. You should never install pip packages as root, with the one exception of docker containers and you know what you are doing, and even then, it's probably better to make a process user.Wolfgang
J
125
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
Jervis answered 22/10, 2017 at 17:52 Comment(4)
I had this problem as well. Sadly, with my packages apt installing python3-dev will not work. What I did was a permutation of this answer: 1. sudo pip install --upgrade pip 2. sudo apt install -y libpq-dev postgresql-server-dev-all 3. sudo pip3 install psycopg2 Not sure if all sudo's are necessary but I don't care so I put them.Footpoundal
For virtual environments pip install psycopg2-binary is the solutionSchnauzer
I have Ubuntu and this sudo apt install libpq-dev python3-dev worked OK for me but I don't think this is the right way since it is not dedicated for my VirtualenvCrossed
Having 2 python version installed, I had to run sudo apt install libpq-dev python3.XX-dev where XX is my version (in my case, 3.12) to install the correct dependancies.Pomerania
C
59

I was getting this issue because I hadn't yet installed PostgreSQL on my machine.

For Mac

brew install postgresql

For Mac without brew

python3 -m pip install psycopg2-binary
Compton answered 27/8, 2018 at 16:16 Comment(3)
Yes, it did too.Burne
This worked for me on MacOS Big SurMischief
after searching for 2 days - this worked for me, thank you.Stratovision
P
28

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.

Pamphylia answered 14/5, 2019 at 1:13 Comment(3)
Clean and fast solution. It did not require any other library installation. Thank you.Infantilism
From the documentation of psycopg2 (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. please add this to your answer.Counteroffensive
The best choice for a dev machine, no need to install the entire postgresql package as other answers suggest.Muriah
M
19

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

Moramorabito answered 30/1, 2016 at 17:16 Comment(3)
With this is, how could you standardize the installation with requirements.txt?Hartshorn
I am on Ubuntu Buster, installed this python-psycopg2 package, still didn't work. Btw, this package is for python2.x, for python 3.x, the package name is python3-psycopg2. No dice on either. I am running python 3.8Supercargo
This is almost certainly the wrong way to solve the problem. You should never install pip packages as root, with the one exception of docker containers and you know what you are doing, and even then, it's probably better to make a process user.Wolfgang
A
5

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
Adala answered 2/8, 2021 at 11:3 Comment(0)
L
3

Upgrading pip worked for me: pip install --upgrade pip

Lasting answered 16/8, 2017 at 20:51 Comment(0)
C
1

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.

Cayenne answered 30/11, 2020 at 11:11 Comment(0)
C
1

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
Clarkin answered 24/12, 2021 at 10:9 Comment(1)
I could not find that library on my Fedora 35. Doing a "sudo dnf search libpq", found this one, libpq-devel.x86_64, which contains "pg_config" executableSupersensitive
O
1

If you installed Postgres.app on macOS, add its bin directory to the PATH environment variable:

export PATH=/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH
Opportunism answered 4/6, 2022 at 23:35 Comment(0)
T
1

For Mac

I had this issue too. I had followed a guide that had me run

brew install postgresql@15

This worked, but I kept getting the error

Error: pg_config executable not found.

So I then tried to brew install postgres without the @15 after it, and it worked! So, if you have installed postgresql@15 or @any-number, try to brew install without a number.

brew install postgresql
Triangular answered 16/2, 2023 at 2:48 Comment(0)
E
1

Your error is becouse didn't install the prerequisites on your machine or environment.

You can install prerequisites from here.

In this case probably your missing prerequisite is postgresql you can try below instructors.

For Mac

brew install postgresql

For Linux

sudo apt-get install postgresql
Erlina answered 24/2, 2023 at 9:43 Comment(1)
Welcome to StackOverflow! Your answer seems similar to the other answers on this question. If you think it's different, it'd help future viewers if you edit your answer to add clarification on how it differs from other answers. If they provide the same answer as yours, it's enough to just upvote the ones that provide the most helpful explanation.Mucor
M
0

For mac os

install postgresql with : brew install postgresql

then

install psycopg2 with : pip install psycopg2

worked for me

Mahau answered 31/1, 2022 at 23:33 Comment(0)
V
0

Incase you are using PyCharm/Visual Code Studio terminal, don't do it. Use the default system terminal, as it ensures correct format of brew install is done.

On M1 pro with Intel chip:

brew install postgresql

pip install psycopg2

Valise answered 5/9, 2022 at 8:55 Comment(0)
D
0

For me just this worked on Mac:

pip install --upgrade pip
pip install psycopg2-binary
Dorthadorthea answered 12/11, 2022 at 14:40 Comment(0)
N
-2

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.

Nit answered 29/7, 2016 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.