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.
- Installing psycopg2 on OS X
- psycopg2 and 64-bit Apache on Leopard
- psycopg2 OS X – _PQbackendPID
- Python, psycopg2, postgresql84 and Mac OS X Snow Leopard
If you know enough to write a comprehensive article you are welcome!
Comments (7 posted).
Feel free to add a new comment. You can use reST syntax.
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:
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.
Assuming you have "pip" installed:
Installing in a virtual environment
Create your virtual environment:
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:
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.
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
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.
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.
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.
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.)