Skip to content
Latchkey

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.

python
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.

.github/workflows/ci.yml
env:
  DATABASE_URL: postgres://postgres:postgres@localhost:5432/test

Run migrations before the step

Apply migrations so the schema exists when commands/tests run.

Terminal
python manage.py migrate --noinput

How to prevent it

  • Configure DATABASES from CI service values, not local defaults.
  • Gate steps on the DB service health check.
  • Run migrate before any step that touches the schema.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →