Postgres 序列(sequence)的权限

这个报错表示当前数据库用户对某个序列(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;

改动前记得先确认一下表的现状,避免影响已有数据。

📌 排查思路

  1. ‌先看报错里具体是哪个序列 ‌,比如 xxx_id_seq。
  2. ‌确认当前执行操作的用户是谁 ‌,用 SELECT current_user; 查看。
  3. ‌给该用户授予对应序列的 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?

相关推荐
这个DBA有点耶3 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G3 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备4 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远4 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
geovindu4 天前
sql: Transaction & Concurrency Patterns using postgresql 18
postgresql·数据库开发·数据库架构
2601_962218614 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
张洛闻Eren4 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
于平安4 天前
MySQL-触发器
数据库·mysql
白远山4 天前
上海24小时自助健身房解决方案实战指南与经验分享
java·数据库·架构·需求分析
Omics Pro4 天前
斯坦福Nature+Science|广义虚拟细胞基础大模型
数据库·人工智能·算法·机器学习·自然语言处理