PostgreSQL Python driver alternative to psycopg, written in Rust with PyO3.
altpg is a high-performance PostgreSQL database adapter for Python that aims to be a drop-in replacement for psycopg2. It is built using Rust and PyO3, leveraging the rust-postgres crate for database connectivity.
- DB-API 2.0 Compliant: Implements the Python Database API Specification v2.0
- High Performance: Written in Rust for maximum performance
- psycopg2 Compatible: Designed to be a drop-in replacement for psycopg2
- SQLAlchemy Compatible: Works with SQLAlchemy and other ORMs
- Type Safe: Leverages Rust's type system for safety
- Easy to Install: Distributed as Python wheels, no compilation needed
pip install altpgOr install from wheel:
pip install target/wheels/altpg-*.whlimport altpg
# Connect to PostgreSQL
conn = altpg.connect(
host='localhost',
port=5432,
user='postgres',
password='password',
dbname='mydb'
)
# Create a cursor
cursor = conn.cursor()
# Execute a query
cursor.execute("SELECT * FROM users WHERE id = %s", (1,))
# Fetch results
row = cursor.fetchone()
print(row)
# Commit and close
conn.commit()
cursor.close()
conn.close()import altpg
with altpg.connect(host='localhost', dbname='mydb', user='postgres', password='password') as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
print(row)connect(dsn=None, host=None, port=None, user=None, password=None, dbname=None)- Create a connectioncursor()- Create a cursor objectcommit()- Commit the current transactionrollback()- Roll back the current transactionclose()- Close the connectionautocommit- Get/set autocommit mode (property)
execute(query, params=None)- Execute a queryfetchone()- Fetch one rowfetchall()- Fetch all rowsfetchmany(size=None)- Fetch multiple rowsclose()- Close the cursordescription- Column information (property)rowcount- Number of affected rows (property)arraysize- Number of rows to fetch at a time (property)
DatabaseError- Base exception for database errorsIntegrityError- Integrity constraint violationProgrammingError- Programming error (e.g., syntax error)OperationalError- Operational error (e.g., connection failed)InterfaceError- Error related to the database interface
apilevel- String constant "2.0"threadsafety- Integer constant 2 (threads may share the module and connections)paramstyle- String constant "pyformat"
altpg can be used as a PostgreSQL dialect for SQLAlchemy:
from sqlalchemy import create_engine
# Use altpg as the driver
engine = create_engine('postgresql+altpg://user:password@localhost/mydb')
# Use with SQLAlchemy ORM
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()Requirements:
- Rust 1.65 or later
- Python 3.8 or later
- maturin
# Install maturin
pip install maturin
# Build the wheel
maturin build --release
# Install the wheel
pip install target/wheels/altpg-*.whl# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install development dependencies
pip install maturin pytest
# Build and install in development mode
maturin develop
# Run tests
pytest tests/altpg is designed for high performance by leveraging Rust's zero-cost abstractions and efficient memory management. Benchmarks show significant performance improvements over pure Python implementations.
- Python 3.8+
- PostgreSQL 9.6+
- Compatible with psycopg2 API
- Works with SQLAlchemy, Django, Flask-SQLAlchemy, and other ORMs
This is an early release with basic functionality. Some advanced psycopg2 features may not yet be implemented:
- Advanced type adapters
- Server-side cursors
- COPY operations
- Asynchronous operations
- NOTIFY/LISTEN
Contributions are welcome!
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE file for details
Built with:
- PyO3 - Rust bindings for Python
- rust-postgres - PostgreSQL client library
- Complete type adapter system
- Server-side cursors
- COPY operations
- Async/await support
- NOTIFY/LISTEN support
- Connection pooling
- Full psycopg2/psycopg3 compatibility
- Performance benchmarks
- Comprehensive test suite with real PostgreSQL tests