Postgres permission denied for sequence: GRANT USAGE and nextval
Sequences carry their own ACL, so table grants never cover nextval. The USAGE grant, default privileges, and why identity columns behave differently.

Introduction & Quick Summary
Symptom. An application role can SELECT, INSERT and UPDATE its tables, but every INSERT into a table with a serial primary key fails with ERROR: permission denied for sequence orders_id_seq. Reads work, updates work, the application user is not postgres, and \dp orders shows exactly the grants you issued. Sometimes the error appears only in production, right after a schema migration created a new table.
Cause. In PostgreSQL a sequence is an independent object with its own ACL. GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public does not touch sequences, so nextval('orders_id_seq') — called implicitly by the serial column default — is checked against the sequence's ACL and refused. Nothing about the table grant implies a sequence grant.
Fix. Grant USAGE (which covers nextval and currval) plus SELECT on the sequence to the application role, then make it automatic for future sequences with OWNED BY and ALTER DEFAULT PRIVILEGES. Check with has_sequence_privilege(), which shows the effective privilege including group membership and inherited roles.
Core Mechanics, Signatures & Reproducible Setup
Sequence privileges in PostgreSQL are three, and they are checked by different functions:
USAGE— required bynextval()andcurrval().SELECT— required to readlast_value(and by some tools that inspect the sequence).UPDATE— required bysetval().
A reproducer that shows the exact failure, run as a superuser in one session:
CREATE ROLE app_user LOGIN PASSWORD 'secret';
CREATE TABLE orders (
id serial PRIMARY KEY,
total numeric NOT NULL
);
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT, INSERT, UPDATE ON orders TO app_user;SET ROLE app_user;
INSERT INTO orders (total) VALUES (10);
-- ERROR: permission denied for sequence orders_id_seqThe fix, back as superuser or the table owner:
GRANT USAGE, SELECT ON SEQUENCE orders_id_seq TO app_user;
-- make it automatic for every sequence this role creates later
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_user;
-- and for sequences that already exist
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO app_user;ALTER DEFAULT PRIVILEGES applies to objects created by the role that runs it (or by the role named in FOR ROLE), in that schema, from that moment on. A migration that runs as postgres therefore needs FOR ROLE postgres, or the grant must be issued by postgres itself.
Key Comparison Table
The grant that fixes the table does not fix the sequence, and the object type decides the privilege name.
| Object type | Privilege checked | By which operation | GRANT ALL ON ALL TABLES covers it? |
|---|---|---|---|
| Table | SELECT / INSERT / UPDATE | SELECT, INSERT, UPDATE | Yes |
| Sequence | USAGE | nextval, currval | No |
| Sequence | UPDATE | setval | No |
| Sequence | SELECT | last_value reads | No |
| Schema | USAGE | Resolving any name in it | No |
| View | SELECT on the view, plus underlying | SELECT | Yes for the view only |
serial and identity columns behave differently, which is the source of most confusion:
| Column definition | How the default runs | Sequence privilege needed | Notes |
|---|---|---|---|
serial / bigserial | Column default calls nextval(...) as the invoking user | USAGE on the sequence | ACL is checked; this is the error case |
GENERATED BY DEFAULT AS IDENTITY | Internal nextval attached to the table | Not checked separately; table INSERT suffices | Preferred for new schemas |
GENERATED ALWAYS AS IDENTITY | Same, rejects explicit values without OVERRIDING SYSTEM VALUE | Not checked separately | Strongest default |
| Application-managed id | Your SQL calls nextval() directly | USAGE, or wrap in a SECURITY DEFINER function | Explicit grants belong in the migration |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Which sequence is the default actually using
The error names a sequence, but a restored schema can have a default pointing at a differently named one, or at a sequence in another schema:
SELECT pg_get_serial_sequence('public.orders', 'id') AS seq;
SELECT d.column_default
FROM information_schema.columns d
WHERE d.table_schema = 'public' AND d.table_name = 'orders' AND d.column_name = 'id';pg_get_serial_sequence() returns the qualified name to grant on, which avoids guessing when \d orders is unavailable (for example through a pooler that restricts meta-commands).
2. Verifying the grant the way PostgreSQL checks it
\dp orders_id_seq shows the ACL, but it does not show inheritance or PUBLIC grants. has_sequence_privilege() answers the question the server actually asks:
SELECT has_sequence_privilege('app_user', 'public.orders_id_seq', 'USAGE') AS can_nextval,
has_schema_privilege('app_user', 'public', 'USAGE') AS can_resolve,
pg_get_userbyid(relowner) AS owner
FROM pg_class
WHERE oid = 'public.orders_id_seq'::regclass;If can_nextval is true and the insert still fails, look at can_resolve and at the table's RLS policies instead of the sequence.
3. Migrations that create objects under another role
When migrations run as postgres and the application connects as app_user, default privileges must be declared for the migrating role:
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO app_user;
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;Run this in the same database that later migrations target. A staging restore that omits pg_dumpall --roles keeps the tables but loses the role-level defaults, so the first new sequence after the restore breaks production and staging differently.
4. Restores and --no-acl
pg_dump includes ACL entries by default; --no-acl omits them, and a dump restored into a different owner produces tables that app_user can read (through PUBLIC defaults on some providers) but not insert into. Compare the ACL after a restore rather than the data:
SELECT c.relname, c.relkind, pg_get_userbyid(c.relowner) AS owner,
coalesce(array_to_string(c.relacl, ' '), '(default)') AS acl
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public' AND c.relkind = 'S'
ORDER BY c.relname;A blank ACL on a sequence means "owner only", which is exactly the state that produces the error for every other role.
5. Sequences reached through a non-default search_path
nextval('orders_id_seq') resolves through search_path. If the sequence lives in app while the session's search_path is public, the call fails with relation "orders_id_seq" does not exist before permissions are even considered. Qualify it in the default, and grant on the qualified name:
ALTER TABLE app.orders ALTER COLUMN id
SET DEFAULT nextval('app.orders_id_seq');
GRANT USAGE, SELECT ON SEQUENCE app.orders_id_seq TO app_user;
GRANT USAGE ON SCHEMA app TO app_user;Poolers that reset search_path per transaction (PgBouncer in transaction mode, most serverless Postgres proxies) make this the more common failure after a deployment.
6. setval in seed scripts
Seed and fixture scripts commonly call setval() to align a sequence with hand-inserted ids. That requires UPDATE on the sequence, which USAGE does not include:
GRANT UPDATE ON SEQUENCE orders_id_seq TO app_user; -- only for seeding roles
SELECT setval('orders_id_seq', (SELECT max(id) FROM orders));Keep this grant on a migration role, not on the runtime role; a runtime user that can setval can also create duplicate-key storms.
Common Compilation/Runtime Mistakes & Version Gotchas
GRANT ALL ON ALL TABLESas a blanket fix. It covers tables, views and foreign tables in the schema — never sequences. Sequelize, Prisma and Django all emitserial/bigserialin older migrations, so this remains a live failure in 2026.- Granting on the table and expecting
nextvalto inherit. A column default is an expression evaluated with the invoking user's privileges unless it is inside aSECURITY DEFINERfunction. ALTER DEFAULT PRIVILEGESwithoutFOR ROLE. It only affects objects the executing role creates. This is the usual reason "the grant is there but the new table still fails".- Forgetting
USAGEon the schema. Sequence ownership is not enough if the role cannot resolve the schema name; the error then changes topermission denied for schema public. PUBLICgrants that hide the problem locally. Managed providers sometimes grant broad privileges onpublicfor convenience; the same migration then fails on a self-hosted instance wherePUBLIChas nothing.GRANT ... ON ALL SEQUENCES IN SCHEMAbefore the sequence exists. It is not retroactive. Run it afterCREATE TABLEin the same migration, or rely on default privileges.- Identity columns do not need the grant, and templates still add it. Grants on identity-backed sequences are harmless but they document nothing; preferring
GENERATED BY DEFAULT AS IDENTITYin new migrations removes this class of error entirely.
Related tools. Paste the failing migration into the SQL formatter when a multi-statement grant block is hard to read, and confirm the ordering — schema usage, table grants, then sequence grants — before rerunning it; a single misplaced semicolon in a DO $$ … $$ block hides which statement actually executed.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesPostgres RLS 42P17: infinite recursion detected in policy
A policy on members that reads members re-enters itself. One security definer function with a pinned search_path ends the recursion and leaves the rest of the policy intact.
POSTGRES_PASSWORD Is Ignored: Docker Volume Init Rules
The official image reads POSTGRES_PASSWORD once, at initdb. Why changed credentials and users do nothing on an existing volume, and how to rotate safely.
PgBouncer Transaction vs Session Pooling: What Breaks
Port 6543 hands out a server connection per transaction, so prepared statements, SET, LISTEN and cursors stop behaving. Driver-level settings for each stack.
Zero-Downtime Postgres Migrations: lock_timeout and NOT VALID
One ALTER TABLE waiting behind a slow SELECT blocks every query that arrives after it. The timeout-retry loop, NOT VALID constraints and CONCURRENTLY index rules.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.