Database Configuration¶
Configure and optimize database connections for PyCharter.
Connection Strings¶
PostgreSQL¶
# Basic
"postgresql://user:pass@localhost:5432/dbname"
# With SSL
"postgresql://user:pass@host:5432/dbname?sslmode=require"
# With connection pool
"postgresql://user:pass@host:5432/dbname?pool_size=10"
MySQL¶
SQLite¶
Metadata Store Configuration¶
from pycharter import PostgresMetadataStore
store = PostgresMetadataStore(
connection_string="postgresql://user:pass@localhost/pycharter",
pool_size=10,
pool_timeout=30
)
store.connect()
Database Initialization¶
# Initialize database schema
pycharter db init
# Run migrations
pycharter db migrate
# Check status
pycharter db status
Environment Variables¶
export DATABASE_URL="postgresql://user:pass@localhost/pycharter"
export DB_POOL_SIZE=10
export DB_POOL_TIMEOUT=30
import os
from pycharter import PostgresMetadataStore
store = PostgresMetadataStore(os.environ["DATABASE_URL"])
SSH Tunnel for Secure Connections¶
from pycharter import DatabaseExtractor
extractor = DatabaseExtractor(
connection_string="postgresql://user:pass@localhost/db",
ssh_tunnel={
"host": "bastion.example.com",
"port": 22,
"username": "deploy",
"key_file": "~/.ssh/id_rsa"
}
)
Connection Pooling Best Practices¶
- Size the pool appropriately - Match to expected concurrent connections
- Set timeouts - Prevent hanging connections
- Use connection recycling - Prevent stale connections
- Monitor pool usage - Track connection wait times