The module interface respects the standard defined in the DB API 2.0.
Create a new database session and return a new connection object.
You can specify the connection parameters either as a string:
conn = psycopg2.connect("dbname=test user=postgres password=secret")
or using a set of keyword arguments:
conn = psycopg2.connect(database="test", user="postgres", password="secret")
The full list of available parameters is:
Using the connection_factory parameter a different class or connections factory can be specified. It should be a callable object taking a dsn argument. See Connection and cursor factories for details.
DB API extension
The connection_factory parameter is a Psycopg extension to the DB API 2.0.
In compliance with the DB API 2.0, the module makes informations about errors available through the following exceptions:
Exception that is the base class of all other error exceptions. You can use this to catch all errors with one single except statement. Warnings are not considered errors and thus not use this class as base. It is a subclass of the Python StandardError.
DB API extension
The pgerror and pgcode attributes are Psycopg extensions.
>>> try:
... cur.execute("SELECT * FROM barf")
... except Exception, e:
... pass
>>> e.pgcode
'42P01'
>>> print e.pgerror
ERROR: relation "barf" does not exist
LINE 1: SELECT * FROM barf
^
Changed in version 2.0.7: added Error.pgerror and Error.pgcode attributes.
DB API extension
Psycopg may raise a few other, more specialized, exceptions: currently QueryCanceledError and TransactionRollbackError are defined. These exceptions are not exposed by the main psycopg2 module but are made available by the extensions module. All the additional exceptions are subclasses of standard DB API 2.0 exceptions, so trapping them specifically is not required.
This is the exception inheritance layout:
StandardError |__ Warning |__ Error |__ InterfaceError |__ DatabaseError |__ DataError |__ OperationalError | |__ psycopg2.extensions.QueryCanceledError | |__ psycopg2.extensions.TransactionRollbackError |__ IntegrityError |__ InternalError |__ ProgrammingError |__ NotSupportedError
Note
This section is mostly copied verbatim from the DB API 2.0 specification. While these objects are exposed in compliance to the DB API, Psycopg offers very accurate tools to convert data between Python and PostgreSQL formats. See Adapting new Python types to SQL syntax and Type casting of SQL types into Python objects
Many databases need to have the input in a particular format for binding to an operation’s input parameters. For example, if an input is destined for a DATE column, then it must be bound to the database in a particular string format. Similar problems exist for “Row ID” columns or large binary items (e.g. blobs or RAW columns). This presents problems for Python since the parameters to the .execute*() method are untyped. When the database module sees a Python string object, it doesn’t know if it should be bound as a simple CHAR column, as a raw BINARY item, or as a DATE.
To overcome this problem, a module must provide the constructors defined below to create objects that can hold special values. When passed to the cursor methods, the module can then detect the proper type of the input parameter and bind it accordingly.
A Cursor Object’s description attribute returns information about each of the result columns of a query. The type_code must compare equal to one of Type Objects defined below. Type Objects may be equal to more than one type code (e.g. DATETIME could be equal to the type codes for date, time and timestamp columns; see the Implementation Hints below for details).
The module exports the following constructors and singletons: