MAGE_DATABASE_CONNECTION_URL environment variable.
In production, you can set the environment variable in the corresponding Terraform script.
PostgreSQL
export MAGE_DATABASE_CONNECTION_URL=postgresql+psycopg2://user:password@host:port/dbname
To use a specific schema_name in PostgreSQL:
export MAGE_DATABASE_CONNECTION_URL="postgresql+psycopg2://user:password@host:port/dbname?options=-c%20search_path%3Dschema_name"
Get credentials from AWS Secret Manager
You can also instruct Mage to fetch your Postgres DB credentials from AWS Secret Manager. You will need to set the following environment variables:| Variable | Value |
|---|---|
AWS_DB_SECRETS_NAME | name of secret in AWS Secret Manager |
AWS_DEFAULT_REGION | AWS region the secret is stored in |
AWS_ACCESS_KEY_ID | AWS access key id (not needed if using IAM role to authenticate) |
AWS_SECRET_ACCESS_KEY | AWS secret access key (not needed if using IAM role to authenticate) |
- username
- password
- engine (must be
postgres) - host
- port
- dbname
MSSQL (not officially supported)
export MAGE_DATABASE_CONNECTION_URL="mssql+pyodbc://?odbc_connect=DRIVER={ODBC Driver 18 for SQL Server};SERVER=host;DATABASE=dbname;UID=user;PWD=password;ENCRYPT=yes;TrustServerCertificate=yes;"
Troubleshoot database migration failures
Mage uses Alembic migrations for orchestration tables such as triggers, pipeline runs, block runs, users, secrets, and audit events. If the server fails during startup with an error likealembic, sqlalchemy.exc, DROP INDEX, CREATE INDEX, alembic_version, or Can't locate revision, use the Mage database CLI before manually editing the database.
Start with a read-only diagnosis:
alembic_version table, migration head in the installed Mage code, recommendations, and any pending revisions that have not been migrated yet.
To list every Alembic revision available in the installed Mage code:
Run pending migrations
If the database is behind the installed Mage code, run:Roll back a failed or incompatible migration
If a migration was applied and you need to revert it, roll back one migration:user table can wait while the running Mage server has active sessions using that table. The CLI applies a PostgreSQL lock timeout so the command fails instead of waiting forever:
mage db diagnose before restarting Mage.
Stamp a revision without running SQL
Usestamp only when the database schema already matches the target revision but the alembic_version metadata is missing or wrong:
stamp changes Alembic metadata without creating, dropping, or altering database objects. It can help when a migration partially completed outside Alembic, but it can also hide real schema drift if used incorrectly. If you see an error such as index "..." does not exist, confirm whether the rest of the migration’s expected schema changes are already present before stamping past that revision.
Typical recovery flow
- Back up the database before making changes in production.
- Run
mage db diagnose. - Run
mage db historyto find the target revision and surrounding migrations. - If the error names a table, index, or constraint, run
mage db schema <table>to inspect the live table. - If the database is behind, run
mage db migrate. - If the latest migration is bad for your deployment, run
mage db rollback --revision -1, then pin or deploy a Mage version with a compatible migration chain. - If the schema already matches the intended revision but
alembic_versionis wrong, runmage db stamp <revision>. - Run
mage db diagnoseagain and restart Mage.