Installing psycopg2 on OS X
Comments: 29 - 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!