SQLSTATE 42501 ERROR Class 42: Syntax Error or Access Rule Violation

insufficient_privilege permission denied to create database — 42501

PostgreSQL error "permission denied to create database" (SQLSTATE 42501): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A role tried to CREATE DATABASE without the CREATEDB privilege. PostgreSQL raises SQLSTATE 42501 (insufficient_privilege).

  • The role lacks the CREATEDB attribute (and is not a superuser).
  • Common for application roles created with least privilege.
  • Same pattern applies to other privileged actions.

What the server log shows

ERROR:  permission denied to create database

Why PostgreSQL raises this — what the manual says

Section 21.2 Role Attributes:

“A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks).”

Database creation requires the CREATEDB role attribute or superuser status. Without it, PostgreSQL blocks the command with 42501.

Common causes

  • The role does not have CREATEDB.
  • Running migrations/tests under a least-privilege role.
  • Expecting superuser behaviour from an ordinary role.

How to fix it

  1. Grant the attribute: ALTER ROLE app CREATEDB; (by a superuser).
  2. Run the create as a role that has CREATEDB or is superuser.
  3. Pre-create the database out-of-band and grant CONNECT/usage to the app role.

Related & next steps

Reference: PostgreSQL 18 Section 22.2 “Role Attributes”.

Was this helpful?