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

insufficient_privilege Insufficient Privilege — SQLSTATE 42501

The role lacks a required privilege on the object, schema, or database.

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

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 USAGE forgotten.
  • Past GRANT ... ON ALL TABLES does 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:

  1. Grant usage and the object privilege: GRANT USAGE ON SCHEMA s TO role; GRANT SELECT, INSERT ON t TO role;.
  2. Cover future objects: ALTER DEFAULT PRIVILEGES IN SCHEMA s GRANT SELECT ON TABLES TO role;.
  3. For serial/identity inserts: GRANT USAGE ON ALL SEQUENCES IN SCHEMA s TO role;.
  4. Verify with has_table_privilege and has_schema_privilege.

Reference: PostgreSQL error codes — Class 42 (Syntax Error or Access Rule Violation).

Was this helpful?