crdb – CockroachDB support#
New in version 3.1.
CockroachDB is a distributed database using the same fronted-backend protocol of PostgreSQL. As such, Psycopg can be used to write Python programs interacting with CockroachDB.
Opening a connection to a CRDB database using
psycopg.connect() provides a
largely working object. However, using the
instead, Psycopg will create more specialised objects and provide a types
mapping tweaked on the CockroachDB data model.
Main differences from PostgreSQL#
CockroachDB behaviour is different from PostgreSQL: please refer to the database documentation for details. These are some of the main differences affecting Psycopg behaviour:
Server-side cursors are well supported only from CockroachDB 22.1.3.
backend_pidis only populated from CockroachDB 22.1. Note however that you cannot use the PID to terminate the session; use SHOW session_id to find the id of a session, which you may terminate with CANCEL SESSION in lieu of PostgreSQL’s
Several data types are missing or slightly different from PostgreSQL (see
adaptersfor an overview of the differences).
The two-phase commit protocol is not supported.
- psycopg.crdb.connect(conninfo: str = '', **kwargs: Any) CrdbConnection[Any] #
Connect to a database server and return a new
This is an alias of the class method
If you need an asynchronous connection use the
- class psycopg.crdb.CrdbConnection(pgconn: PGconn, row_factory: ~psycopg.rows.RowFactory[~psycopg.rows.Row] = <function tuple_row>)#
Wrapper for a connection to a CockroachDB database.
- class psycopg.crdb.AsyncCrdbConnection(pgconn: PGconn, row_factory: ~psycopg.rows.AsyncRowFactory[~psycopg.rows.Row] = <function tuple_row>)#
Wrapper for an async connection to a CockroachDB database.
- class psycopg.crdb.CrdbConnectionInfo(pgconn: PGconn)#
ConnectionInfosubclass to get info about a CockroachDB database.
The object behaves like
ConnectionInfo, with the following differences:
Return the CockroachDB server version connected.
Return a number in the PostgreSQL format (e.g. 21.2.10 -> 210210).
The default adapters map establishing how Python and CockroachDB types are converted into each other.
This registry contains only the types and adapters supported by CockroachDB. Several PostgreSQL types and adapters are missing or different from PostgreSQL, among which:
jsontype is an alias for
inttype is an alias for