Learning center

Find the sequence privilege behind a denied PostgreSQL insert

If an insert error names a sequence, inspect the default expression that supplies the column. A table INSERT grant does not by itself authorize a nextval call on a separate sequence.

For example, a fictional writer may insert into reporting.export_jobs. The id column's default explicitly invokes nextval on reporting.export_jobs_id_seq. After inspecting the default, an operator can query the role’s sequence privileges without advancing the sequence.

SELECT has_sequence_privilege('export_writer',
                              'reporting.export_jobs_id_seq', 'USAGE') AS usage,
       has_sequence_privilege('export_writer',
                              'reporting.export_jobs_id_seq', 'UPDATE') AS can_update;

Either privilege suffices for nextval. If both are false and the default is the source of the error, review USAGE on the one sequence with its owner. Avoid calling nextval as a diagnostic. It advances the sequence and PostgreSQL does not reclaim the value on transaction rollback. Sequence manipulation functions · Privilege inquiry functions

Trace another denied request.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading