select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

SQLSTATE / CLASS 42 · SYNTAX ERROR OR ACCESS RULE VIOLATION

insufficient_privilege

SQLSTATE
42501
Condition name
insufficient_privilege
Class
Syntax Error or Access Rule Violation
Source macro
ERRCODE_INSUFFICIENT_PRIVILEGE
Evidence
Observed at runtime in the source evidence
Analysis & operational context

English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗

<h1>42501</h1>

At a glance

SQLSTATE 42501 is insufficient_privilege in Class 42. 42501 is insufficient privilege. The selected natural path calls nextval on a runner-owned sequence from a separate role that has schema USAGE but no sequence USAGE; PostgreSQL reports permission denied for sequence %s.

Meaning

42501 is insufficient privilege. In the selected path, nextval reaches nextval_internal, which checks sequence access for the effective GetUserId() before advancing the sequence. Schema USAGE lets the restricted role resolve objects in the schema; it does not grant the sequence privilege needed by this call. The server therefore emits the ERROR primary permission denied for sequence %s, with the sequence name supplied dynamically.

The ownership boundary matters: an owner or administrative connection prepares the disposable schema and sequence, while a separate restricted role runs nextval. The successful repair is a narrow GRANT USAGE ON SEQUENCE ... made by the owner/admin connection, followed by nextval again as the same restricted role; it is not evidence that the restricted role may grant itself access.

The same SQLSTATE also covers other privilege boundaries. The generic ACL checker can report permission denied for relation %s, permission denied for schema %s, or a column-specific form, while owner-only operations use must be owner of relation %s or the corresponding object kind. Row-level security is a separate executor check: a table ACL can pass while an INSERT or UPDATE row violates a policy and raises a new row violates row-level security policy ... error. These are source-confirmed producer branches, not additional observations from the selected sequence run.

Diagnosis

Check current_user and session_user, any SET ROLE, the database, schema, exact relation or column, and the relevant ACL. Keep the setup/grant connection separate from the probe connection: replace runner_host, runner_port, runner_db, and runner_user with the real disposable target and owner/admin credential, then connect separately as the generated restricted role (for example, syntax_role). A pool can otherwise make an ACL change appear to affect a different backend. If the primary names a relation, schema, or column, inspect that object's privilege rather than assuming sequence USAGE; must be owner requires checking the object's owner and effective role; an RLS primary requires inspecting the policy's USING/WITH CHECK path and the role it applies to. The error is an ERROR on the selected autocommit probe, so that same probe session remains IDLE; an explicit transaction would need its normal error-state handling before more work. Distinguish this object-privilege failure from 28000 startup authorization and 0A000 unsupported features.

Response

Have the owner/admin connection grant only the required privilege on the exact object (the selected case needs sequence USAGE), or use the intended owner/security-definer or RLS policy when that is the application design. Re-run nextval on the same restricted-role session after the ACL change and verify the returned value and session state. A superuser would bypass ACL checks, but do not raise a role to superuser as a substitute for targeted least-privilege grants; schema USAGE, table privileges, and sequence privileges are separate checks. For an owner-only failure, use the owning/migration role or change ownership deliberately rather than treating an ACL grant as an owner transfer. Inspect the effective role if membership or SET ROLE is involved.

Messages

The selected sequence source path raises an explicit ERROR with SQLSTATE 42501 and primary permission denied for sequence %s. %s is the resolved sequence relation name; this path has no DETAIL or HINT. Other fixed ACL branches use permission denied for relation %s, permission denied for schema %s, and permission denied for column "%s" of relation "%s"; owner-only checks use must be owner of relation %s or the matching object kind. RLS WITH CHECK branches use new row violates row-level security policy ... primaries. A client exception without the server SQLSTATE and the primary for the specific branch is not sufficient to identify that 42501 producer.

Representative case

The page uses the same statements as the runner registry. The owner/admin connection creates the sequence and grants schema USAGE; the restricted-role connection runs both nextval probes. Generated names such as syntax_schema and syntax_role are replaced by disposable runner values when executed, and connection placeholders such as runner_host, runner_port, runner_db, and runner_user must be replaced with the actual target rather than copied literally.

CREATE SEQUENCE syntax_schema.syntax_sequence START WITH 1;
GRANT USAGE ON SCHEMA syntax_schema TO syntax_role;
SELECT nextval('syntax_schema.syntax_sequence');
GRANT USAGE ON SEQUENCE syntax_schema.syntax_sequence TO syntax_role;
SELECT nextval('syntax_schema.syntax_sequence');

The selected 18.6 run reports the structured diagnostic and passes the repair assertions; the 10.21 run passes the same case-specific checks. The downloadable case and evidence projections are 42501 case JSON and authored evidence. The runner manifest is verify/cases/42501/cases.json, and the page SQL is checked against its shared registry.

Versions

The selected natural runtime scope is PostgreSQL 18.6 and 10.21; it does not infer behavior for every intermediate release.

Sources

  • src.errcodes.18.6 — fixed errcodes.txt definition at commit 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba (source).
  • src.sequence-permission.18.6src/backend/commands/sequence.c at REL_18_6 commit 724edf9bde9d356724ad384a2e196edc3c9f80f7; fixed blob SHA-256 3e5afe17d5a84862fae502a5481211220368f1d639e9d07ba6f96ee8be92a8d8 (source).
  • src.sequence-permission.10.23src/backend/commands/sequence.c at REL_10_23 commit 02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4; fixed blob SHA-256 5510e1266e8d8c548a4318d753392e92e689d348f3353a2aa53d8dea871c44ab (source).
  • src.aclcheck-errors.18.6 / src.aclcheck-errors.10.23 — fixed src/backend/catalog/aclchk.c ACL-kind tables map no-privilege and not-owner results to relation/schema/column messages; blob SHA-256 9700258318959b47c42edb423418fb511dd3a008023e732f601eecf4c80868f8 / 3a4330bd55ea8c0ec12324d7046ec31d64c920f99196235b84612d6cd1a4b26c (18.6 source, 10.23 source).
  • src.rls-errors.18.6 / src.rls-errors.10.23 — fixed src/backend/executor/execMain.c row-level security WITH CHECK errors; blob SHA-256 33b97337fa23a649c5e7a092e1bd405a54e8503529236c62c9d5bb93a1774a8d / 386d09f7e964ebc426a554cf51ac7cb505c67cc95b971ee7acad1a6e0a9d0c1a (18.6 source, 10.23 source).
  • src.calls.REL_18_6 / src.calls.REL_10_23 — fixed call scans, SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf / 00d16d3eb01b71ccf1b245c8f3102f9d0ec9f36fb02777b8dd1b99fcb263040c.

Source evidence

Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.

42501 is the insufficient_privilege condition in Class 42.

Method: Read fixed errcodes.txt and the locked catalogue metadata.

Limits: Directory identity does not identify every backend path.

src.errcodes.18.6

The fixed source calls for 42501 include the mechanism and message boundary selected for this page.

Method: Trace the resolved REL_18_6 and REL_10_23 source call records at the locked commits.

Limits: Other calls can use the same SQLSTATE with different context or dynamic fields.

src.sequence-permission.18.6 src.sequence-permission.10.23 src.aclcheck-errors.18.6 src.aclcheck-errors.10.23 src.rls-errors.18.6 src.rls-errors.10.23

Fixed 42501 branches also cover relation/schema/column ACL failures, owner-only checks, and row-level security WITH CHECK failures with different primaries.

Method: Read the fixed aclcheck_error tables and executor row-security error branches at REL_18_6 and REL_10_23.

Limits: These source paths are not additional runtime observations; the selected runtime remains the sequence ACL case.

src.aclcheck-errors.18.6 src.aclcheck-errors.10.23 src.rls-errors.18.6 src.rls-errors.10.23

The selected permission_denied_sequence case passed on isolated PostgreSQL 18.6 and 10.21 targets.

Method: Run the shared registry case and inspect structured diagnostics, state/recovery assertions, and cleanup.

Limits: This covers the selected case and targets only; it does not generalize to all drivers or intermediate releases.

manifest.42501 snippet-registry.42501

The locked catalogue records 42501; runtime scope is limited to the selected targets.

Method: Read generated facts and selected summaries.

Limits: Definition presence is not an exact behavioral introduction.

src.errcodes.18.6 src.calls.REL_18_6 src.calls.REL_10_23

Message templates

explicit ERROR · message.sequence-permission

Primary

permission denied for sequence %s
explicit ERROR · message.acl-relation-schema-owner

Primary

permission denied for relation %s

Primary

permission denied for schema %s

Primary

permission denied for column "%s" of relation "%s"

Primary

must be owner of relation %s

Primary

must be owner of schema %s

Generic ACL and owner-only source branches; no concrete runtime value is claimed.

explicit ERROR · message.rls-with-check

Primary

new row violates row-level security policy "%s" for table "%s"

Primary

new row violates row-level security policy for table "%s"

Primary

new row violates row-level security policy "%s" (USING expression) for table "%s"

Primary

new row violates row-level security policy (USING expression) for table "%s"

Fixed executor WITH CHECK branches; no concrete RLS runtime is claimed.

Reproduction & repair cases

permission_denied_sequence · PG 10, 18

Preconditions

  • A runner-owned disposable target is provisioned.
  • The runner can create a login role and grant schema/table privileges.

Trigger: Create a sequence, grant schema usage but no sequence privilege, then call nextval as a separate role.

Expected assertions

  • SQLSTATE is 42501 with sequence privilege diagnostic
  • The denied role session remains IDLE
  • Granting sequence USAGE permits nextval and returns the first value

Repair: Grant only the required sequence privilege or change the owning role policy, then retry nextval on the same role session.

Cleanup: Drop the case schema and temporary role with an owner connection.

Recorded runtime evidence

18.6 (Homebrew) · passed

Run: 42501-syntax-latest-test2

{
  "role": "u42501_d61b41bcce",
  "repair": {
    "value": 1,
    "status": "IDLE",
    "granted_sequence_privilege": "USAGE"
  },
  "diagnostic": {
    "text": "permission denied for sequence permission_sequence",
    "context": null,
    "severity": "ERROR",
    "sqlstate": "42501",
    "table_name": null,
    "column_name": null,
    "schema_name": null,
    "source_file": "sequence.c",
    "source_line": "655",
    "message_hint": null,
    "datatype_name": null,
    "exception_type": "InsufficientPrivilege",
    "internal_query": null,
    "message_detail": null,
    "constraint_name": null,
    "message_primary": "permission denied for sequence permission_sequence",
    "source_function": "nextval_internal",
    "internal_position": null,
    "statement_position": null,
    "severity_nonlocalized": "ERROR"
  },
  "denied_status": "IDLE",
  "connection_autocommit": true
}
10.21 (Debian 10.21-1.pgdg90+1) · passed

Run: 42501-syntax-pg10

{
  "role": "u42501_523bc1d1a0",
  "repair": {
    "value": 1,
    "status": "IDLE",
    "granted_sequence_privilege": "USAGE"
  },
  "diagnostic": {
    "text": "permission denied for sequence permission_sequence",
    "context": null,
    "severity": "ERROR",
    "sqlstate": "42501",
    "table_name": null,
    "column_name": null,
    "schema_name": null,
    "source_file": "sequence.c",
    "source_line": "600",
    "message_hint": null,
    "datatype_name": null,
    "exception_type": "InsufficientPrivilege",
    "internal_query": null,
    "message_detail": null,
    "constraint_name": null,
    "message_primary": "permission denied for sequence permission_sequence",
    "source_function": "nextval_internal",
    "internal_position": null,
    "statement_position": null,
    "severity_nonlocalized": "ERROR"
  },
  "denied_status": "IDLE",
  "connection_autocommit": true
}

Definition snapshot: english-manuals:775a94932e700508a244efae906… · English manual source