Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install psycopg2 with pg_config error?

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

like image 502
Chris Avatar asked Jan 30 '16 16:01

Chris


People also ask

What is Pg_config file?

The view pg_config describes the compile-time configuration parameters of the currently installed version of PostgreSQL. It is intended, for example, to be used by software packages that want to interface to PostgreSQL to facilitate finding the required header files and libraries.

Where is Pg_config Linux?

pg_config is in postgresql-devel ( libpq-dev in Debian/Ubuntu, libpq-devel on Centos/Fedora/Cygwin/Babun.)

Does psycopg2 work with Python 3?

The current psycopg2 implementation supports: Python 2 versions from 2.6 to 2.7. Python 3 versions from 3.2 to 3.6. PostgreSQL server versions from 7.4 to 9.6.


2 Answers

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 
like image 134
jahmed31 Avatar answered Sep 29 '22 20:09

jahmed31


I was getting this issue because I hadn't yet installed PostgreSQL on my machine yet. On mac just a simple brew install postgresql fixed the problem.

like image 36
Seph Cordovano Avatar answered Sep 29 '22 19:09

Seph Cordovano