Installing psycopg2 on OS X
Comments: 21 - Date: June 20th, 2008 - Categories: Code, Mac OS X
I have recently been doing some python development that uses the psycopg2 driver for PostgreSQL. On Linux, this worked wonderfully, and everything was installed using the regular easy_install method with no troubles. However on OS X it wasn’t quite so easy.
First I tried using easy_install but it failed with a long list of gcc warnings and errors. Looking near the beginning of the errors I saw:
Warning: /bin/sh: pg_config: command not found followed by
./psycopg/connection.h:27:22: error: libpq-fe.h: No such file or directory Well this makes sense as I’m using PostgreSQL Plus for OS X, and it doesn’t put it’s header files or libraries in a standard place. There is an easy workaround though.
Thanks to Luke Tucker who provided this easy solution: PATH=$PATH:/Library/PostgresPlus/8.3/bin/ sudo easy_install psycopg2
Original Method
This is left here for historical purposes. I recommend using the above command instead.
We’ll need to download the psycopg2 package and install it manually. Don’t worry, this is very easy and doesn’t require much work at all.
- Open you favorite terminal application.
- Download the package. Extract the package, enter the directory.
curl -o psycopg2.tar.gz http://www.initd.org/pub/software/psycopg/psycopg2-latest.tar.gz
tar xzf psycopg2.tar.gz
cd psycopg2-${version} - Edit the setup configuration file. Use whatever editor you’re comfortable with.
vim setup.cfg - Uncomment the include_dirs line and change it to read:
include_dirs=/Library/PostgresPlus/8.3/include - Uncomment the library_dirs line and change it to read:
library_dirs=/Library/PostgresPlus/8.3/lib - Save and quit your editor.
- Finally, build and install the package.
sudo python setup.py install
And now you should have a working PostgreSQL driver for Python. This was very useful for me since I like to use SQLAlchemy for my database work in Python and it uses psycopg2.
Alternative
As a alternative method, you can replace steps 4 and 5 above with:
- Uncomment pg_config line and change it to read:
pg_config=/Library/PostgresPlus/8.3/bin/pg_config
Thanks, pacopablo!
Comment by David - June 20, 2008 @ 9:54 pm
Thanks for this post. I have been doing a lot of searching on how to get psycopg2 installed with the system python on Leopard and haven’t found much of anything that was relevant or recent. I decided to do some more digging at the end of the day and your post popped up.
Worked like a charm!
Comment by jonypawks - June 20, 2008 @ 11:26 pm
No problem. I put it up here because it took me a while to get this figured out as well. Glad to hear its already helping someone.
Comment by pacopablo - August 22, 2008 @ 1:19 pm
Thanks for the article. It helped tremendously. Additionally, instead of explicitly specifying the `include_dirs` and `library_dirs` you can uncomment the `pg_config` line and set it to the location of the `pg_config` binary. In my case it is:
pg_config=/Library/PostgresPlus/8.3/bin/pg_config
Anyway, thanks very much for the info.
Comment by Christopher Arndt - September 29, 2008 @ 8:35 am
Thanks, for this tip. I had exactly the same problem. I switched my dev machine to Mac (because it has better specs) but I still find it hard to adapt to the missing out-of-the-box package management system that I learned to love on debian/Ubuntu.
I put a link to this post on the TurboGears doc wiki at http://docs.turbogears.org/1.0/DatabasePostgres.
Comment by jonypawks - September 29, 2008 @ 8:41 am
Thanks Chris. I was actually using TurboGears when I ran in to this issue in the first place.
Comment by John - October 11, 2008 @ 7:34 pm
Excellent! Many thanks for the help.
Comment by Weixi Yen - November 24, 2008 @ 8:49 pm
OMG!!! OMG!!!
OMG!!!
/worship
Comment by Brandon - December 25, 2008 @ 12:06 pm
Works perfectly with PostgreSQL 8.3, OS X 10.5.6 and Psychopg2-2.0.8
Comment by Luke Tucker - January 30, 2009 @ 10:36 am
Putting /Library/PostgresPlus/8.3/bin in your path when running easy_install should also allow the package to find pg_config. I was able to get it to install without editing the package after doing this.
Comment by oriste - February 5, 2009 @ 11:30 am
Thank you so much for sorting this out for a beginner!
Pingback by installing psycopg2 on Mac OS X « Waruna’s Geoblog - February 11, 2009 @ 9:11 am
[...] http://blog.jonypawks.net/2008/06/20/installing-psycopg2-on-os-x/ [...]
Comment by wkremser - February 12, 2009 @ 1:15 pm
Big thx to you. helped me a lot with my osx installation.
I am using a standard postgresql and here the path is: /Library/PostgreSQL8/bin
Pingback by Sinkro.net · Installare PsycoPg2 su OsX - February 17, 2009 @ 9:00 am
[...] http://blog.jonypawks.net/2008/06/20/installing-psycopg2-on-os-x/ [...]
Comment by Rich - March 3, 2009 @ 6:01 pm
Just wanted to give you a huge thanks for this! Was having the same issue not finding pg_config trying to install pygresql with easy_install, the method posted at the top worked perfectly for me.
Comment by Gloria - April 25, 2009 @ 10:48 am
Thanks, this was helpful
Comment by Maciej Konieczny - May 8, 2009 @ 2:04 pm
Thanks, this saved my day.
Comment by David Simmer - October 27, 2009 @ 10:03 am
Hugely helpful, thank you. In my case I’m using Fink, so my command was:
PATH=$PATH:/usr/local/pgsql/bin/ sudo easy_install psycopg2
Comment by David Simmer - October 27, 2009 @ 10:04 am
I should clarify, I was using Fink to install pyscopg2 into python2.4 on Snow Leopard, where fink doesn’t have pyscopg2 available for python2.4. If i were using python 2.5 or 2.6, a simple “fink install psycopg2″ would have been sufficient.
Comment by Peter - December 29, 2009 @ 4:39 pm
Thanks!! Your original one works, but not the shorter one for me…
Comment by Pat - February 2, 2010 @ 2:08 pm
None of the steps above worked for me. [Snow Leopard 10.6.2; PostgreSQL 8.4, pyscopg2-2.0.13]
A post on the Django Users list at osdir.com had the suggestion to update easy_install:
sudo easy_install -U setuptools
This followed by adding the PostgreSQL bin to my path allowed easy_install to succeed.
Comment by Pat - February 2, 2010 @ 2:10 pm
I pasted the wrong link in above. The correct one is here.
Leave a comment