How to fix Permission denied for sequence in PostgreSQL
This error occurs when a database user lacks permissions to access a sequence object. Sequences are separate database objects from tables, so granting table permissions does not automatically grant sequence access. The fix involves explicitly granting USAGE and UPDATE privileges on the sequence.
Quick fix
Fixes Permission denied for sequence
psql -U postgres -d your_database_name- Connect to the database as a superuser
- Identify the sequence causing the error
- Grant USAGE privilege on the specific sequence
- Grant permissions on all sequences in a schema at once
psql -U postgres -d your_database_namePermission denied for sequence
What this error means
In PostgreSQL, sequences are special single-row table objects that generate unique identifier values. When a table uses a SERIAL column or a DEFAULT nextval() expression, PostgreSQL internally manages a sequence to generate new IDs. This error occurs when a user tries to insert data into a table with an auto-incrementing column but lacks the necessary privileges (USAGE, SELECT, or UPDATE) on the underlying sequence. Unlike table permissions, sequence permissions must be granted separately and explicitly—they are not inherited from table permissions.
Typical symptoms
- INSERT queries fail with "Permission denied for sequence" error when trying to add rows to tables with SERIAL columns
- Application cannot insert data into tables with auto-increment IDs even though SELECT and INSERT permissions are granted on the table
- pg_dump fails with sequence permission errors when backing up the database
- NEXTVAL() function calls fail with insufficient privilege errors
- Error messages include the specific sequence name (e.g., "user_id_seq")
Common causes
- User does not have USAGE privilege on the sequence object
- User does not have UPDATE or SELECT privilege on the sequence needed for nextval() function
- Sequence permissions were not granted when the table permissions were configured
- SERIAL column was created without properly setting up sequence grants for the application user
- Database user was migrated or restored without preserving original sequence permission grants
- Azure Database for PostgreSQL user lacks privileges on sequences in shared system schemas
How to fix "Permission denied for sequence"
Open a terminal and connect to your PostgreSQL database as the postgres superuser:
psql -U postgres -d your_database_nameOr if using a remote connection:
psql -h your_host -U postgres -d your_database_nameFrom the error message, note the exact sequence name (e.g., "user_id_seq"). You can also list all sequences in a schema:
\ds public.*This shows all sequences in the public schema. Replace "public" with your schema name if needed.
Grant the necessary privileges to your application user. Replace "sequence_name" with your actual sequence name and "app_user" with your database user:
GRANT USAGE, SELECT, UPDATE ON SEQUENCE sequence_name TO app_user;Breakdown of privileges:
- USAGE: Allows use of currval() and nextval() functions
- SELECT: Allows reading the current sequence value
- UPDATE: Required for nextval() to advance the sequence
If your application user needs access to multiple sequences, grant permissions on all sequences in a schema in one command:
GRANT USAGE, SELECT, UPDATE ON ALL SEQUENCES IN SCHEMA public TO app_user;Replace "public" with your actual schema name. This approach is often cleaner than granting on individual sequences.
To ensure new sequences automatically get the correct permissions, set default privileges:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT, UPDATE ON SEQUENCES TO app_user;This ensures any sequences created in the future will have the proper permissions for app_user.
Test that the user can now insert data into the affected table:
SET ROLE app_user;
INSERT INTO your_table (name) VALUES ('test');
SET ROLE postgres;If the INSERT succeeds, permissions are correctly configured. If it still fails, ensure you have granted permissions on all affected sequences.
Advanced notes
By default, PUBLIC has no privileges on sequences in PostgreSQL. When using the azure_pg_admin role in Azure Database for PostgreSQL, ensure your application user has appropriate permissions. If pg_dump fails, it may be dumping system schemas where your user lacks permissions—dump specific schemas instead with the "-n schema_name" flag. In multi-schema applications, remember that each sequence belongs to a specific schema and requires separate grants. The difference between SERIAL (auto-increment) and BIGSERIAL columns does not affect sequence permission requirements—both need explicit grants.
Frequently asked questions
- What does "Permission denied for sequence" mean?
- In PostgreSQL, sequences are special single-row table objects that generate unique identifier values. When a table uses a SERIAL column or a DEFAULT nextval() expression, PostgreSQL internally manages a sequence to generate new IDs. This error occurs when a user tries to insert data into a table with an auto-incrementing column but lacks the necessary privileges (USAGE, SELECT, or UPDATE) on the underlying sequence.
- How do I fix "Permission denied for sequence"?
- This error occurs when a database user lacks permissions to access a sequence object. Sequences are separate database objects from tables, so granting table permissions does not automatically grant sequence access. The fix involves explicitly granting USAGE and UPDATE privileges on the sequence.
- What causes "Permission denied for sequence"?
- User does not have USAGE privilege on the sequence object User does not have UPDATE or SELECT privilege on the sequence needed for nextval() function Sequence permissions were not granted when the table permissions were configured
- Is the "Permission denied for sequence" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚PostgreSQL Official Documentation
- 📖PostgreSQL Documentation - GRANT Command(Official Docs)
- 📖PostgreSQL Documentation - Error Codes Appendix(Official Docs)