Links about building Psycopg on Mac OS X

Posted by Daniele Varrazzo on November 11, 2010
Tagged as os-x, recipe

Looks like building Psycopg on OS X is tricky: the code needs no tweak, but linking against the right library seems problematic.

Here is a list of articles I've found about the matter or I've been suggested: please take a look at these and leave a comment if you wish to suggest anything else or share your experience.

  1. Installing psycopg2 on OS X
  2. psycopg2 and 64-bit Apache on Leopard
  3. psycopg2 OS X – _PQbackendPID
  4. Python, psycopg2, postgresql84 and Mac OS X Snow Leopard

If you know enough to write a comprehensive article you are welcome!

Comments (17 posted).

Comment #1
Charlie Clark
Nov. 12, 2010, 11:03 a.m.

My advice is to use MacPorts or something similar for installing Python and Postgres. There are several reasons for this: Apple only ships Python with a new version of the OS which can get outdated fairly quickly and they also use non-standard paths for installation which can make it difficult to follow any documentation on installing software on Unix systems. Using MacPorts keeps your software up to date and prevents you getting headaches working out what the error messages mean.

There are also ports for psycopg2 which you can use. Or you can install the relevant virtualenv package. This depends largely on what you are doing. Virtualenv is more suitable for working with applications that others have written such as BFG, Zope or Django where it may be important to have specific versions of libraries. If you use virtualenv you can isolate a project from a system installation by using the --no-site-packages argument.

Using MacPorts

Installing psycopg2 for Python 2.6:

sudo port install py26-psycopg2

Installing manually

psycopg2 compiles without modification assuming pg_config can be found. By default MacPorts doesn't add the Postgres binaries to the path and without this psycopg2 fails to compile. You can either add the relevant path for Postgres binaries to your session variables or profile.

export PATH=$PATH:/opt/local/lib/postgresql90/bin

Assuming you have "pip" installed:

sudo pip install psycopg2

Installing in a virtual environment

Create your virtual environment:

virtualenv-2.6 --no-site-packages myproject

This will create a folde myproject with it's own Python binary and library space. I highly recommend you do not use "activate" in the virtual environment as this messes around with environmental variables and can be extremely confusing:

cd myproject
bin/pip install psycopg2

Comment #2
piro
Nov. 12, 2010, 11:34 a.m.

Charlie: great overview!

Notice that, if you install the library manually, you can make it found the pg_config binary by setting its path in setup.cfg, e.g.

pg_config=/opt/local/lib/postgresql90/bin/pg_config

Comment #3
Kurt Schwehr
Dec. 6, 2010, 2:46 p.m.

You can also use fink to install pyscopg2. Just make sure you have turned on the "unstable" tree with "fink configure". Unstable is where we update things more frequently.

fink install psycopg2-py27

http://pdb.finkproject.org/pdb/package.php/psycopg2-py27

-kurt

Comment #4
Adrian Nye
Sept. 26, 2011, 8:59 p.m.

Both the fink and MacOS ports of psycopg2 install their own separate copy of postgres. So then you have to make sure any natively installed version is uninstalled and no longer running and the new db has to be configured and all the databases reinstalled.

Comment #5
Nelson
Sept. 28, 2011, 12:46 a.m.

FWIW, I had no trouble at all building this on MacOS Lion using Homebrew's Python installation. I just did a "pip install psycopg2" and it built and installed fine in /usr/local with the rest of Homebrew.

Comment #6
Daniele Varrazzo
Oct. 7, 2011, 6:44 p.m.

This is probably the most straightforward explanation I've found to date:

http://hardlifeofapo.com/psycopg2-and-postgresql-9-1-on-snow-leopard/

It boils down to 1: set your $PATH, 2: set your ARCHFLAGS="-arch i386 -arch x86_64", 3: easy_install, or pip or whatever.

Comment #7
Chris V
March 19, 2012, 12:42 p.m.

Thanks, Daniele!

OS X Lion, using built-in Apache and Postgres.

Exported ARCHFLAGS as you said, and built/installed from source without a hitch. (Didn't need to change my PATH.)

Comment #8
Jurie
May 27, 2012, 7:14 p.m.

Today I installed PostgreSQL 9.1. using the EnterpriseDB package from http://www.postgresql.org/download/macosx/

Then I installed psycopg2 using pip.

I am using Python 2.7 on OS X 10.7.3.

When I run my web app I get this error message:

from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID ImportError: dlopen(/Users/jhorneman/dev/.virtualenvs/dbtest/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0 dylib Referenced from: /Users/jhorneman/dev/.virtualenvs/dbtest/lib/python2.7/site-packages/psycopg2/_psycopg.so Reason: image not found

I've googled all over the place and tried various things, but without any luck.

I interpret the error message to mean that psycopg2 wants OpenSSL 1.0.0, which is not installed on my Mac. The only instructions I've found on how to install OpenSSL 1.0.0. are "don't".

Any help would be most welcome.

Comment #9
Jurie
May 27, 2012, 10:10 p.m.

Problem solved, maybe.

I installed OpenSSL 1.0.0 using Homebrew, then I typed:

export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.1c/lib

And now the error message is gone. My app isn't running yet but that's probably a different problem ;)

Comment #10
Chad Whitacre
May 29, 2012, 10:45 p.m.

WFM if I export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.1/lib in the environment in which my application is running. I'm using the EnterpriseDB installer like Jurie.

I found a libpq.5.dylib in /usr/lib which I assumed was from a previous source install of mine and I removed it. Even after rebuilding psycopg2 the driver failed now trying to load libpq.5.dylib.

Check out the DLYD_PRINT_* envvars in man dyld for debugging aides.

Comment #11
Jurie
June 3, 2012, 6:27 p.m.

Chad: Thanks. Although I solved my problem using the Cellar path, I did also find a libssl.1.0.0.dylib in the PostgreSQL path you mention. Maybe I will uninstall the Homebrew SSL and switch to that one. But for now my problem has been solved.

Comment #12
Dan Koch
June 4, 2012, 12:04 a.m.

Had a similar experience as mentioned in comment #8 - no problems building or installing, rather in importing psycopg2 and getting the missing libssl.1.0.0.dylib error. Turns out that the PostgreSQL 9.0.7 installer from EnterpriseDB ships with that lib, but my Mac OS X 10.6.8 system was using an older version in /usr/lib. So I added symlinks for it and libcrypto.1.0.0.dylib which fixed the problem.

I usually modify the pg_config line in the setup.cfg file before I build/install. I also tried a few things with the library_dirs and libraries lines but had no success with the import error until adding the symlinks.

Comment #13
Alex R
June 8, 2012, 3:12 a.m.

I added symlinks for libssl.1.0.0.dylib and libcrypto however it is still giving error about checking for libssl.0.9.8. Any thoughts?

Comment #14
Alex R
June 8, 2012, 3:24 a.m.

Never mind, got it to work. On my system there were two symlinks libssl.dylib -> libssl.0.9.8.dylib and the same for libcrypto. Just needed to change those to point to the new 1.0.0 symlinks and now it works

Comment #15
Farhan
June 18, 2012, 2:30 p.m.

Hey guys, so i'm facing the same issue, and I can't really understand how to symlink the libssl.dylib to the new version. Any pointer?

Comment #16
yuyan zhang
Oct. 23, 2012, 12:44 p.m.

when i run 1. export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.1c/lib 2.python >>> import psycopg2 it's ok but if without setup 1,it still raise the error:

System Message: ERROR/3 (<string>, line 7)

Unexpected indentation.
Reason: image not found

System Message: WARNING/2 (<string>, line 8)

Block quote ends without a blank line; unexpected unindent.

why

Comment #17
Jonathan
Aug. 1, 2013, 6:33 p.m.

I had a similar issue, and wanted to share another approach.

I outlined it here: http://www.destructuring.net/2013/07/31/trouble-installing-psycopg2-on-osx/

using file on the dylib i learned that the PostgreSQL version I had was built for ppc & i386 ( odd combo, i know).

the solution was not just to export ARCHFLAGS, but I had to use lipo to strip 64bit out of the Python binary, then build & use psycopg2.

Feel free to add a new comment. You can use reST syntax.