Set up PostgreSQL database and run Alembic migrations
The midPilot Connector Generator uses PostgreSQL for data persistence and Alembic for database schema migrations. This guide covers database setup and migration management.
The easiest way to get started is using the provided Docker Compose configuration.
1
Configure environment
Edit your .env file:
DATABASE__HOST=localhostDATABASE__PORT=5432DATABASE__NAME=connector_dbDATABASE__USER=connector_userDATABASE__PASSWORD=secure_password_here# Auto-constructed by the appDATABASE__URL=postgresql+asyncpg://${DATABASE__USER}:${DATABASE__PASSWORD}@${DATABASE__HOST}:${DATABASE__PORT}/${DATABASE__NAME}
2
Start the database
# Start only the database servicedocker compose up db -d# Or start all servicesdocker compose up -d
The database will:
Start PostgreSQL 15 Alpine
Create the configured database
Run health checks
Persist data in postgres_data volume
3
Verify the database
# Check health statusdocker compose ps# Test connectiondocker compose exec db psql -U connector_user -d connector_db -c "SELECT version();"
# Connect as postgres usersudo -u postgres psql# Create user and databaseCREATE USER connector_user WITH PASSWORD 'secure_password';CREATE DATABASE connector_db OWNER connector_user;GRANT ALL PRIVILEGES ON DATABASE connector_db TO connector_user;\q
If DATABASE__URL is not provided or contains placeholders (e.g., ${DATABASE__USER}), the application will automatically construct it from individual parameters.
# Upgrade to latestuv run alembic upgrade head# Upgrade to specific revisionuv run alembic upgrade abc123# Upgrade one versionuv run alembic upgrade +1# Show SQL without executinguv run alembic upgrade head --sql
# Stop and remove volumesdocker compose down -v# Start freshdocker compose up -d# Run migrationsdocker compose exec midpilot-connector-gen uv run alembic upgrade head
Symptom: Alembic migration fails because a table/column already existsSolution:
# Check current revisionuv run alembic current# Mark migration as applied without running ituv run alembic stamp head# Or rollback and retryuv run alembic downgrade -1uv run alembic upgrade head
Connection refused
Symptom:could not connect to server: Connection refusedSolution:
# Check if database is runningdocker compose ps db# Check database logsdocker compose logs db# Verify environment variablesenv | grep DATABASE# Test connection manuallydocker compose exec db psql -U connector_user -d connector_db
Verify DATABASE__USER and DATABASE__PASSWORD in .env
Ensure user has correct permissions
Recreate user if needed:
docker compose exec db psql -U postgresDROP USER IF EXISTS connector_user;CREATE USER connector_user WITH PASSWORD 'new_password';GRANT ALL PRIVILEGES ON DATABASE connector_db TO connector_user;
Database doesn't exist
Symptom:FATAL: database "connector_db" does not existSolution:
# Create databasedocker compose exec db createdb -U postgres connector_db# Grant permissionsdocker compose exec db psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE connector_db TO connector_user;"# Run migrationsuv run alembic upgrade head
Slow migrations
Symptom: Migrations take a long timeSolution:
Check database resources
Review migration SQL for inefficiencies
Run migrations during low-traffic periods
# Preview SQL without executinguv run alembic upgrade head --sql > migration.sql# Review the SQLcat migration.sql