Zou uses PostgreSQL as its database backend with SQLAlchemy as the ORM. This guide covers database setup, configuration, and maintenance.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cgwire/zou/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Zou requires:- PostgreSQL 10 or higher
- A dedicated database and user account
- Network connectivity between Zou and PostgreSQL
Database Setup
1. Create Database and User
Connect to PostgreSQL and create a database and user:2. Configure Environment Variables
Set the database connection parameters:3. Initialize Database Schema
Run the database initialization command:- Creates all required tables using Flask-Migrate (Alembic)
- Applies all migration files from
zou/migrations/versions/ - Sets up initial database schema
4. Initialize Base Data
Create the minimal required data:5. Create Admin User
Create an admin user to access the API:Connection Configuration
Basic Connection Parameters
SQLAlchemy database driver.
postgresql+psycopg- psycopg3 (recommended, default)postgresql+psycopg2- psycopg2 (legacy)
Database server hostname or IP address.
Database server port.
Database username for authentication.
Database password for authentication.
Name of the database to connect to.
Connection URI
The connection URI is automatically generated from the configuration:Connection Pool Settings
Zou uses SQLAlchemy’s connection pooling to manage database connections efficiently. These settings are critical for production performance.Pool Configuration
Number of connections to maintain in the pool.Recommendations:
- Development: 5-10
- Production (small): 20-30
- Production (large): 50-100
Maximum connections beyond pool_size.Total max connections =
DB_POOL_SIZE + DB_MAX_OVERFLOWTypically set to 2× pool_size for burst traffic.Test connections before use to prevent stale connection errors.Adds slight overhead but prevents errors. Recommended: enabled
Recycle connections after N seconds (default: 1 hour).Prevents issues with:
- Database connection timeouts
- Network firewalls closing idle connections
- Long-lived connection problems
How to reset connections when returned to pool.Options:
commit- Commit any pending transactions (recommended)rollback- Roll back any pending transactionsnone- No reset (not recommended)
Example Pool Configuration
Production Pool Sizing
Formula for pool sizing:- 100 concurrent users
- 2 requests per second per user
- 50ms average query time
DB_POOL_SIZE=30 and DB_MAX_OVERFLOW=60
Database Migrations
Zou uses Flask-Migrate (built on Alembic) for database schema migrations.Migration Commands
Check Database Status
Initialize Database
Upgrade Database Schema
Downgrade Database (Development)
Create Migration (Development)
zou/migrations/versions/.
Reset Database
Migration Files Location
Migrations are stored in:Migration File Structure
Custom Migration Configuration
The migration environment is configured inzou/migrations/env.py:
Database Maintenance
Backup Database
Clear Old Data
Verify Database Connection
From zou/app/utils/dbhelpers.py:587:Troubleshooting
Connection Refused
Error:psycopg.OperationalError: connection refused
Solutions:
- Check PostgreSQL is running:
systemctl status postgresql - Verify host and port:
psql -h localhost -p 5432 -U zouuser -d zoudb - Check firewall rules
- Verify
postgresql.conflisten_addresses
Too Many Connections
Error:FATAL: remaining connection slots are reserved
Solutions:
- Increase PostgreSQL
max_connectionsinpostgresql.conf - Reduce
DB_POOL_SIZEandDB_MAX_OVERFLOW - Check for connection leaks in application code
Stale Connections
Error:server closed the connection unexpectedly
Solutions:
- Enable
DB_POOL_PRE_PING=True - Lower
DB_POOL_RECYCLE(try 300-600 seconds) - Increase PostgreSQL timeout settings
Slow Queries
Solutions:- Enable PostgreSQL query logging:
- Check missing indexes:
- Run
ANALYZEto update statistics:
Migration Conflicts
Error:Target database is not up to date
Solutions:
Performance Tips
- Use connection pooling - Default settings work well for most deployments
- Enable pre-ping - Prevents stale connection errors
- Monitor pool usage - Watch for pool exhaustion in logs
- Regular vacuuming - PostgreSQL autovacuum should be enabled
- Add indexes - Zou includes performance indexes in migrations
- Optimize queries - Use query profiling for slow endpoints
Security Best Practices
- Use strong passwords - Never use default passwords in production
- Restrict network access - Firewall PostgreSQL to only Zou servers
- Use SSL connections - Configure PostgreSQL SSL and use
sslmode=require - Regular backups - Automate database backups
- Principle of least privilege - Database user should only have necessary permissions
- Update regularly - Keep PostgreSQL updated with security patches