Building postgresql8x and psycopg2 for x86_64 and i386 on Snow Leopard (OS X 10.6)

I’ve recently installed Apple’s new 64 bit OS Snow Leopard, on my work computer. I use postgresql extensivly together with python, and usually use apple’s bundled python2.5 for working with django.

As the daredevil I am, I wanted to recompile all my macports to use the new 64 bit system, and therefore deleted them all, and made a fresh install of macports. After building the postgresql81 port, I was about to build the psycopg2 python postgresql driver for python 2.5, when it gave me a warning about not being able to find some symbols in the postgresql library it had linked to. I quickly realized that this might be an architecture problem, and sure enough, it turns out that python 2.5 is a i386/ppc and python 2.6 is x86_64/i386/ppc binary, as can be seen here:

$ file `which python`
/usr/bin/python: Mach-O universal binary with 3 architectures
/usr/bin/python (for architecture x86_64):	Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386):	Mach-O executable i386
/usr/bin/python (for architecture ppc7400):	Mach-O executable ppc
$ file `which python2.5`
/usr/bin/python2.5: Mach-O universal binary with 2 architectures
/usr/bin/python2.5 (for architecture i386):	Mach-O executable i386
/usr/bin/python2.5 (for architecture ppc7400):	Mach-O executable ppc

The solution seemed so simple. Recompile postgresql81 for both architectures, and let the linker figure out the rest.

Building the postgresql81 port as the +universal variant, does not work. It has something to do with the fact, that the linker (ld) does not know how to produce a binary for multiple architectures. After a good nights sleep, the solution was only a trac ticket away.

So, to build a i386 and x86_64 version of postgresql8x via macports, you have to patch the Portfile, which is located in /opt/local/var/macports/sources/rsync.macports.org/release/ports/databases/postgresql81.

That can be done like this – notice that the patch seem to place the files wrong, so we’re moving them as well:

$ cd /opt/local/var/macports/sources/rsync.macports.org/release/ports/databases/postgresql81
$ curl -s http://trac.macports.org/raw-attachment/ticket/14619/combined_updated_universal.patch | sudo patch
$ sudo mkdir files/
$ sudo mv ld.sh files/
$ sudo mv patch_pg_config_h files/

Now you can go ahead and build the postgresql81 port with both architectures, like so:

$ sudo port install postgresql81 +universal

And then, finally, we can build the psycopg2 extension for python:

$ wget http://initd.org/pub/software/psycopg/psycopg2-2.0.12.tar.gz
$ tar zxf psycopg2-2.0.12.tar.gz
$ cd psycopg2-2.0.12
$ sudo python2.5 setup.py install
$ sudo python2.5 setup.py clean
$ sudo python2.6 setup.py install

And you’re off.

This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Building postgresql8x and psycopg2 for x86_64 and i386 on Snow Leopard (OS X 10.6)

  1. David Watson says:

    Thanks so much! I’ve been searching the net for days trying to find a way to get psycopg2 working on snow leopard. The only trouble I had was that the bash bracket test lines didn’t work so I just did the file moves by hand. Not sure what’s wrong there, but the rest of it worked like a charm. So happy and productive now. Thank you.

  2. Rune Bromer says:

    What shell are you using? I get a syntax like error when I try to run: [ -f "ld.sh" -a -n -f "files/ld.sh" ] && sudo mv ld.sh files/

    It fails with the error message [: too many arguments.

    Also, unless I have the hole permission things wrong you need a sudo in front of the “mkdir files”.

  3. @Rune, Hm, I was either using bash or zsh at the time (probably bash). I’ll correct it by removing the checks, chances are they’re not needed anyway.

    Thanks.

  4. Adam Nelson says:

    Make sure you have (at least for me) 10.4 development tools turned on when installing Xcode. Otherwise I got errors like:

    ld: library not found for -lbundle1.o collect2: ld returned 1 exit status error: command ‘gcc-4.0′ failed with exit status 1

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">