manage.py test stops with 'permission denied to create database' on PostgreSQL
Got an error creating the test database: permission denied to create databaseShort 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.
Fix it fast
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';"2Match what you see, then jump to its fix
What does the error look like?
Got an error creating the test database: permission denied to create databasedjango.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 testConfirmed by postgresql.org
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 --keepdb3Verify
The run starts without a prompt.
python manage.py test --noinputConfirmed by docs.djangoproject.com
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
Databases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
IntelliJ IDEA Test Connection fails for a PostgreSQL data source
Connection to localhost:5432 refused.
When Test Connection fails, work from the bottom up.
4 causes, each with a fixDatabases
pg_dump refuses to run: server version is newer than pg_dump
pg_dump: detail: server version: 17.2; pg_dump version: 14.13
pg_dump will not dump a server whose major version is newer than its own.
3 causes, each with a fixDatabases
Postgres cannot find the vector type for pgvector
ERROR: type "vector" does not exist
Installing pgvector on the server is not enough.
3 causes, each with a fix