Skip to content
Latchkey

Django "Got an error creating the test database: database already exists" in CI

Django tries to create a fresh test_<db> before running tests. A leftover database from a cancelled or parallel run already holds that name, so creation fails and Django prompts to delete it, which is non-interactive in CI.

What this error means

The test runner halts with "Got an error creating the test database: database \"test_app\" already exists" and waits on a "Type yes to delete it" prompt that never gets input.

python
Got an error creating the test database: database "test_app" already exists

Type 'yes' if you would like to try deleting the test database 'test_app', or 'no' to cancel:

Common causes

A previous run left the test database

A cancelled job did not tear down test_app, so the next run collides on creation.

Parallel jobs share one database server

Two jobs target the same Postgres and race to create the same test database name.

How to fix it

Reuse the database with --keepdb

Tell the runner to reuse an existing test database instead of recreating it, avoiding the collision and the prompt.

Terminal
python manage.py test --keepdb --noinput

Run non-interactively against a clean service

  1. Use a fresh Postgres service container per job so no leftover database exists.
  2. Pass --noinput so the runner never blocks on the delete prompt.
  3. For parallel jobs, give each a distinct database or service.
Terminal
python manage.py test --noinput

How to prevent it

  • Use an ephemeral DB service container so each job starts clean.
  • Always pass --noinput so prompts never hang the job.
  • Isolate parallel jobs with distinct database names.

Related guides

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