Skip to main content
Codex-LB supports two database backends: SQLite (default) and PostgreSQL. SQLite is recommended for most deployments, while PostgreSQL is available for high-traffic or multi-instance setups.

SQLite (Default)

SQLite is the default database backend and is suitable for most use cases. It requires no external services and stores all data in a single file.

Configuration

The ~ expands to your home directory. For Docker deployments:

SQLite Features

Codex-LB automatically creates backups before running database migrations.
Backups are stored in the same directory as the database file with a timestamp:
Codex-LB enables SQLite’s Write-Ahead Logging (WAL) mode for improved concurrency and crash recovery.Benefits:
  • Multiple readers can access the database while a writer is active
  • Better crash recovery
  • Improved performance for concurrent workloads
WAL mode creates additional files alongside your database:
These files are managed automatically and should be included in backups.
SQLite uses a connection pool for better performance:
  • Pool Size: Maximum number of persistent connections
  • Max Overflow: Additional connections that can be created temporarily
  • Timeout: How long to wait for an available connection

SQLite Limitations

SQLite has some limitations compared to PostgreSQL:
  • Single Writer: Only one process can write at a time
  • No Network Access: Cannot be accessed remotely
  • Limited Concurrency: Not suitable for high-traffic multi-instance deployments
For most single-instance deployments, these limitations are not a concern.

PostgreSQL

PostgreSQL is recommended for high-traffic deployments or when running multiple Codex-LB instances.

Setup

  1. Install PostgreSQL (if not already installed):
  1. Create a Database and User:
  1. Configure Codex-LB:

Connection URL Format

Examples:

PostgreSQL Connection Pool

PostgreSQL uses a connection pool for optimal performance:
Recommended Pool Sizes:
  • Light traffic (< 10 req/s): POOL_SIZE=10, MAX_OVERFLOW=5
  • Medium traffic (10-50 req/s): POOL_SIZE=20, MAX_OVERFLOW=10
  • High traffic (> 50 req/s): POOL_SIZE=40, MAX_OVERFLOW=20
Monitor your PostgreSQL connection count and adjust accordingly.

PostgreSQL vs SQLite

Database Migrations

Codex-LB uses Alembic for database migrations. Migrations run automatically on startup by default.

Automatic Migrations

  • MIGRATE_ON_STARTUP: Run migrations automatically when Codex-LB starts
  • FAIL_FAST: Stop startup if migrations fail (recommended for production)

Manual Migrations

If you prefer to run migrations manually:
Then run migrations using Alembic:

Backup & Recovery

SQLite Backups

SQLite backups are created automatically before migrations. To create manual backups:

PostgreSQL Backups

Use pg_dump for PostgreSQL backups:
For continuous backups, consider:
  • pg_basebackup: Physical backups
  • WAL archiving: Point-in-time recovery
  • Cloud backups: AWS RDS automated backups, etc.

Troubleshooting

SQLite Database Locked

Cause: Another Codex-LB instance is accessing the database. Solution: Stop all Codex-LB instances and restart:

PostgreSQL Connection Refused

Solutions:
  1. Verify PostgreSQL is running: sudo systemctl status postgresql
  2. Check the connection URL format
  3. Verify firewall rules allow port 5432
  4. Check pg_hba.conf for access permissions

Migration Failures

If migrations fail:
  1. Check logs for specific error messages
  2. Verify database connectivity
  3. For SQLite, restore from automatic backup:
  1. For PostgreSQL, restore from pg_dump backup

Docker Deployment

SQLite with Docker Volume

PostgreSQL with Docker Compose