Redrock Postgres Documentation
Home Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

The psycopg module

Psycopg implements the Python Database DB API 2.0 specification. As such it also exposes the module-level objects required by the specifications.

psycopg.connect()

psycopg.connect(conninfo='', *, autocommit=False, prepare_threshold=5, row_factory=None, cursor_factory=None, context=None, **kwargs)

Connect to a database server and return a new Connection instance.

RETURN TYPE:

Connection[Any]

This is an alias of the class method Connection.connect: see its documentation for details.

If you need an asynchronous connection use AsyncConnection.connect instead.

Exceptions

The standard DBAPI exceptions are exposed both by the !psycopg module and by the psycopg.errors module. The latter also exposes more specific exceptions, mapping to the database error states (see SQLSTATE exceptions).

!Exception
|__ Warning
|__ Error
    |__ InterfaceError
    |__ DatabaseError
        |__ DataError
        |__ OperationalError
        |__ IntegrityError
        |__ InternalError
        |__ ProgrammingError
        |__ NotSupportedError

psycopg.adapters

psycopg.adapters

The default adapters map establishing how Python and PostgreSQL types are converted into each other.

This map is used as a template when new connections are created, using psycopg.connect(). Its ~psycopg.adapt.AdaptersMap.types attribute is a ~psycopg.types.TypesRegistry containing information about every PostgreSQL builtin type, useful for adaptation customisation (see Data adaptation configuration):

>>> psycopg.adapters.types["int4"]
<TypeInfo: int4 (oid: 23, array oid: 1007)>

TYPE:

~psycopg.adapt.AdaptersMap