Psycopg on Mac OS 10.8 with Python 2.7 and Postgresql 9.2

I don't have time for a full tutorial, but here is how I managed, after weeks of trying without luck, to get Psycopg2 to work with Python 2.7.3 and PostgreSQL 9.2 on Mac OS 10.8, Mountain Lion.

The problem I was having, after installing Psycopg using python setup.py install was related to this error when running import psycopg2 from a Python shell. Symbol not found: _PQbackendPID.

This fixed it:

  1. You'll need XCode from the App Store and the Command Line Tools (Google it if this sounds like gibberish).
  2. Follow the instructions about exporting your path here: http://www.jonathandean.com/2011/08/postgresql-8-4-on-mac-os-x-10-7-lion/. This will make your Postgresql 9.2 the default. You can check if it already is by typing which psql in a Terminal window.
  3. Create a Universal libpq.dylib following instructions here: http://contestclash.blogspot.com/2009/09/building-psycopg2-on-snow-leopard.html. Copy the new file to wherever your Postgresql is. In my case it was /Library/PostgreSQL/9.2/lib. These instructions solved the 64/32 bit architecture problem. (This crucial piece of info was burried in Google and took quite a while to find. All other solutions suggested running Python in 32 bit mode, which didn't work).
  4. Modify the psycopg2 setup.cfg file following these instructions (only the last part, right after "Change setup.cfg so it looks like the following:"): http://blog.timc3.com/2010/08/20/psycopg2-os-x-_pqbackendpid/.
  5. Create the simlinks mentioned in the comments here in order to fix a new problem with SSL and Crypto libraries: http://psycopg.lighthouseapp.com/projects/62710/tickets/111-library-not-loaded-libssl100dylib
  6. Now build psycopg2 using "python setup.py install".
  7. You should be able to import psycopg2 from a Python shell or from IDLE (I actually did it for EPD free).

Many, many thanks to each of the four sources cited in steps 2-6. You did all the work. All I needed to do was find which instructions to follow.

I hope this helps others struggling with this issue.

Add new comment