Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

createdb fails with permission denied to create database

Errorcreatedb: database creation failed: ERROR: permission denied to create database

Short 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.

  • Applies to PostgreSQL 16, PostgreSQL 17, PostgreSQL 18
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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;"
  2. 2Match what you see, then jump to its fix

    1. 1rolcreatedb and rolsuper are both fThe role you connect as lacks CREATEDBFix
    2. 2current_user is your operating system login (for example the Linux account you are signed in with) and not the role you meantcreatedb connects as the wrong userFix

What does the error look like?

  • createdb: database creation failed: ERROR: permission denied to create database
  • ERROR: 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 mydb

Confirmed by postgresql.org

Did this fix it?

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 mydb

3Verify

List the databases and look for the new one.

psql -U postgres -l

Confirmed by postgresql.org

Did this fix it?

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

Next step

Want this handled for good?

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