import pgdb cxn = pgdb.connect(user='pgsql') cur = cxn.cursor() cur.execute('SELECT * FROM pg_database') rows = cur.fetchall() for i in rows: print i cur.close() cxn.commit() cxn.close()