Skip to content
Latchkey

Django "FATAL: database ... does not exist" in CI

The Postgres service is reachable, but the specific database Django connects to was never created. The service container only creates the database named in POSTGRES_DB, so a mismatch with the name in DATABASES raises FATAL at connect time.

What this error means

Django fails immediately with "django.db.utils.OperationalError: FATAL: database \"app\" does not exist" while the connection itself succeeds.

Django
django.db.utils.OperationalError: FATAL:  database "app" does not exist

Common causes

POSTGRES_DB does not match the app database name

The service creates a database from POSTGRES_DB (default postgres), but DATABASES points at app, which was never created.

DATABASE_URL names a different database

The URL host and credentials are right, but the trailing database name differs from what the service initialized.

How to fix it

Create the database in the service env

Set POSTGRES_DB so the container creates the exact database Django will use.

.github/workflows/ci.yml
services:
  postgres:
    image: postgres:16
    env:
      POSTGRES_DB: app_test
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports: ["5432:5432"]

Align DATABASE_URL with the created database

Make the database segment of the URL match POSTGRES_DB exactly.

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

How to prevent it

  • Set POSTGRES_DB to the same name your DATABASES config uses.
  • Drive both the service and Django from one DATABASE_URL value.
  • Note that Django can create the test database, but the base database must exist first.

Related guides

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