createdb fails with permission denied to create database
createdb: database creation failed: ERROR: permission denied to create databaseShort answer
PostgreSQL lets only superusers and roles with the CREATEDB attribute create databases. createdb connects as your operating system user unless told otherwise, so it often runs as a role such as root that does not exist or cannot create databases. Connect as a privileged role with -U, or grant CREATEDB to the role you use.
Fix it fast
1Find out which cause you have
Ask the server which role you are and whether it may create databases.
psql -d postgres -c "SELECT current_user;" psql -d postgres -c "SELECT rolname, rolcreatedb, rolsuper FROM pg_roles WHERE rolname = current_user;"2Match what you see, then jump to its fix
What does the error look like?
createdb: database creation failed: ERROR: permission denied to create databaseERROR: permission denied to create database
Cause 1 of 2
The role you connect as lacks CREATEDB
1Check
The manual says a role must be explicitly given permission to create databases, except superusers, who bypass all permission checks.
psql -U postgres -c "SELECT rolname, rolcreatedb FROM pg_roles ORDER BY 1;"2Fix
As a superuser, grant the attribute to the application's role. Give it to the role that needs it, not to every role.
ALTER ROLE app_user CREATEDB;3Verify
Create the database as that role.
createdb -U app_user mydbConfirmed by postgresql.org
Cause 2 of 2
createdb connects as the wrong user
1Check
The createdb manual lists -U and PGUSER as the ways to choose the connection user. The libpq manual says the user defaults to the operating system name of the user running the program, so with neither set, createdb connects as your Linux or Windows login name.
echo "$USER / ${PGUSER:-PGUSER not set}"2Fix
Name the role with -U, or set PGUSER for the session, and pass the database name explicitly.
createdb -U postgres mydb
# or, for the whole shell session
export PGUSER=postgres
createdb mydb3Verify
List the databases and look for the new one.
psql -U postgres -lConfirmed by postgresql.org
How do I stop it coming back?
- Give a deployment or test-runner role CREATEDB only when it must create databases, and nothing more.
- Set PGUSER (and PGDATABASE) in the scripts that call createdb so they never depend on who runs them.
- Avoid superuser accounts for day-to-day work; the manual warns it is a dangerous privilege.
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
DBeaver cannot download the PostgreSQL driver from Maven
The PostgreSQL driver files are missing and the download does not complete
DBeaver downloads the PostgreSQL JDBC driver from a Maven repository the first time you connect.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 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 fix