PostgreSQL driver and tools library providing PG-API and DB-API 2.0 interfaces for Python.
npx @tessl/cli install tessl/pypi-py-postgresql@1.3.0A comprehensive Python library for PostgreSQL database connectivity providing both modern PostgreSQL-specific PG-API and standard DB-API 2.0 interfaces. The package includes full protocol implementation, type system support, cluster management tools, and command-line utilities for robust PostgreSQL integration.
pip install py-postgresqlPrimary connection interface:
import postgresqlDB-API 2.0 interface:
import postgresql.driver.dbapi20 as dbapiType system and exceptions:
import postgresql.types
import postgresql.exceptionsimport postgresql
# Connect using pq:// URI format
db = postgresql.open('pq://user:password@host:port/database')
# Prepare and execute statements
get_users = db.prepare("SELECT id, name FROM users WHERE active = $1")
active_users = get_users(True)
# Transaction support
with db.xact():
insert_user = db.prepare("INSERT INTO users (name, email) VALUES ($1, $2)")
insert_user("John Doe", "john@example.com")
# Streaming results
for user in get_users.rows(True):
print(f"User: {user['name']}")
db.close()py-postgresql provides multiple interfaces for PostgreSQL connectivity:
Core database connection functionality supporting both high-level PG-API and standard DB-API 2.0 interfaces, with flexible connection parameters and authentication methods.
def open(iri=None, prompt_title=None, **kw): ...Prepared statement interface with parameter binding, result streaming, and transaction management for efficient and secure database operations.
class Statement:
def __call__(*parameters): ...
def rows(*parameters): ...
def first(*parameters): ...
def chunks(*parameters): ...Standard Python database interface providing cursor-based operations, connection management, and exception handling for compatibility with existing database applications.
def connect(user=None, password=None, host=None, port=None, database=None, **kw): ...
class Connection:
def cursor(): ...
def commit(): ...
def rollback(): ...
def close(): ...
class Cursor:
def execute(query, parameters=None): ...
def executemany(query, parameter_sequences): ...
def fetchone(): ...
def fetchmany(size=None): ...
def fetchall(): ...Comprehensive PostgreSQL type support including primitive types, arrays, composite types, and custom type conversion with automatic serialization and deserialization.
# Type constants
BOOLOID: int
INT2OID: int
INT4OID: int
INT8OID: int
TEXTOID: int
JSONBOID: int
# ... 60+ additional type OIDs
class Array:
def __init__(elements, element_type): ...
class Row:
def __getitem__(key): ...
def keys(): ...
def values(): ...Complete exception hierarchy mapping PostgreSQL error codes to specific Python exception classes with detailed error information and SQL state codes.
class Error(Exception): ...
class ConnectionError(Error): ...
class TransactionError(Error): ...
class AuthenticationSpecificationError(Error): ...
def ErrorLookup(state_code): ...
def WarningLookup(state_code): ...Transaction control with savepoints, context managers, and isolation level management for reliable database operations.
class Transaction:
def start(): ...
def commit(): ...
def rollback(): ...
def savepoint(name=None): ...
class Database:
def xact(): ...PostgreSQL cluster and installation management tools for controlling local PostgreSQL instances, configuration, and maintenance operations.
class Cluster:
def start(): ...
def stop(): ...
def restart(): ...
def initialize(): ...
class Installation:
def version(): ...
def bin_directory(): ...Advanced PostgreSQL features including COPY operations, LISTEN/NOTIFY, advisory locks, and streaming results for high-performance database applications.
class CopyManager:
def load_rows(statement, rows): ...
def dump_rows(statement): ...
class NotificationManager:
def listen(channel): ...
def notify(channel, payload=None): ...
class ALock:
def __enter__(): ...
def __exit__(): ...Core types used across the API:
class Connection:
"""Database connection interface with query and transaction methods."""
class Database:
"""Base database interface providing core operations."""
class Statement:
"""Prepared statement with parameter binding and execution methods."""
class Row:
"""Named tuple-like interface for query result rows."""
class Array:
"""PostgreSQL array type with multi-dimensional support."""