Repository
Database Connections
Python
Overview
Database Connections is a unified Python library for managing connections across multiple database systems. It provides a consistent, production-ready interface for working with both relational and non-relational databases, with built-in support for connection pooling, async/sync execution, health monitoring, and performance metrics.
This library is designed for modern backend systems and AI-driven applications where multiple data sources must be managed reliably and efficiently.
Key Features
- 🔌 Multi-Database Support
Works with PostgreSQL, Redis, MongoDB, ClickHouse, RabbitMQ, and Neo4j. - ⚡ Async & Sync Support
Unified interface for both synchronous and asynchronous workloads. - 🔄 Connection Pooling
Efficient reuse of connections with configurable pool sizes and overflow handling. - 🏥 Health Monitoring
Built-in health checks for both connection pools and database servers. - 📊 Performance Metrics
Track active connections, wait times, and pool utilization in real time. - 🔧 Framework Integration
Seamless integration with FastAPI, Flask, and Django. - 🎯 Fully Typed API
Strong type hints for better IDE support and safer development. - 🧪 Production Ready
Comprehensive test coverage and enterprise-grade design.
Installation
Install the base package:
Install with database-specific support:
pip install db_connections[redis]
pip install db_connections[mongodb]
pip install db_connections[clickhouse]
pip install db_connections[rabbitmq]
pip install db_connections[neo4j]
Install multiple databases:
Install everything:
For development:
Quick Start
PostgreSQL (Sync)
PostgresConnectionPool,
PostgresPoolConfig,
)
config = PostgresPoolConfig(
host="localhost",
port=5432,
database="mydb",
user="postgres",
password="secret",
min_size=2,
max_size=10,
)
with PostgresConnectionPool(config) as pool:
with pool.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
print(cursor.fetchall())
Redis
RedisSyncConnectionPool,
RedisPoolConfig,
)
config = RedisPoolConfig(host="localhost", port=6379, db=0)
with RedisSyncConnectionPool(config) as pool:
with pool.get_connection() as conn:
conn.set("key", "value")
print(conn.get("key"))
MongoDB
MongoSyncConnectionPool,
MongoPoolConfig,
)
config = MongoPoolConfig(host="localhost", port=27017, database="mydb")
with MongoSyncConnectionPool(config) as pool:
with pool.get_connection() as client:
db = client[config.database]
print(db.users.find_one())
Async Example (PostgreSQL)
AsyncPostgresConnectionPool,
PostgresPoolConfig,
)
config = PostgresPoolConfig.from_env()
async with AsyncPostgresConnectionPool(config) as pool:
async with pool.get_connection() as conn:
results = await conn.fetch("SELECT * FROM users")
Supported Databases
- PostgreSQL — psycopg2 / asyncpg
- Redis — redis / redis[hiredis]
- MongoDB — pymongo / motor
- ClickHouse — clickhouse-connect
- RabbitMQ — pika / aio-pika
- Neo4j — neo4j driver
Each connector includes a dedicated setup guide inside the repository.
Core Capabilities
🔄 Connection Pooling
- Min/max pool configuration
- Automatic reuse and cleanup
- Thread-safe and async-safe implementations
🏥 Health Checks
- Pool state monitoring
- Database responsiveness checks
- Response time tracking
📊 Metrics
- Active vs idle connections
- Wait time statistics
- Connection lifecycle tracking
Framework Integration
FastAPI Example
async def startup():
config = PostgresPoolConfig.from_env()
app.state.db_pool = AsyncPostgresConnectionPool(config)
await app.state.db_pool.initialize_pool()
Configuration Options
Supports multiple configuration styles:
- Environment variables
- DSN / connection strings
- URI-based configuration
- SSL/TLS and timeout settings
Why This Library?
Modern backend systems often rely on multiple databases simultaneously. This library simplifies that complexity by providing:
- A unified API across all databases
- Consistent async/sync behavior
- Production-grade pooling and monitoring
- Strong typing for safer development
It is especially useful for microservices architectures and AI agent systems that require reliable multi-database access.
License
MIT License