I’ve just installed psychopg2, the new PostregreSQL driver for Python. For some reason the bundled setup.cfg has debugging turned on, so needs editting before compilation.

It’s syntax seems much the same as version one:

#!/usr/bin/env python

# import module
import psycopg2

# connect to database
dbconn = psycopg2.connect("host=localhost \
                            dbname=pgtest \
                            user=postgres \
                            password=password")

# create cursor
dbcur = dbconn.cursor()

# execute query
dbcur.execute("SELECT * FROM t_contacts")

# output results as list
print dbcur.fetchall()