Nate Silva

Mar 29 2010

How to build the pscyopg2 Python PostgreSQL interface on Mac OS X Snow Leopard

These instructions may work on earlier versions of OS X, but I’ve only tested on Snow Leopard.

A common problem for Python programmers using Mac OS X is how to install psycopg2, the standard Python interface for PostgreSQL.

If you Google for the instructions, you’ll get all kinds of advice, most of which involves using MacPorts or installing a complete server installation of PostgreSQL on your Mac.

I don’t want to do that.

I don’t want to run a database server on my local Mac. I want only the necessary PostgreSQL client libraries and I want to use the OS X native build system, not MacPorts.

It’s actually easy to do this. The instructions may look complicated, but it’s only because I’ve added a lot of explanation to them.

Step 1: Install the PostgreSQL client library

  1. Download the PostgreSQL source code from http://www.postgresql.org/ftp/source/. Don’t download Mac OS X binaries, as that would be a complete server installation.
  2. Un-tar the source code and cd into its directory. Then run:
    • CFLAGS="-arch i386 -arch x86_64 -arch ppc" ./configure --with-openssl
      • This CFLAGS setting will get you a universal binary. Without it you would only get a 64-bit Intel binary (assuming you have a 64-bit Intel Mac), or a 32-bit Intel binary (if you have a 32-bit Intel Mac). We also include PowerPC support, for the unlikely event that you have a program that runs under Rosetta that needs to use the PostgreSQL libraries.
  3. We are going install only the client parts of PostgreSQL. These instructions are based on the PostgreSQL manual, section 15.5.:
    • sudo make -C src/bin install
    • sudo make -C src/include install
      • In my experience, the error about utils/fmgroids.h can be ignored.
    • sudo make -C src/interfaces install
    • sudo make -C doc install

Step 2: Install Psycopg2

  1. Download and un-tar the Psycopg source code.
  2. Edit the psycopg2 setup.cfg file. Add or update the following lines:
    • have_ssl=1
    • pg_config=/usr/local/pgsql/bin/pg_config
  3. Build and install it:
    • python setup.py build
    • sudo python setup.py install
  4. Test it:
    • $ python
    • >>> import psycopg2
    • >>> psycopg2.__version__
      '2.0.14 (dt dec ext pq3)'

Looks good!

3 notes

  1. natesilva posted this
Page 1 of 1