↑ P(119): b'P\x00\x00\x00v\x00DECLARE "py:0x1191c10" INSENSITIVE SCROLL CURSOR WITH HOLD FOR select i from generate_series(0,100000) as g(i)\x00\x00\x00'
↑ B(13): b'B\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00'
↑ E(10): b'E\x00\x00\x00\t\x00\x00\x00\x00\x01'
↑ S(5): b'S\x00\x00\x00\x04'
↓ b'1'(0): b''
↓ b'2'(0): b''
↓ b'C'(15): b'DECLARE CURSOR\x00'
↓ b'Z'(1): b'I'
↑ P(43): b'P\x00\x00\x00*\x00FETCH FORWARD 10 IN "py:0x1191c10"\x00\x00\x00'
↑ B(15): b'B\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01'
↑ E(10): b'E\x00\x00\x00\t\x00\xff\xff\xff\xff'
↑ S(5): b'S\x00\x00\x00\x04'
↓ b'1'(0): b''
↓ b'2'(0): b''
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x00'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x01'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x02'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x03'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x04'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x05'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x06'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x07'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\x08'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x00\x00\t'
↓ b'C'(9): b'FETCH 10\x00'
↓ b'Z'(1): b'I'
↑ P(37): b'P\x00\x00\x00$\x00MOVE LAST  IN "py:0x1191c10"\x00\x00\x00'
↑ B(13): b'B\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00'
↑ E(10): b'E\x00\x00\x00\t\x00\x00\x00\x00\x01'
↑ P(37): b'P\x00\x00\x00$\x00MOVE NEXT  IN "py:0x1191c10"\x00\x00\x00'
↑ B(13): b'B\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00'
↑ E(10): b'E\x00\x00\x00\t\x00\x00\x00\x00\x01'
↑ P(42): b'P\x00\x00\x00)\x00MOVE BACKWARD 0 IN "py:0x1191c10"\x00\x00\x00'
↑ B(13): b'B\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00'
↑ E(10): b'E\x00\x00\x00\t\x00\x00\x00\x00\x01'
↑ S(5): b'S\x00\x00\x00\x04'
↓ b'1'(0): b''
↓ b'2'(0): b''
↓ b'C'(7): b'MOVE 1\x00'
↓ b'1'(0): b''
↓ b'2'(0): b''
↓ b'C'(7): b'MOVE 0\x00'
↓ b'1'(0): b''
↓ b'2'(0): b''
↓ b'C'(7): b'MOVE 0\x00'
↓ b'Z'(1): b'I'
↑ P(44): b'P\x00\x00\x00+\x00FETCH BACKWARD 10 IN "py:0x1191c10"\x00\x00\x00'
↑ B(15): b'B\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01'
↑ E(10): b'E\x00\x00\x00\t\x00\xff\xff\xff\xff'
↑ S(5): b'S\x00\x00\x00\x04'
↓ b'1'(0): b''
↓ b'2'(0): b''
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\xa0'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x9f'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x9e'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x9d'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x9c'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x9b'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x9a'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x99'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x98'
↓ b'D'(10): b'\x00\x01\x00\x00\x00\x04\x00\x01\x86\x97'
↓ b'C'(9): b'FETCH 10\x00'
↓ b'Z'(1): b'I'
↑ C(19): b'C\x00\x00\x00\x12Ppy:0x1191c10\x00'
↑ S(5): b'S\x00\x00\x00\x04'
↓ b'3'(0): b''
↓ b'Z'(1): b'I'
↑ X(5): b'X\x00\x00\x00\x04'
Releases
v1.1
v1.1.0
Zip File
v1.0
v1.0.4
Zip File
v1.0.3
Zip File
Documentation
v1.1.0 [2012-10-01]
v1.0.4 [2012-03-01]
v0.9.3 [2010-01-01]
v0.8.2 [2009-06-13]
Connect
PG-API:
>>> import postgresql
>>> db = postgresql.open(user = 'usename', database = 'datname', port = 5432)
Or, if DB-API 2.0 is the target:
>>> import postgresql.driver.dbapi20 as dbapi
>>> db = dbapi.connect(user = 'usename', database = 'datname', port = 5432, password = 'secret')
The Driver documentation has more information on acceptable keyword arguments.
COPY
COPY "FROM STDIN" and "TO STDOUT" is directly supported using the chunking interfaces. This is allows for connection-to-connection copies:
>>> send = src.prepare("COPY a_table TO STDOUT")
>>> recv = dst.prepare("COPY a_table FROM STDIN")
>>> recv.load_chunks(send.chunks())
         
Syntax Errors
py-postgresql's driver provides additional "element tracebacks" along with the printed exception. This gives significant contextual information that may help reduce debugging time:
>>> prepare('select 1\nfrom 1d')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "postgresql/driver/pq3.py", line 1718, in prepare
    ps._fini()
  File "postgresql/driver/pq3.py", line 857, in _fini
    self.database._pq_complete()
  File "postgresql/driver/pq3.py", line 1973, in _pq_complete
    self._raise_pq_error(x)
  File "postgresql/driver/pq3.py", line 1999, in _raise_pq_error
    raise err
postgresql.exceptions.SyntaxError: syntax error at or near "1"
  CODE: 42601
  LOCATION: File 'scan.l', line 807, in base_yyerror from SERVER
  POSITION: 15
STATEMENT: [parsing]
  LINE:
    from 1d
         ^ [line 2, character 6]
  statement_id: py:0x10fbc08
  string:
    select 1
    from 1d
CONNECTION: [idle]
  client_address: ::1
  client_port: 52915
  version:
    PostgreSQL 8.3.6 on i386-apple-darwin, compiled by GCC i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5370)
CONNECTOR: [Host] pq://jwp:***@localhost:5432
  category: None
DRIVER: postgresql.driver.pq3.Driver
Composites
Composites are supported:
>>> db.execute('CREATE TYPE ctyp AS (i int, t text, ts timestamp);')
>>> r = db.prepare("select (901, 'string', now())::ctyp").first()
>>> str(r)
"(901, 'string', datetime.datetime(2009, 5, 12, 20, 23, 30, 351411))"
>>> r[0]
901
>>> r['i']
901
>>> r['ts']
datetime.datetime(2009, 5, 12, 20, 23, 30, 351411)
Credits
Copy of the latest authors file:

Contributors:
 James William Pye [faults are mostly mine] <x@jwp.io>
 Elvis Pranskevichus
 William Grzybowski [subjective paramstyle]
 Barry Grussling [inet/cidr support]
 Matthew Grant [inet/cidr support]

Support by Donation:
 AppCove Network

Further Credits
===============

When licenses match, people win. Code is occasionally imported from other
projects to enhance py-postgresql and to allow users to enjoy dependency free
installation.


DB-API 2.0 Test Case
--------------------

postgresql/test/test_dbapi20.py:
 Stuart Bishop <zen@shangri-la.dropbear.id.au>


fcrypt
------

postgresql/resolved/crypt.py:
 Carey Evans <careye@spamcop.net>

py-postgresql
connect to PostgreSQL with Python 3
py-postgresql provides a driver, cluster management tools, and client development tools.
sample features
Pure Python All code, at first, is written in pure Python so that py-postgresql will work anywhere that you can install Python 3. Optimizations in C are made where needed, but are always optional.
Prepared Statements Using the PG-API interface, protocol-level prepared statements may be created and used multiple times. db.prepare(sql)(*args)
COPY Support Use the convenient COPY interface to directly copy data from one connection to another. No intermediate files or tricks are necessary.
Arrays and Composite Types Arrays and composites are fully supported. Queries requesting them will returns objects that provide access to the elements within.
"pg_python" Quick Console Get a Python console with a connection to PostgreSQL for quick tests and simple scripts.