Skip to main content
The midPilot Connector Generator uses PostgreSQL for data persistence and Alembic for database schema migrations. This guide covers database setup and migration management.

Database Requirements

PostgreSQL 15+

PostgreSQL 15 or later with asyncpg driver support
The application uses:
  • SQLAlchemy - Async ORM for database operations
  • Alembic - Database migration tool
  • asyncpg - High-performance async PostgreSQL driver

Setup Methods

Choose your preferred database setup method:

Database Configuration

Connection Parameters

The application supports both individual parameters and full connection URLs:
DATABASE__HOST=localhost
DATABASE__PORT=5432
DATABASE__NAME=connector_db
DATABASE__USER=connector_user
DATABASE__PASSWORD=secure_password
If DATABASE__URL is not provided or contains placeholders (e.g., ${DATABASE__USER}), the application will automatically construct it from individual parameters.

Connection Pool Settings

Configure the SQLAlchemy connection pool:
# Connection pool size
DATABASE__POOL_SIZE=10

# Maximum overflow connections
DATABASE__MAX_OVERFLOW=20

# Enable SQL query logging (for debugging)
DATABASE__ECHO=false
DATABASE__POOL_SIZE
integer
default:"10"
Number of connections to maintain in the pool
DATABASE__MAX_OVERFLOW
integer
default:"20"
Maximum connections beyond pool_size
DATABASE__ECHO
boolean
default:"false"
Log all SQL queries (useful for debugging, but verbose)

Alembic Migrations

Alembic manages database schema changes through migrations. The application includes migration files in the alembic/versions/ directory.

Running Migrations

1

Check current migration status

uv run alembic current
Shows the currently applied migration revision.
2

View migration history

uv run alembic history
Displays all available migrations.
3

Run all pending migrations

uv run alembic upgrade head
Applies all migrations up to the latest version.
4

Verify the schema

# Connect to database
docker compose exec db psql -U connector_user -d connector_db

# List tables
\dt

# Describe a table
\d table_name

Migration Commands

# Upgrade to latest
uv run alembic upgrade head

# Upgrade to specific revision
uv run alembic upgrade abc123

# Upgrade one version
uv run alembic upgrade +1

# Show SQL without executing
uv run alembic upgrade head --sql

Existing Migrations

The application includes these migrations:
Purpose: Creates initial database schemaTables created:
  • Base tables for connector storage
  • Initial indexes and constraints
# View migration
cat alembic/versions/001_initial_schema.py
Purpose: Adds job tracking columnChanges:
  • Adds scrape_job_ids column
  • Updates related indexes
# View migration
cat alembic/versions/002_add_scrape_job_ids_column_to_.py
Purpose: Adds input normalization supportChanges:
  • Adds normalized_input column to jobs table
  • Enables input deduplication
# View migration
cat alembic/versions/003_add_normalized_input_column_to_jobs_.py

Alembic Configuration

The migration system is configured in alembic.ini:
[alembic]
script_location = alembic
prepend_sys_path = .

# Database URL loaded from DATABASE__URL environment variable
sqlalchemy.url =
The database URL is read from the DATABASE__URL environment variable by alembic/env.py, ensuring consistency with application configuration.

Database Management

Backup and Restore

# Backup database
docker compose exec db pg_dump -U connector_user connector_db > backup.sql

# Backup with timestamp
docker compose exec db pg_dump -U connector_user connector_db > backup_$(date +%Y%m%d_%H%M%S).sql

# Compressed backup
docker compose exec db pg_dump -U connector_user connector_db | gzip > backup.sql.gz

# Backup schema only
docker compose exec db pg_dump -U connector_user --schema-only connector_db > schema.sql

Reset Database

To completely reset the database:
1

Downgrade to base

uv run alembic downgrade base
This removes all tables created by migrations.
2

Re-run migrations

uv run alembic upgrade head
This recreates all tables with the latest schema.
Or using Docker:
# Stop and remove volumes
docker compose down -v

# Start fresh
docker compose up -d

# Run migrations
docker compose exec midpilot-connector-gen uv run alembic upgrade head

Access Database

# Interactive shell
docker compose exec db psql -U connector_user -d connector_db

# Run single query
docker compose exec db psql -U connector_user -d connector_db -c "SELECT COUNT(*) FROM jobs;"

# Execute SQL file
docker compose exec -T db psql -U connector_user -d connector_db < query.sql

Troubleshooting

Symptom: Alembic migration fails because a table/column already existsSolution:
# Check current revision
uv run alembic current

# Mark migration as applied without running it
uv run alembic stamp head

# Or rollback and retry
uv run alembic downgrade -1
uv run alembic upgrade head
Symptom: could not connect to server: Connection refusedSolution:
# Check if database is running
docker compose ps db

# Check database logs
docker compose logs db

# Verify environment variables
env | grep DATABASE

# Test connection manually
docker compose exec db psql -U connector_user -d connector_db
Symptom: FATAL: password authentication failedSolution:
  • Verify DATABASE__USER and DATABASE__PASSWORD in .env
  • Ensure user has correct permissions
  • Recreate user if needed:
docker compose exec db psql -U postgres
DROP USER IF EXISTS connector_user;
CREATE USER connector_user WITH PASSWORD 'new_password';
GRANT ALL PRIVILEGES ON DATABASE connector_db TO connector_user;
Symptom: FATAL: database "connector_db" does not existSolution:
# Create database
docker compose exec db createdb -U postgres connector_db

# Grant permissions
docker compose exec db psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE connector_db TO connector_user;"

# Run migrations
uv run alembic upgrade head
Symptom: Migrations take a long timeSolution:
  • Check database resources
  • Review migration SQL for inefficiencies
  • Run migrations during low-traffic periods
# Preview SQL without executing
uv run alembic upgrade head --sql > migration.sql

# Review the SQL
cat migration.sql

Production Best Practices

Regular Backups

Schedule automated backups with retention policies

Test Migrations

Test migrations in staging before production

Monitor Performance

Track query performance and connection pool usage

Use Connection Pooling

Tune pool_size and max_overflow for your workload

Next Steps

Configuration

Configure database connection settings

Docker Setup

Deploy with Docker Compose

API Reference

Start using the API

Build docs developers (and LLMs) love