Installing psycopg2 on OS X
Comments: 28 - 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.
Comment by Johann Spies - April 24, 2010 @ 9:44 am
My installation on Snow Leopard ends with
sudo python setup.py install
running install
running build
running build_py
running build_ext
building ‘psycopg2._psycopg’ extension
gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION=”2.0.14 (dt dec ext pq3)” -DPG_VERSION_HEX=0×080403 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -DHAVE_PQPROTOCOL3=1 -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -I. -I/Library/PostgreSQL/8.4/include -I/Library/PostgreSQL/8.4/include/postgresql/server -c psycopg/psycopgmodule.c -o build/temp.macosx-10.3-i386-2.5/psycopg/psycopgmodule.o
cc1: error: unrecognized command line option “-Wno-long-double”
cc1: error: unrecognized command line option “-Wno-long-double”
lipo: can’t figure out the architecture type of: /var/tmp//ccmP38pg.out
error: command ‘gcc’ failed with exit status 1
How do I fix it?
Comment by carriere - May 10, 2010 @ 9:29 am
I’ve got the same error
psycopg/psycopgmodule.c:822: error: ‘PyType_GenericAlloc’ undeclared (first use in this function)
error: command ‘gcc’ failed with exit status 1
did you find a solution ?
Comment by Peter - August 5, 2010 @ 2:22 pm
Installing psycopg2 on Mac OS X
Friday, 30. October 2009 Filed in: Apple
psycopg2 is an adapter to PostgreSQL for Python. Install this software on GNU/Linux is easy because many distros offer binary packages ready to install it. On Mac OS X is different so we explain how to do that.
Of course, you need to install PostgreSQL and Python before to install this adapter. When you are ready you must follow this steps:
$ wget http://initd.org/pub/software/psycopg/psycopg2-2.0.12.tar.gz
$ tar -zxvf psycopg2-2.0.12.tar.gz
$ cd psycopg-2.0.12
$ export PATH=$PATH:/Library/PostgreSQL/8.4/bin/
$ python setup.py build
$ sudo python setup.py install
Then you can use this adapter in your Python scripts:
>> import psycopg2
Comment by Vlad - February 11, 2011 @ 1:11 pm
This doesnt work in Snow Leopard. Do you have any updates on how to make it run in 10.6?
I still get the following:
Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import psycopg2
Traceback (most recent call last):
File “”, line 1, in
File “/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/psycopg2/__init__.py”, line 69, in
from _psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
Referenced from: /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/psycopg2/_psycopg.so
Expected in: dynamic lookup
Will appreciate any feedback!
Comment by QWorks - October 30, 2011 @ 6:28 pm
Thank you a million times over. I have been trying to get psycopg2 on OS X Lion for like 4 hours now…This was the single thing that finally got rid of my headache!!
Now on to developing
Pingback by Installing psycopg2 on Mac OS X | Patrick's playground - March 1, 2012 @ 5:09 am
[...] a quick search I found the right solution here. It only needed a little bit adjustment as I don't use PostgresPlus, but just plain [...]
Comment by israe barba - April 1, 2012 @ 9:16 am
I can’t install on OSX Lion … I get the following
running install
running build
running build_py
running build_ext
building ‘psycopg2._psycopg’ extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3 -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION=”2.4.5 (dt dec pq3 ext)” -DPG_VERSION_HEX=0×090103 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -I/Library/PostgreSQL/9.1/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I. -I/Library/PostgreSQL/9.1/include -I/Library/PostgreSQL/9.1/include/postgresql/server -c psycopg/psycopgmodule.c -o build/temp.macosx-10.3-fat-2.7/psycopg/psycopgmodule.o
unable to execute gcc-4.0: No such file or directory
error: command ‘gcc-4.0′ failed with exit status 1
Any idea?? thanks
Leave a comment