psycopg2 - это надежная оболочка для PostgreSQL. Из документов:
DSN = 'dbname=test'
## don't modify anything below this line (except for experimenting)
class SimpleQuoter(object):
def sqlquote(x=None):
return "'bar'"
import sys
import psycopg2
if len(sys.argv) > 1:
DSN = sys.argv[1]
print("Opening connection using dsn:", DSN)
conn = psycopg2.connect(DSN)
print("Encoding for this connection is", conn.encoding)
curs = conn.cursor()
curs.execute("SELECT 1 AS foo")
print(curs.fetchone())
curs.execute("SELECT 1 AS foo")
print(curs.fetchmany())
curs.execute("SELECT 1 AS foo")
print(curs.fetchall())
conn.rollback()
sys.exit(0)
curs.execute("SELECT 1 AS foo", async=1)
curs.execute("SELECT %(foo)s AS foo", {'foo':'bar'})
curs.execute("SELECT %(foo)s AS foo", {'foo':None})
curs.execute("SELECT %(foo)f AS foo", {'foo':42})
curs.execute("SELECT %(foo)s AS foo", {'foo':SimpleQuoter()})