Psycopg#
PostgreSQL adapter using psycopg 3 with both sync and async support. Features native pipeline mode for multi-statement batching.
Cloud Connectivity#
PsycopgSyncConfig can create sync pools through the Google AlloyDB in-process
connector when both psycopg and google-cloud-alloydb-connector are installed:
from sqlspec.adapters.psycopg import PsycopgSyncConfig
config = PsycopgSyncConfig(
connection_config={"user": "app", "password": "secret", "dbname": "orders"},
driver_features={
"enable_alloydb": True,
"alloydb_instance_uri": "projects/project/locations/us-central1/clusters/cluster/instances/instance",
},
)
This connector path is sync-only. PsycopgAsyncConfig continues to use regular
psycopg async connection configuration.
Sync Configuration#
Async Configuration#
Connection Parameters#
Pool Parameters#
Driver Features#
Sync Driver#
Async Driver#
Extension Dialects#
Psycopg supports the pgvector, pg_textsearch, 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_metadata_capabilities(driver, domains=None)[source]#
Get PostgreSQL data-dictionary capability profile.
- Return type:
- get_system_metadata_capabilities(driver, domains=None)[source]#
Get PostgreSQL opt-in system metadata capability disclosures.
- get_dependencies(driver, object_name=None, schema=None)[source]#
Get dependency metadata.
- Return type:
- get_ddl(driver, object_name, schema=None, *, object_type='table', include_dependencies=True, prefer_native=True, redact=True)[source]#
Get object DDL where PostgreSQL exposes native definition helpers.
- Return type:
DDLResult
- get_system_metadata(driver, request=None, **kwargs)[source]#
Get opt-in PostgreSQL system metadata.
- Return type:
SystemMetadataResult
- get_version(driver)[source]#
Get PostgreSQL database version information.
Uses an inline cache check to avoid cross-module method calls that can trigger MyPyC boundary issues.
- 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.
- 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:
- 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_metadata_capabilities(driver, domains=None)[source]#
Get PostgreSQL data-dictionary capability profile.
- Return type:
- async get_system_metadata_capabilities(driver, domains=None)[source]#
Get PostgreSQL opt-in system metadata capability disclosures.
- async get_constraints(driver, table=None, schema=None)[source]#
Get constraint metadata.
- Return type:
- async get_privileges(driver, object_name=None, schema=None)[source]#
Get privilege metadata.
- Return type:
- async get_dependencies(driver, object_name=None, schema=None)[source]#
Get dependency metadata.
- Return type:
- async get_ddl(driver, object_name, schema=None, *, object_type='table', include_dependencies=True, prefer_native=True, redact=True)[source]#
Get object DDL where PostgreSQL exposes native definition helpers.
- Return type:
DDLResult
- async get_system_metadata(driver, request=None, **kwargs)[source]#
Get opt-in PostgreSQL system metadata.
- Return type:
SystemMetadataResult
- async get_version(driver)[source]#
Get PostgreSQL database version information.
Uses an inline cache check to avoid cross-module method calls that can trigger MyPyC boundary issues.
- 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.
- 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:
Extension Settings#
Use the configuration types below in their corresponding extension_config
namespace: "litestar", "events", or "adk" as supported by this adapter.
Native null pools and JSON codecs#
Set null_pool=True in connection_config or driver_features to select
psycopg's native null pool. Returned connections close instead of remaining idle.
max_size and max_waiting still control concurrency and waiting clients;
min_size is omitted because null pools do not maintain idle connections.
The same option works for sync and async configurations.
json_serializer and json_deserializer driver features also configure
psycopg's native JSON adaptation for each new connection. Codec setup errors
propagate to the caller.