Psycopg2, the Python PostgreSQL Database Adapter and Building from Source

Some problems arose when I built PostgreSQL from source to have the latest and greatest version (8.3.3 at the publication time of this article) and needed to use the headers for building Psycopg2 and other packages.

As CentOS 5 currently ships with PostgreSQL 8.1.x and I needed 8.3.3 I decided to build it from source and install it in /usr/local .  No problem there, the build and install went fine, and I adapted the init script in /etc/init.d to use the new 8.3.3 install so that it started up at boot time and so that it shutdown cleanly at reboot.  When it came to building and installing Psycopg2, the Python PostgreSQL Database Adapter, I ran into some problems.  The build process threw an error that said that it could not find libpq.so.5. I figured it was due to the fact that PostgreSQL was installed in a non-standard location, so I edited the setup.cfg, found the proper incantations to set the paths:

library_dirs=/usr/local/pgsql/lib/

include_dirs=/usr/local/pgsql/lib/

To the build command, I added the LDFLAGS option, just in case:

LDFLAGS='-L /usr/local/pgsql/lib/' python setup.py build

and I was on my way.  Both build and install went off without a hitch.

When I attempted to use Django's syncdb manouevre, however, I saw the same error, more or less.  The Django PostgreSQL database backend module could not find libpq.so.5.  I figured that my shell environment needed to know where the PostgreSQL headers were located, so I added the following to my .bashrc script:

export LD_LIBRARY_PATH=/usr/local/pgsql/lib:/usr/local/lib:/usr/lib

and that did the trick.  Django syncdb worked like a charm, and from the python command prompt I could import the Psycopg2 package:

>>> import psycopg2
>>>

Sweet.  But wait, what about Apache?  Sure enough, it did not know about the LD_LIBRARY_PATH value from my environment, so Django could not find the rogue libpq.so.5 file.  The version of Apache that we are using, 2.2.3, does not use the envvars configuration file like you see with 2.2.8, so I had to create the envvars file in /etc/httpd/conf with:

export LD_LIBRARY_PATH=/usr/local/pgsql/lib:/usr/local/lib:/usr/lib:$LD_LIBRARY_PATH

and then I added the following to the /etc/init.d/httpd service script:

if [ -f /etc/httpd/conf/envvars ]; then
    . /etc/httpd/conf/envvars
fi

now we're rollin.


1 Comment, 0 trackbacks (Trackback URL)

1 response to Psycopg2, the Python PostgreSQL Database Adapter and Building from Source

  1. Alexandre  March 4th, 2009 at 9:58 a.m.

    Thanks for the tips!

Leave a Comment
  1. (required)
  2. Ignore this field:
  3. Don't put anything in this field:
    Don't put anything here:
  4. Leave this empty:
    (required)
  5. Your email is not publically displayed.