Django "django.db.utils.OperationalError" in CI
Django's database layer raised an OperationalError - it could not connect to the database, or the schema it needs is not there. In CI this usually means the DB service is wrong/not ready, or migrations have not run.
What this error means
A Django management command or test fails with "django.db.utils.OperationalError: could not connect to server" or "no such table", only in CI.
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "db" (172.18.0.2) and accepting TCP/IP connections on port 5432?Common causes
The database service is misconfigured or not ready
DATABASES points at a host that is wrong or not yet accepting connections in CI.
Migrations were not applied
A "no such table" OperationalError means the schema was never created in the CI database.
How to fix it
Point Django at the CI database and wait for it
Set the DB settings from CI service values and ensure the service is healthy before running.
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testRun migrations before the step
Apply migrations so the schema exists when commands/tests run.
python manage.py migrate --noinputHow to prevent it
- Configure DATABASES from CI service values, not local defaults.
- Gate steps on the DB service health check.
- Run
migratebefore any step that touches the schema.