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.

close – close the connection

Connection.close()

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

Return type:None

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

Connection.commit()

Commit any pending transaction to the database

Return type:None

Note that connections always use a transaction, there is no auto-commit.

rollback – roll back the connection

Connection.rollback()

Roll back any pending transaction to the database

Return type:None

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

Connection.cursor()

Return a new cursor object using the connection

Returns:a connection object
Return type:Cursor

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

Note

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

Connection.closed

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

Connection.cursor_type

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.

Connection.type_cache

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.