# virtualenv – How to install psycopg2 with “pip” on Python?

## The Question :

536 people think this question is useful

I’m using virtualenv and I need to install “psycopg2”.

I have done the following:

pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160



And I have the following messages:

Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Running setup.py egg_info for package from http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160

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'.
Complete output from command python setup.py egg_info:
running egg_info

creating pip-egg-info\psycopg2.egg-info

writing pip-egg-info\psycopg2.egg-info\PKG-INFO

writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt

writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'

warning: manifest_maker: standard file '-c' 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
Storing complete log in C:\Documents and Settings\anlopes\Application Data\pip\p
ip.log



My question, I only need to do this to get the psycopg2 working?

python setup.py build_ext --pg-config /path/to/pg_config build ...


The Question Comments :
• Did it work when you tried python setup.py build_ext –pg-config /path/to/pg_config build … ??
• For python 3.4 or python 3.5, you have to install the corresponding verion’s dev package like sudo apt install libpq-dev python3.4-dev or sudo apt install libpq-dev python3.5-dev

## The Answer 1

879 people think this answer is useful

Note: Since a while back, there are binary wheels for Windows in PyPI, so this should no longer be an issue for Windows users. Below are solutions for Linux, Mac users, since lots of them find this post through web searches.

# Option 1

Install the psycopg2-binary PyPI package instead, it has Python wheels for Linux and Mac OS.

pip install psycopg2-binary



# Option 2

Install the prerequsisites for building the psycopg2 package from source:

## Debian/Ubuntu

### Python 3

sudo apt install libpq-dev python3-dev



You might need to install python3.8-dev or similar for e.g. Python 3.8.

### Python 21

sudo apt install libpq-dev python-dev



If that’s not enough, try

sudo apt install build-essential



or

sudo apt install postgresql-server-dev-all



as well before installing psycopg2 again.

## CentOS 6

1 Really? It’s 2020

## The Answer 2

118 people think this answer is useful

On CentOS, you need the postgres dev packages:

sudo yum install python-devel postgresql-devel



That was the solution on CentOS 6 at least.

## The Answer 3

88 people think this answer is useful

On Mac Mavericks with Postgres.app version 9.3.2.0 RC2 I needed to use the following code after installing Postgres:

sudo PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin pip install psycopg2 ## The Answer 4 80 people think this answer is useful if you’re on a mac you can use homebrew brew install postgresql  And all other options are here: http://www.postgresql.org/download/macosx/ Good luck ## The Answer 5 62 people think this answer is useful I recently configured psycopg2 on a windows machine. The easiest install is using a windows executable binary. You can find it at http://stickpeople.com/projects/python/win-psycopg/. To install the native binary in a virtual envrionment, use easy_install: C:\virtualenv\Scripts\> activate.bat (virtualenv) C:\virtualenv\Scripts\> easy_install psycopg2-2.5.win32-py2.7-pg9.2.4-release.exe  ## The Answer 6 28 people think this answer is useful For Python 3 you should use sudo apt-get install libpq-dev python3-dev under Debian. ## The Answer 7 27 people think this answer is useful This is what worked for me (On RHEL, CentOS: sudo yum install postgresql postgresql-devel python-devel  And now include the path to your postgresql binary dir with you pip install: sudo PATH=$PATH:/usr/pgsql-9.3/bin/ pip install psycopg2



Make sure to include the correct path. Thats all 🙂

UPDATE: For python 3, please install python3-devel instead of python-devel

## The Answer 8

21 people think this answer is useful

If you using Mac OS, you should install PostgreSQL from source. After installation is finished, you need to add this path using:



## The Answer 11

6 people think this answer is useful

I’ve done this before where in windows you install first into your base python installation.

Then, you manually copy the installed psycopg2 to the virtualenv install.

It’s not pretty, but it works.

## The Answer 12

5 people think this answer is useful

Besides installing the required packages, I also needed to manually add PostgreSQL bin directory to PATH.
$vi ~/.bash_profile Add PATH=/usr/pgsql-9.2/bin:$PATH before export PATH.
$source ~/.bash_profile $pip install psycopg2

## The Answer 13

4 people think this answer is useful

Before you can install psycopg2 you will need to install the python-dev package.

If you’re working from Linux (and possibly other systems but i can’t speak from experience) you will need to make sure to be quite exact about what version of python your running when installing the dev package.

For example when I used the command:

sudo apt-get install python3-dev



I still ran into the same error when trying to

pip install psycopg2



As I am using python 3.7 I needed to use the command

sudo apt-get install python3.7-dev



Once I did this I ran into no more issues. Obviously if your on python version 3.5 you would change that 7 to a 5.

## The Answer 14

3 people think this answer is useful

On windows XP you get this error if postgres is not installed …

## The Answer 15

3 people think this answer is useful

I installed Postgresql92 using the RedHat / CentOS repository on PG’s downloads site http://www.postgresql.org/download/linux/redhat/

To get pg_config, I had to add /usr/pgsql-9.2/bin to PATH.

## The Answer 16

2 people think this answer is useful

I’ve been battling with this for days, and have finally figured out how to get the “pip install psycopg2” command to run in a virtualenv in Windows (running Cygwin).

I was hitting the “pg_config executable not found.” error, but I had already downloaded and installed postgres in Windows. It installed in Cygwin as well; running “which pg_config” in Cygwin gave “/usr/bin/pg_config”, and running “pg_config” gave sane output — however the version installed with Cygwin is:

VERSION = PostgreSQL 8.2.11

This won’t work with the current version of psycopg2, which appears to require at least 9.1. When I added “c:\Program Files\PostgreSQL\9.2\bin” to my Windows path, the Cygwin pip installer was able to find the correct version of PostgreSQL, and I was able to successfully install the module using pip. (This is probably preferable to using the Cygwin version of PostgreSQL anyway, as the native version will run much quicker).

## The Answer 17

2 people think this answer is useful

On Fedora 24: For Python 3.x

sudo dnf install postgresql-devel python3-devel

sudo dnf install redhat-rpm-config



Activate your Virtual Environment:

pip install psycopg2



## The Answer 18

2 people think this answer is useful

Psycopg2 Depends on Postgres Libraries. On Ubuntu You can use:

apt-get install libpq-dev



Then:

pip install psycopg2



## The Answer 19

2 people think this answer is useful

For MacOS,

Use the below command to install psycopg2, works like charm!!!

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install psycopg2



## The Answer 20

1 people think this answer is useful

For lowly Windows users were stuck having to install psycopg2 from the link below, just install it to whatever Python installation you have setup. It will place the folder named “psycopg2” in the site-packages folder of your python installation.

After that, just copy that folder to the site-packages directory of your virtualenv and you will have no problems.

here is the link you can find the executable to install psycopg2

http://www.lfd.uci.edu/~gohlke/pythonlibs/

## The Answer 21

1 people think this answer is useful

On OpenSUSE 13.2, this fixed it:

sudo zypper in postgresql-devel



## The Answer 22

1 people think this answer is useful

I could install it in a windows machine and using Anaconda/Spyder with python 2.7 through the following commands:

 !pip install psycopg2



Then to establish the connection to the database:

 import psycopg2



## The Answer 23

1 people think this answer is useful

In Arch base distributions:

sudo pacman -S python-psycopg2
pip2 install psycopg2  # Use pip or pip3 to python3



## The Answer 24

1 people think this answer is useful

if pip is not working than you can download .whl file from here https://pypi.python.org/pypi/psycopg2 extract it.. than python setup.py install

## The Answer 25

0 people think this answer is useful

On Ubuntu I just needed the postgres dev package:

sudo apt-get install postgresql-server-dev-all



*Tested in a virtualenv

## The Answer 26

0 people think this answer is useful

On OSX 10.11.6 (El Capitan)

brew install postgresql



## The Answer 28

0 people think this answer is useful

I was having this problem, the main reason was with 2 equal versions installed. One by postgres.app and one by HomeBrew.

If you choose to keep only the APP:

brew unlink postgresql
pip3 install psycopg2



## The Answer 29

0 people think this answer is useful

On macOS Mojave make sure you on newest update with Command Line Tools 10.3 – that worked for me – updated it with Software Update, previous version of Command Line Tools on Mojave did not work for me.

## The Answer 30

0 people think this answer is useful

Make sure Postgres is installed and PATH is updated before running pip install psycopg2

export PATH="\$PATH:/Applications/Postgres.app/Contents/Versions/12/bin"