74

Is it possible to install psycopg2 into a virtualenv when PostgreSQL isn't installed on my development system—MacBook Pro with OS X 10.6?

When I run pip install psycopg2 from within my virtualenv, I received the error shown below.

I'm trying to connect to a legacy database on a server using Django, and I'd prefer not to install PostgreSQL on my development system if possible.

Why not install PostgreSQL?

I received an error when installing PostgreSQL using homebrew. I have Xcode4—and only Xcode4—installed on my MacBook Pro and am thinking it's related to missing gcc 4.0. However, this is a problem for another StackOverflow question.

Update 8:37 AM on April 12, 2011: I'd still like to know if this is possible without installing PostgreSQL on my MacBook Pro. However, I ran brew update and forced a reinstallation of ossp-uuid with brew install --force ossp-uuid and now brew install postgresql works. With PostgreSQL successfully installed, I was able to pip install psycopg2 from within my virtualenv.

Error from pip install psycopg2

$ pip install psycopg2
Downloading/unpacking psycopg2
  Running setup.py egg_info for package 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'.
    Complete output from command python setup.py egg_info:
    running 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 dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
warning: manifest_maker: standard file '-c' not found

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
Storing complete log in /Users/matthew/.pip/pip.log

Preliminary Research

Below are the articles I read as preliminary research:

Community
  • 1
  • 1
Matthew Rankin
  • 400,554
  • 38
  • 116
  • 156
  • 1
    see the correct answer here: http://stackoverflow.com/questions/5420789/how-to-install-psycopg2-with-pip-on-python – osmosis Jan 17 '12 at 04:46
  • that question's answer is for debian/ubuntu, so does not help if you are using osx. – Tim Feb 11 '12 at 11:21
  • On Centos 6 and Postgres 9.1 set the correct path to pg_config in your virtualenv: `export PATH=/usr/pgsql-9.1/bin:"$PATH"` – elim Jun 15 '12 at 08:43
  • 2
    Installing PostgreSQL should not be a requirement to develop django. We need to do this because our requirements.txt for the server lists psycopg without us ever using it locally. It's all a bit too tightly coupled and causes a lot of configuration grief. – Alper May 27 '14 at 16:01
  • Wow, not one answer to this posting concerning the specifics of a virtualenv on Mac setup. Correct me if I'm wrong. – john hight Sep 17 '14 at 23:23
  • brew install postgresql worked for me too. What threw me off was that I had the Heroku postgres93 app installed and running assuming that would be acceptable. – topherjaynes Nov 03 '14 at 02:46

14 Answers14

74
apt-get install libpq-dev

helped me on debian squeeze

j0k
  • 21,914
  • 28
  • 75
  • 84
Yevgeniy Shchemelev
  • 3,341
  • 1
  • 26
  • 37
50

psycopg depends on pg_config command, and if you don't have it, you can't install psycopg.

If system installation is a problem to you, why don't you try compiling PostgreSQL and including generated bin files in your $PATH? Like:

export PATH=/path/to/compiled/postgresql/bin:"$PATH"
pip install psycopg2
Matthew Rankin
  • 400,554
  • 38
  • 116
  • 156
Hugo Tavares
  • 23,060
  • 5
  • 45
  • 43
28
brew install postgresql

export LDFLAGS="-L/usr/local/opt/openssl/lib"

export CPPFLAGS="-I/usr/local/opt/openssl/include"

pip3 install psycopg2
Michael Kemmerzell
  • 3,294
  • 4
  • 16
  • 30
bruinspaw
  • 481
  • 5
  • 4
  • 5
    For some reason this worked for me on Mac OS X when several other solutions didn't. I already had postgresql installed and pg_config on my PATH, but still got the same error when installing psycopg2. The two `export` lines solved it for me. – George Bezerra Aug 07 '19 at 21:16
  • 2
    After several hours of struggling with this error this solution solved my probelm. thank you. – BobyCloud Jan 18 '20 at 22:48
  • Can somebody explain why this works? What has openssl got to do here ? – Ananta K Roy Nov 18 '20 at 15:37
  • 1
    @AnantaKRoy openssl is required by psycopg for secure ssl connections to postgres. The flags are the key here. Apple has moved around openssl and the setups are looking for them somewhere else. – Snap E Tom Dec 21 '20 at 08:42
11
apt-get install libpq-dev

helped me in debian squeeze too . After that do pip install psycopg2. I faced problem of pg_config not found problem when i was setting up my environment on heroku , now its working fine .

j0k
  • 21,914
  • 28
  • 75
  • 84
Ashwini Kumar
  • 119
  • 1
  • 4
5

You need to install the python-dev package in order to make use of python extensions such as psycopg2. I don't know how to install packages in mac but I run the following commands to install a python package on my Ubuntu machine.

sudo apt-get install python-dev 

Or

sudo apt-get install python3-dev

if you are using Python3.x.

Once the installation is finished run the following command within your virtual environment.

pip install psycopg2
orthodoxpirate
  • 1,514
  • 15
  • 16
4

You don't need the complete PostgreSQL installed: you only need the client side libraries.

piro
  • 11,767
  • 4
  • 32
  • 35
  • 1
    Do you have a recommendation on installing the PostgreSQL client-side libraries on OS X? – Matthew Rankin May 12 '11 at 11:57
  • I have no recommendation, I am not an OS X user. The fink build uses postgresql90-dev to build (probably for the .h) and postgresql90-shlibs (probably for the libpq), but I don't know how to rank this option w.r.t. other packages (which I don't know about) or building the libpq from source. Ref. http://fink.cvs.sourceforge.net/fink/dists/10.4/unstable/main/finkinfo/database/psycopg2-py.info?view=markup – piro May 13 '11 at 17:06
3

I solved it in MAC OSX using :

$ wget https://ftp.postgresql.org/pub/source/v9.5.3/postgresql-9.5.3.tar.bz2
$ tar xfv postgresql-9.5.3.tar.bz2
$ cd postgresql-9.5.3
$ ./configure
$ make
$ cd src/bin/pg_config
$ export PATH=`pwd`:"$PATH"
$ pip install psycopg2
wpercy
  • 8,491
  • 4
  • 29
  • 39
ji-ruh
  • 706
  • 1
  • 7
  • 24
1

Using the method you described in your April 12th update, I was able to install PostgreSQL(+1). Note that I originally was running Python 2.7.1 (32bit) and homebrew threw several errors and warnings regarding using a 32bit version of Python. I have since switched the 64/32 bit version of 2.7.1 and it works like a champ.

Regarding the pyscopg2, I was able to install it into my virtual environment from source by editing setup.cfg. I set pg_config to correct path inside homebrew's Cellar (pg_config=/usr/local/Cellar/postgresql/9.0.4/bin/pg_config). After saving the changes, I ran python setup.py install with zero issues. It's worth noting that I did not set the Mac's default python setting to 32 bit. I used the new 64 bit from start to finish.

After looking over some of the documentation, I think if I added homebrew's postgresql path to the system path I could have used pip to install it.

Reference:
http://favosdream.blogspot.com/2009/09/make-psycopg2-and-readline-work-in-snow.html

Update 6-8-2011:
While porting a project written on OS X to Windows 7, I found out that I had to install PostgreSQL on Windows as well. This ended up creating another user on my start up screen and other things that I just didn't like. While doing some digging I found Windows drivers for PostgreSQL here. I have since uninstalled the full PostgreSQL and installed the ODBC drivers which, thus far, work great.

To address the original question, after doing a bit more digging I think I found the equivalent ODBC for OS X here. I have not had a chance to try them out, but the concept works very well on Windows 7. I will update this when I get a chance to try them out. Until then, I hope this helps.

Adam Lewis
  • 6,307
  • 6
  • 36
  • 59
  • On Windows, you do not need to install Postgresql, but merely have the binaries in your path. This can be accomplished by downloading a zip file of the binaries http://www.enterprisedb.com/products-services-training/pgbindownload and adding that directory to your path (i.e. changing the PATH environment variable). Copy them somewhere safe/known before adding the path to make life easier! – Cory Dolphin Aug 26 '12 at 07:30
  • @wcdolphin Thanks for the input. That sure would have made life easier. – Adam Lewis Aug 26 '12 at 23:48
1

All from the above doesn't work for me (OS Catalina 10.15.1)

There was a conflict with classical postgres and homebrew version. Please delete homebrew version by command

$ brew uninstall postgresql

and then install it from the official website:

$ sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

It is actual for a simple psycopg2 install or django-heroku package.

John Smith
  • 822
  • 10
  • 23
Iron Cow
  • 11
  • 1
1

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
0

As I never needed to install postgresql database on this server I installed the following libraries on Ubuntu 14_04 version before running pip install psycopg2 on the same server

apt-get install libpq-dev python-dev and then executed pip install psycopg2 within virtual env.

Output Collecting psycopg2 Using cached psycopg2-2.6.1.tar.gz Building wheels for collected packages: psycopg2 Running setup.py bdist_wheel for psycopg2 Stored in directory: /root/.cache/pip/wheels/e2/9a/5e/7b620848bbc7cfb9084aafea077be11618c2b5067bd532f329 Successfully built psycopg2 Installing collected packages: psycopg2 Successfully installed psycopg2-2.6.1

AKV
  • 161
  • 4
  • 12
0

Use pip install psycopg2-binary, it worked for me when pip install psycopg2 wasn't working.

Ani Menon
  • 23,084
  • 13
  • 81
  • 107
0

I know you are asking for development environment but if you are deploying on server say, Heroku. Just add below line in the requirements.txt of your project.

django-heroku==0.3.1

As this package itself will install the required packages like psycopg2 on server deployment.

0

I solved this issue by chance with:

brew install psycopg2

instead of

pip install psycopg2

Bijan Negari
  • 133
  • 2
  • 9