Anyone know a good Pygresql Tutorial for Interfacing between Python &Postgresql

Peter Maas peter.maas at
Thu Jun 17 09:25:24 CEST 2004

John fabiani schrieb:
> I'm a newbie so take what I have to say with a grain of salt.  The 
> problem for me was not how to make a connection to postgres but how to 
> use the data returned. 

Here is a tested example code. It relies on pyPgSQL but PyGreSQL
should be quite similar especially the data access:

#!/usr/bin/env python
# -*- coding: latin-1 -*-

Example code how to read data from a PostgreSQL database. You
need the pyPgSQL module which is DB-API 2.0 compliant so that
the calls are not database dependent except of connection URL
and some SQL capabilities.

# PostgreSQL interface module
from pyPgSQL import PgSQL

if __name__ == '__main__':
     # open connection
     con = PgSQL.connect(None, "aUser", "aPasswd", "aHost", "aDatabase")

     # create cursor
     c_adr = con.cursor()

     # let cursor execute an SQL command
     c_adr.execute("SELECT * FROM address")

     # fetch a result set
     r_adr = c_adr.fetchmany(10)

     # The result set is a list of records.
     print r_adr[0]

     #  Each record is a dictionary like object with field names as keys.
     print r_adr[0].keys()

     # The field values are the dictionary values.
     print r_adr[0]["firstname"]

     # print all records
     for record in r_adr:
         print record

Mit freundlichen Gruessen,

Peter Maas

Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas at

More information about the Python-list mailing list