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.
On Windows, You may want to install the Windows port of Psycopg, which is recommended in psycopg's documentation.
Simply run the following:
sudo apt install libpq-dev
Fixed the issue for me
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
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 :)
On alpine, the library containing pg_config
is postgresql-dev
. To install, run:
apk add postgresql-dev
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.
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
On Mac OS X, I solved it using the homebrew package manager
brew install postgresql
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
apt-get build-dep python-psycopg2
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
For mac users, extend your path variable to include PostgreSQL like this export PATH=$PATH:/Library/PostgreSQL/12/bin
.
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.
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
sudo yum install postgresql-devel (centos6X)
pip install psycopg2==2.5.2
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 aserror: 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 anerror: libpq-fe.h: No such file or directory
you are missing them.- The
pg_config
program: it is usually installed by thelibpq-dev
package but sometimes it is not in aPATH
directory. Having it in thePATH
greatly streamlines the installation, so try runningpg_config --version
: if it returns an error or an unexpected version number then locate the directory containing thepg_config
shipped with the rightlibpq
version (usually/usr/lib/postgresql/X.Y/bin/
) and add it to thePATH
:$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
You only need
pg_config
to compilepsycopg2
, 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
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!
run the following command to resolve pg_config executable not found:-
sudo apt-get install --reinstall libpq-dev
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
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
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"
.
for CentOS/RedHat make sure that /etc/alternatives/pgsql-pg_config
is a non-broken symlink
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
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.
pg_config
is in postgresql-devel
(libpq-dev
in Debian/Ubuntu, libpq-devel
on Centos/Cygwin/Babun.)
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.
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
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.
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.
You should add python requirements used in Postgres on Ubuntu. Run:
sudo apt-get install libpq-dev python-dev
Installing python-psycopg2 solved it for me on Arch Linux:
pacman -S python-psycopg2
On Gentoo You have to execute the following
# Install postgresql client libraries
sudo emerge postgresql-base
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.
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 .
If you are using mac then make sure you have installed Postgresql in your machine.
Try to add it to PATH:
PATH=$PATH:/usr/pgsql-9.1/bin/ ./pip install psycopg2
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.
On Linux Mint sudo apt-get install libpq-dev
worked for me.
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.
a simple solution can be
pip install psycopg2==2.7.5
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.
You need to upgrade your pip before installing psycopg2. Use this command
pip install --upgrade pip
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.
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.
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
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
sudo apt-get install libpq-dev
works for me on Ubuntu 15.4
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
Source: Stackoverflow.com