Wednesday, October 21, 2009

Trials and Tribulations: pyscopg2

So here's my latest debacle that caused me to waste hours of my time trying to find a solution. Let me set the stage:

Actors:
Virtual machine with RedHat ES 4.7
Remote Postgres database

Intent:
Connect to my remote database and list out the contents of a simple table

Sounds easy? I thought it would be but...

I downloaded the latest pyscopg stuff from the site

wget http://initd.org/pub/software/psycopg/PSYCOPG-2-0/psycopg2-2.0.11.tar.gz

and then tried to install and I then get these messages:


python setup.py build
running build
running build_py
running build_ext
error: No such file or directory

After googling with browsers filled to the brim with a plethora of tabs it seems as if I may be missing the development libraries for the package; fair enough lets get those packages.

The version of Redhat that I have installed on the machine seems to be able to support Postgres 7.4 rather than 8.1 so I get the RPMs to support the entire lot.
I would have upgraded to 8.1 just to be current but I would have to install tons of development pakcages to support 8.1 and it just wasn't worth it.


Try to install postgresql-devel-????.rpm and it will point out what other dependencies it needs.

Success! The package compiles and installs. The sun is shining once again and I can almost hear the cherubs singing my praise but lo, a dissenting voice is heard...

Writing the script makes it painfully obvious that we're not done yet.


Importing psycopg from within a Python script, it gives us the error about PQserverVersion not being defined.

After much digging it seems as if the 8.1 libraries (libPQ.so) have the above mentioned function as a new addition and they've also changed the lo_create function to lo_creat so after a little patching...

3. Patch the connection_int.c
change the line:
self->server_version = (int)PQserverVersion(pgconn);
to:
self-server_version = '70401';
4. Patch the lobject_int.c
change the line:
self->oid = lo_create(self->conn->pgconn, new_oid);
to:
self->oid = lo_creat(self->conn->pgconn, new_oid);

After those changes are made and I reinstalled the package, I could import the package with no errors.

I was able to connect to my table at the end of the day but what a path I had to take to get there.

Here's so you don't have to do this bullshit...

Cheers!