PostgreSQL and Python 3

Project Home Page

http://python.projects.postgresql.org/

Install

Download the py-postgresql page from http://pgfoundry.org/projects/python/

Unpack py-postgresql-0.9.1.tar.gz in a directory such as $HOME/tools

cd into $HOME/tools/py-postgresql-0.9.1.tar.gz

Run the following:

$ python3 ./setup.py install

Error

On my Ubuntu machine the compile failed with:

postgresql/protocol/optimized/module.c:14:20: error: Python.h: No such file or directory...error: command 'gcc' failed with exit status 1

Fix

From http://python.projects.postgresql.org/docs/0.9/admin.html#installation

Under most POSIX systems, the above should work without problem if the proper Python executable is referenced. However, if it does fail, it is likely due to a C extension???s inability to compile.

$ env PY_BUILD_EXTENSIONS=0 python3 ./setup.py install

Which resulted in a permission problem on my machine.

So, just prefix with sudo

dsw:~/tools/py-postgresql-0.9.1$ sudo env PY_BUILD_EXTENSIONS=0 python3 ./setup.py install

All set...

Connection

>>> import postgresql as pg>>> db=pg.open("pg://localhost/database")

If you need a password

>>> db = postgresql.open("pq://username:password@localhost/database")