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(conninfo: str = '', *, autocommit: bool = False, prepare_threshold: int | None = 5, context: Optional[AdaptContext] = None, row_factory: Optional[RowFactory[Row]] = None, cursor_factory: Cursor[+Row]]] = None, **kwargs: str | int | None) Self#

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

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.

psycopg.capabilities#

An object that can be used to verify that the client library used by psycopg implements a certain feature. For instance:

# Fail at import time if encrypted passwords is not available
import psycopg
psycopg.capabilities.has_encrypt_password(check=True)

# Verify at runtime if a feature can be used
if psycopg.capabilities.has_hostaddr():
    print(conn.info.hostaddr)
else:
    print("unknown connection hostadd")
Type:

Capabilities

New in version 3.2.

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#

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 types attribute is a 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:

AdaptersMap