Connection – The connection object

class pgdb.Connection

These connection objects respond to the following methods.

Note that pgdb.Connection objects also implement the context manager protocol, i.e. you can use them in a with statement. When the with block ends, the current transaction will be automatically committed or rolled back if there was an exception, and you won’t need to do this manually.

close – close the connection


Close the connection now (rather than whenever it is deleted)

Return type


The connection will be unusable from this point forward; an Error (or subclass) exception will be raised if any operation is attempted with the connection. The same applies to all cursor objects trying to use the connection. Note that closing a connection without committing the changes first will cause an implicit rollback to be performed.

commit – commit the connection


Commit any pending transaction to the database

Return type


Note that connections always use a transaction, unless you set the Connection.autocommit attribute described below.

rollback – roll back the connection


Roll back any pending transaction to the database

Return type


This method causes the database to roll back to the start of any pending transaction. Closing a connection without committing the changes first will cause an implicit rollback to be performed.

cursor – return a new cursor object


Return a new cursor object using the connection


a connection object

Return type


This method returns a new Cursor object that can be used to operate on the database in the way described in the next section.

Attributes that are not part of the standard


The following attributes are not part of the DB-API 2 standard.


This is True if the connection has been closed or has become invalid


The default cursor type used by the connection

If you want to use your own custom subclass of the Cursor class with he connection, set this attribute to your custom cursor class. You will then get your custom cursor whenever you call Connection.cursor().

New in version 5.0.


A dictionary with the various type codes for the PostgreSQL types

This can be used for getting more information on the PostgreSQL database types or changing the typecast functions used for the connection. See the description of the TypeCache class for details.

New in version 5.0.


A read/write attribute to get/set the autocommit mode

Normally, all DB-API 2 SQL commands are run inside a transaction. Sometimes this behavior is not desired; there are also some SQL commands such as VACUUM which cannot be run inside a transaction.

By setting this attribute to True you can change this behavior so that no transactions will be started for that connection. In this case every executed SQL command has immediate effect on the database and you don’t need to call Connection.commit() explicitly. In this mode, you can still use with con: blocks to run parts of the code using the connection con inside a transaction.

By default, this attribute is set to False which conforms to the behavior specified by the DB-API 2 standard (manual commit required).

New in version 5.1.