API Reference¶
Complete API reference for all pycopg classes and methods.
Config¶
Configuration for database connections.
from pycopg import Config
Constructor¶
Config(
host: str = "localhost",
port: int = 5432,
database: str = "postgres",
user: str = "postgres",
password: str = "",
sslmode: Optional[str] = None,
options: dict = {}
)
Class Methods¶
Method |
Description |
|---|---|
|
Create Config from PostgreSQL URL |
|
Create Config from environment variables |
from_env Parameters¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Path to .env file |
|
|
|
Whether to load .env file. Set |
Properties¶
Property |
Type |
Description |
|---|---|---|
|
|
psycopg-compatible DSN string |
|
|
SQLAlchemy-compatible URL |
|
|
Statement timeout in milliseconds (None = no limit) |
Methods¶
Method |
Description |
|---|---|
|
Return dict for psycopg.connect() |
|
Create new Config with different database |
Database¶
Synchronous database interface.
from pycopg import Database
Constructor¶
Database(config: Config)
Class Methods¶
Method |
Description |
|---|---|
|
Create from PostgreSQL URL |
|
Create from environment |
|
Create a new database and connect to it |
|
Create database using env credentials |
Query Methods¶
Method |
Parameters |
Returns |
Description |
|---|---|---|---|
|
|
|
Execute SQL, return results |
|
|
|
Execute for multiple params |
|
|
|
Fetch single row |
|
|
|
Fetch single value |
Context Managers¶
Method |
Parameters |
Yields |
Description |
|---|---|---|---|
|
|
|
Connection context |
|
|
|
Cursor with dict rows |
Schema Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
- |
|
|
|
|
|
|
- |
|
|
- |
Table Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- |
|
|
- |
Extension Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
- |
|
|
|
|
|
|
- |
|
|
- |
Index Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
|
Constraint Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
- |
|
|
|
DataFrame Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
|
|
|
- |
|
|
|
PostGIS Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
|
TimescaleDB Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
- |
|
|
- |
|
- |
|
|
|
|
Role Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
|
|
|
|
|
|
- |
|
|
- |
|
|
- |
|
|
- |
|
|
- |
|
|
|
|
|
|
Backup Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
|
|
|
|
Database Admin Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
|
|
- |
|
Size/Stats Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
|
|
|
|
|
|
|
Maintenance Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
|
- |
|
|
- |
|
|
|
AsyncDatabase¶
Asynchronous database interface with full parity to Database.
from pycopg import AsyncDatabase
Full Async Parity (v0.3.0): AsyncDatabase provides all the same methods as Database with async/await. All methods listed in the Database section above (query, schema, table, DataFrame, PostGIS, TimescaleDB, role, backup, admin, maintenance, and size methods) are available asynchronously.
Async-Only Methods¶
These methods are only available on AsyncDatabase and have no sync equivalent:
Method |
Parameters |
Returns |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- |
Async Context Managers¶
Method |
Parameters |
Yields |
|---|---|---|
|
|
|
|
|
|
|
- |
|
PooledDatabase¶
Synchronous connection pool.
from pycopg import PooledDatabase
Constructor¶
PooledDatabase(
config: Config,
min_size: int = 2,
max_size: int = 10,
max_idle: float = 300.0,
max_lifetime: float = 3600.0,
timeout: float = 30.0,
num_workers: int = 3,
)
Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
- |
|
|
|
|
|
|
|
|
|
- |
|
- |
- |
|
|
- |
|
- |
- |
Properties¶
Property |
Type |
Description |
|---|---|---|
|
|
Pool statistics |
AsyncPooledDatabase¶
Asynchronous connection pool.
from pycopg import AsyncPooledDatabase
Constructor¶
Same as PooledDatabase.
Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
- |
|
|
- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- |
|
|
|
- |
|
- |
|
|
- |
|
Migrator¶
SQL migration manager.
from pycopg import Migrator
Constructor¶
Migrator(
db: Database,
migrations_dir: Union[str, Path],
table: str = "schema_migrations",
)
Methods¶
Method |
Parameters |
Returns |
|---|---|---|
|
- |
|
|
- |
|
|
- |
|
|
|
|
|
|
|
|
|
|
Exceptions¶
from pycopg import (
PycopgError, # Base exception
ConnectionError, # Connection failed
ConfigurationError, # Bad config
ExtensionNotAvailable, # Missing extension
TableNotFound, # Table doesn't exist
InvalidIdentifier, # SQL injection attempt
MigrationError, # Migration failed
)