Symptoms
The role lacks a required privilege on the object, schema, or database.
- The error is written to the server log and returned to the client carrying
SQLSTATE 42501. - Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
- PL/pgSQL can trap it by name:
EXCEPTION WHEN insufficient_privilege THEN.
Environment
Severity: ERROR | PostgreSQL versions: 12, 13, 14, 15, 16, 17
Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).
Root Cause
Access control rejected the operation at one of the privilege layers: database CONNECT, schema USAGE, or the object privilege itself. Missing schema USAGE is by far the most common cause.
Common causes:
- Object privilege granted but schema
USAGEforgotten. - Past
GRANT ... ON ALL TABLESdoes not cover future tables. - PostgreSQL 15+ no longer auto-grants CREATE on the public schema.
- Serial/identity INSERTs need privileges on the underlying sequence.
Diagnostic Queries
Recovery
Steps to resolve 42501:
- Grant usage and the object privilege:
GRANT USAGE ON SCHEMA s TO role; GRANT SELECT, INSERT ON t TO role;. - Cover future objects:
ALTER DEFAULT PRIVILEGES IN SCHEMA s GRANT SELECT ON TABLES TO role;. - For serial/identity inserts:
GRANT USAGE ON ALL SEQUENCES IN SCHEMA s TO role;. - Verify with
has_table_privilegeandhas_schema_privilege.
Reference: PostgreSQL error codes — Class 42 (Syntax Error or Access Rule Violation).
Thanks — noted. This helps keep the database accurate.