这个报错表示当前数据库用户对某个序列(sequence)没有使用权限。 在 PostgreSQL 中,即使有表的 INSERT 权限,如果表的主键是用 serial 或 bigserial 生成的,插入数据时还需要对应的序列权限,否则就会报这个错。
🔑 快速解决:给用户授予序列权限
直接执行授权命令,把 序列名 和 用户名 换成实际的:
sql
GRANT USAGE ON SEQUENCE 序列名 TO 用户名;
比如报错信息是 permission denied for sequence userinfo_uid_seq,就执行:
sql
GRANT USAGE ON SEQUENCE userinfo_uid_seq TO test;
如果还不够,也可以连 SELECT 权限一起给,有些场景下会用到:
sql
GRANT USAGE, SELECT ON SEQUENCE userinfo_uid_seq TO test;
⚠️ 两个容易忽略的坑
- 只授表权限不够 :很多人给用户授了表的
INSERT、SELECT权限,但忘了序列是独立对象,必须单独授权。 - Schema 权限也要有 :如果表在特定 schema 下(比如
api),还需要先给该 schema 的USAGE权限,否则可能报permission denied for schema之类的错。
sql
GRANT USAGE ON SCHEMA api TO test;
💡 如果表很多,怎么批量授权
如果库里有大量带序列的表,可以写个循环一次性搞定:
sql
DO $$ DECLARE r RECORD; BEGIN FOR r IN SELECT sequence_schema, sequence_name FROM information_schema.sequences WHERE sequence_schema = 'public' LOOP EXECUTE 'GRANT USAGE, SELECT ON SEQUENCE ' || quote_ident(r.sequence_schema) || '.' || quote_ident(r.sequence_name) || ' TO test'; END LOOP; END $$;
💡 更省事的替代方案
如果项目还在开发阶段,可以考虑把表的自增列从 serial 改成 identity。identity 列对权限的要求更简单,能少踩一些坑。
sql
ALTER TABLE events ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY;
改动前记得先确认一下表的现状,避免影响已有数据。
📌 排查思路
- 先看报错里具体是哪个序列 ,比如
xxx_id_seq。 - 确认当前执行操作的用户是谁 ,用
SELECT current_user;查看。 - 给该用户授予对应序列的
USAGE权限,再重新执行原来的操作。
This error occurs because your PostgreSQL database user has permission to insert data into a table, but lacks the required privileges to update the underlying auto-incrementing ID sequence (SERIAL or BIGSERIAL columns) attached to that table. [1](https://www.google.com/goto?url=CAESlQEB6zswFZ9BFzHM2cEE_XILePw22lahUxxI2Uq-d2vN9vaFONA38WbxfcwaYQcD69kYhbZPjJw5yTGqR7cuYo-9vMOjOCZBtyBYCOui10kyWz4ZYOYceIHL3Emygm0I5CtZtBPoZ2GhgwokziCDEv8o5_NYwvBTLr0BI22QueetovQsw-DGAlTAlvioNKrXxRMv9EnvCQ "1"), [2](https://www.google.com/goto?url=CAEShAEB6zswFRlV4PI11mA0vUTI9RrDnr-vUu33wIe9411XOiSCIiFO_rDNXZfkZm_yNAREFalgwzCG5jr_AWinKCbqSZohOJGJAy6CXMRX-gPtYJYwZW76nlUOlFsJeibkGhr6RDw51dsaZHRDfFLBdvU6ezBIraTX4c_Um2Vh4rI_190dxaQ "2")
Here is how you can quickly fix this issue by granting the necessary permissions.
Option 1: Fix a Single Sequence
If the error explicitly names a single sequence (e.g., permission denied for sequence users_id_seq ), run the following SQL command as a database administrator or superuser: [1](https://www.google.com/goto?url=CAESlQEB6zswFZ9BFzHM2cEE_XILePw22lahUxxI2Uq-d2vN9vaFONA38WbxfcwaYQcD69kYhbZPjJw5yTGqR7cuYo-9vMOjOCZBtyBYCOui10kyWz4ZYOYceIHL3Emygm0I5CtZtBPoZ2GhgwokziCDEv8o5_NYwvBTLr0BI22QueetovQsw-DGAlTAlvioNKrXxRMv9EnvCQ "1")
sql
GRANT USAGE, SELECT ON SEQUENCE sequence_name TO your_username;
请谨慎使用此类代码。
Replace sequence_name with the sequence from your error message, and your_username with the user getting the error. [1](https://www.google.com/goto?url=CAEScAHrOzAV4qsLlASLLeXoNXgAufLz-gcFzR3cTEhOSJgk0dt54a0SsxI56hA3rOZ8nbHr795cmIrpLc_F0gIf-u2O2OnXrIjS7Zx684niepKeWBTYYBFnirUKPhJihVBr7oarRmSGswkTuyxMISui_dg "1")
Option 2: Fix All Sequences in a Schema
If you have multiple tables or want to make sure this doesn't happen on other existing tables in your schema (usually public ), run this command: [1](https://www.google.com/goto?url=CAESpgEB6zswFVlkNGfTX6fOeRTJ1P804WPZJ4xMeBNFPZlKz_CVr7LKM5D4tPFzGeV8uSnBotBOJMT33giQUtGDTgPAKuB8T0VFZ6vn9SVf7YvmdbIQjePBfnve4EUHKzubXJgp_s4blv0H-ccnJlSt0rbnXyrPl8s430yXXzHVjh8MJ8o_zZ3-Rzew8FnqBC3LhQHgXseuLnwI2FNmE903SF-uSXsiqjEj "1")
sql
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO your_username;
请谨慎使用此类代码。
Option 3: Prevent the Issue for Future Tables
By default, PostgreSQL does not automatically grant permissions on new sequences to existing users. To fix this permanently so that any new tables and sequences automatically grant access, alter the default privileges: [1](https://www.google.com/goto?url=CAESkQEB6zswFTiaXdqKp4ua8_oTkoz1HM8hVVMNJZaaqq437ibR061971byZipphqm5udNTR1-FQrPfw48XtC6lN5rVDGY3103S_CZIZrByDp0jATm8tXAMaru6fW-8qmDfHaEvHBl1CfoQ8Naa7cUAP7haYAJ_JS8X5oa931jAs3XKee17RTq5B7pepjpG4bnMaEoz "1")
sql
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO your_username;
请谨慎使用此类代码。
Alternative: Ownership Fix
If the sequence is completely owned by the wrong user, you might need to change its owner to your current database user instead: [1](https://www.google.com/goto?url=CAESlQEB6zswFZ9BFzHM2cEE_XILePw22lahUxxI2Uq-d2vN9vaFONA38WbxfcwaYQcD69kYhbZPjJw5yTGqR7cuYo-9vMOjOCZBtyBYCOui10kyWz4ZYOYceIHL3Emygm0I5CtZtBPoZ2GhgwokziCDEv8o5_NYwvBTLr0BI22QueetovQsw-DGAlTAlvioNKrXxRMv9EnvCQ "1")
sql
ALTER SEQUENCE sequence_name OWNER TO your_username;
请谨慎使用此类代码。
Would you like to know how to check the current privileges of a specific sequence using psql , or do you need help identifying the user currently executing your application's queries?