Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

manage.py test stops with 'permission denied to create database' on PostgreSQL

ErrorGot an error creating the test database: permission denied to create database

Short answer

Django creates a separate database called test_ plus your database name, using the DATABASES USER. When that role lacks the CREATEDB attribute, PostgreSQL refuses and the test run stops. Give the role CREATEDB with ALTER ROLE, make sure it can read the postgres database, and use --keepdb or --noinput for leftover databases.

  • Applies to PostgreSQL 16, Django 5.2, psycopg 3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which role Django logs in with, then check whether that role can create databases.

    psql -U postgres -c "SELECT rolname, rolcreatedb, rolsuper FROM pg_roles WHERE rolname = 'myuser';"
  2. 2Match what you see, then jump to its fix

    1. 1rolcreatedb is f for the USER in your DATABASES settingThe database role has no CREATEDB attributeFix
    2. 2Django asks 'Type yes if you would like to try deleting the test database' or fails in CIA test database from an interrupted run is still thereFix

What does the error look like?

  • Got an error creating the test database: permission denied to create database
  • django.db.utils.ProgrammingError: permission denied to create database

Cause 1 of 2

The database role has no CREATEDB attribute

1Check

Django creates the test database as the USER you configured, so that role needs the right to create databases. CREATEDB is off by default for a new role.

psql -U postgres -c "\du myuser"

2Fix

Grant the attribute to that role from a superuser session. The attribute is not inherited through group membership, so grant it to the login role itself.

psql -U postgres -c "ALTER ROLE myuser CREATEDB;"

3Verify

Run the tests again.

python manage.py test

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 2

A test database from an interrupted run is still there

1Check

A run that was forcibly stopped may leave test_<name> behind, and the next run asks whether to reuse or destroy it. CI has nobody to answer.

psql -U postgres -l | grep test_

2Fix

Delete it automatically with --noinput, or keep one test database between runs with --keepdb.

python manage.py test --noinput
# or keep it between runs:
python manage.py test --keepdb

3Verify

The run starts without a prompt.

python manage.py test --noinput

Confirmed by docs.djangoproject.com

Did this fix it?

How do I stop it coming back?

  • Give the development and CI roles CREATEDB, and keep the production role without it.
  • Use --keepdb on a laptop to save the setup time on every run.
  • Use --noinput in CI so a leftover database never blocks a build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.