Psycopg¶
PostgreSQL adapter using psycopg 3 with both sync and async support. Features native pipeline mode for multi-statement batching.
Sync Configuration¶
- class sqlspec.adapters.psycopg.PsycopgSyncConfig[source]¶
Bases:
SyncDatabaseConfig[Connection,ConnectionPool,PsycopgSyncDriver]Configuration for Psycopg synchronous database connections with direct field-based configuration.
- driver_type¶
alias of
PsycopgSyncDriver
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, **kwargs)[source]¶
Initialize Psycopg synchronous configuration.
- Parameters:
connection_config¶ (
PsycopgPoolParams|dict[str, typing.Any] |None) – Connection and pool configuration parameters (TypedDict or dict)connection_instance¶ (
ConnectionPool|None) – Existing pool instance to usemigration_config¶ (
dict[str, typing.Any] |None) – Migration configurationstatement_config¶ (
StatementConfig|None) – Default SQL statement configurationdriver_features¶ (
PsycopgDriverFeatures|dict[str, typing.Any] |None) – Optional driver feature configurationbind_key¶ (
str|None) – Optional unique identifier for this configurationextension_config¶ (
dict[str,dict[str,Any] |LitestarConfig|FastAPIConfig|StarletteConfig|FlaskConfig|ADKConfig|EventsConfig|OpenTelemetryConfig|PrometheusConfig] |None) – Extension-specific configuration (e.g., Litestar plugin settings)
- create_connection()[source]¶
Create a single connection (not from pool).
- Return type:
Connection- Returns:
A psycopg Connection instance.
- provide_session(*_args, statement_config=None, **_kwargs)[source]¶
Provide a driver session context manager.
- provide_pool(*args, **kwargs)[source]¶
Provide pool instance.
- Return type:
ConnectionPool- Returns:
The connection pool.
- get_signature_namespace()[source]¶
Get the signature namespace for Psycopg types.
This provides all Psycopg-specific types that Litestar needs to recognize to avoid serialization attempts.
Async Configuration¶
- class sqlspec.adapters.psycopg.PsycopgAsyncConfig[source]¶
Bases:
AsyncDatabaseConfig[AsyncConnection,AsyncConnectionPool,PsycopgAsyncDriver]Configuration for Psycopg asynchronous database connections with direct field-based configuration.
- driver_type¶
alias of
PsycopgAsyncDriver
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, **kwargs)[source]¶
Initialize Psycopg asynchronous configuration.
- Parameters:
connection_config¶ (
PsycopgPoolParams|dict[str, typing.Any] |None) – Connection and pool configuration parameters (TypedDict or dict)connection_instance¶ (
AsyncConnectionPool|None) – Existing pool instance to usemigration_config¶ (
dict[str, typing.Any] |None) – Migration configurationstatement_config¶ (
StatementConfig|None) – Default SQL statement configurationdriver_features¶ (
PsycopgDriverFeatures|dict[str, typing.Any] |None) – Optional driver feature configurationbind_key¶ (
str|None) – Optional unique identifier for this configurationextension_config¶ (
dict[str,dict[str,Any] |LitestarConfig|FastAPIConfig|StarletteConfig|FlaskConfig|ADKConfig|EventsConfig|OpenTelemetryConfig|PrometheusConfig] |None) – Extension-specific configuration (e.g., Litestar plugin settings)
- async create_connection()[source]¶
Create a single async connection (not from pool).
- Return type:
AsyncConnection- Returns:
A psycopg AsyncConnection instance.
- provide_session(*_args, statement_config=None, **_kwargs)[source]¶
Provide an async driver session context manager.
- async provide_pool(*args, **kwargs)[source]¶
Provide async pool instance.
- Return type:
AsyncConnectionPool- Returns:
The async connection pool.
Sync Driver¶
- class sqlspec.adapters.psycopg.PsycopgSyncDriver[source]¶
Bases:
PsycopgPipelineMixin,SyncDriverAdapterBasePostgreSQL psycopg synchronous driver.
Provides synchronous database operations for PostgreSQL using psycopg3. Supports SQL statement execution with parameter binding, transaction management, result processing with column metadata, parameter style conversion, PostgreSQL arrays and JSON handling, COPY operations for bulk data transfer, and PostgreSQL-specific error handling.
- dispatch_special_handling(cursor, statement)[source]¶
Hook for PostgreSQL-specific special operations.
- with_cursor(connection)[source]¶
Create context manager for PostgreSQL cursor.
- Return type:
PsycopgSyncCursor
- handle_database_exceptions()[source]¶
Handle database-specific exceptions and wrap them appropriately.
- Return type:
PsycopgSyncExceptionHandler
- execute_stack(stack, *, continue_on_error=False)[source]¶
Execute a StatementStack using psycopg pipeline mode when supported.
- Return type:
- select_to_storage(statement, destination, /, *parameters, statement_config=None, partitioner=None, format_hint=None, telemetry=None, **kwargs)[source]¶
Execute a query and stream Arrow results to storage (sync).
- Return type:
- load_from_arrow(table, source, *, partitioner=None, overwrite=False, telemetry=None)[source]¶
Load Arrow data into PostgreSQL using COPY.
- load_from_storage(table, source, *, file_format, partitioner=None, overwrite=False)[source]¶
Load staged artifacts into PostgreSQL via COPY.
- Return type:
- property data_dictionary: PsycopgSyncDataDictionary¶
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
Async Driver¶
- class sqlspec.adapters.psycopg.PsycopgAsyncDriver[source]¶
Bases:
PsycopgPipelineMixin,AsyncDriverAdapterBasePostgreSQL psycopg asynchronous driver.
Provides asynchronous database operations for PostgreSQL using psycopg3. Supports async SQL statement execution with parameter binding, async transaction management, async result processing with column metadata, parameter style conversion, PostgreSQL arrays and JSON handling, COPY operations for bulk data transfer, PostgreSQL-specific error handling, and async pub/sub support.
- async dispatch_execute_many(cursor, statement)[source]¶
Execute SQL with multiple parameter sets (async).
- async dispatch_execute_script(cursor, statement)[source]¶
Execute SQL script with multiple statements (async).
- async dispatch_special_handling(cursor, statement)[source]¶
Hook for PostgreSQL-specific special operations.
- with_cursor(connection)[source]¶
Create async context manager for PostgreSQL cursor.
- Return type:
PsycopgAsyncCursor
- handle_database_exceptions()[source]¶
Handle database-specific exceptions and wrap them appropriately.
- Return type:
PsycopgAsyncExceptionHandler
- async execute_stack(stack, *, continue_on_error=False)[source]¶
Execute a StatementStack using psycopg async pipeline when supported.
- Return type:
- async select_to_storage(statement, destination, /, *parameters, statement_config=None, partitioner=None, format_hint=None, telemetry=None, **kwargs)[source]¶
Execute a query and stream Arrow data to storage asynchronously.
- Return type:
- async load_from_arrow(table, source, *, partitioner=None, overwrite=False, telemetry=None)[source]¶
Load Arrow data into PostgreSQL asynchronously via COPY.
- async load_from_storage(table, source, *, file_format, partitioner=None, overwrite=False)[source]¶
Load staged artifacts asynchronously.
- Return type:
- property data_dictionary: PsycopgAsyncDataDictionary¶
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
Extension Dialects¶
Psycopg supports the pgvector and ParadeDB dialects for vector similarity search and full-text search operators. See the Dialects reference for operator details.
Data Dictionary¶
- class sqlspec.adapters.psycopg.data_dictionary.PsycopgSyncDataDictionary[source]¶
Bases:
SyncDataDictionaryBasePostgreSQL-specific sync data dictionary.
-
dialect:
ClassVar[str] = 'postgres'¶ Dialect identifier. Must be defined by subclasses as a class attribute.
- get_version(driver)[source]¶
Get PostgreSQL database version information.
- Parameters:
driver¶ (
PsycopgSyncDriver) – Sync database driver instance.- Return type:
- Returns:
PostgreSQL version information or None if detection fails.
- get_feature_flag(driver, feature)[source]¶
Check if PostgreSQL database supports a specific feature.
- Parameters:
driver¶ (
PsycopgSyncDriver) – Sync database driver instance.
- Return type:
- Returns:
True if feature is supported, False otherwise.
- get_optimal_type(driver, type_category)[source]¶
Get optimal PostgreSQL type for a category.
- Parameters:
driver¶ (
PsycopgSyncDriver) – Sync database driver instance.
- Return type:
- Returns:
PostgreSQL-specific type name.
- get_tables(driver, schema=None)[source]¶
Get tables sorted by topological dependency order using Recursive CTE.
- Return type:
- get_columns(driver, table=None, schema=None)[source]¶
Get column information for a table or schema.
- Return type:
- get_indexes(driver, table=None, schema=None)[source]¶
Get index metadata for a table or schema.
- Return type:
-
dialect:
- class sqlspec.adapters.psycopg.data_dictionary.PsycopgAsyncDataDictionary[source]¶
Bases:
AsyncDataDictionaryBasePostgreSQL-specific async data dictionary.
-
dialect:
ClassVar[str] = 'postgres'¶ Dialect identifier. Must be defined by subclasses as a class attribute.
- async get_version(driver)[source]¶
Get PostgreSQL database version information.
- Parameters:
driver¶ (
PsycopgAsyncDriver) – Async database driver instance.- Return type:
- Returns:
PostgreSQL version information or None if detection fails.
- async get_feature_flag(driver, feature)[source]¶
Check if PostgreSQL database supports a specific feature.
- Parameters:
driver¶ (
PsycopgAsyncDriver) – Async database driver instance.
- Return type:
- Returns:
True if feature is supported, False otherwise.
- async get_optimal_type(driver, type_category)[source]¶
Get optimal PostgreSQL type for a category.
- Parameters:
driver¶ (
PsycopgAsyncDriver) – Async database driver instance.
- Return type:
- Returns:
PostgreSQL-specific type name.
- async get_tables(driver, schema=None)[source]¶
Get tables sorted by topological dependency order using Recursive CTE.
- Return type:
- async get_columns(driver, table=None, schema=None)[source]¶
Get column information for a table or schema.
- Return type:
- async get_indexes(driver, table=None, schema=None)[source]¶
Get index metadata for a table or schema.
- Return type:
-
dialect: