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?

相关推荐
Decay__7 分钟前
PostgreSQL 用户权限与安全 —— 角色体系、GRANT 授权、pg_hba.conf 认证、行级安全与审计
运维·数据库·sql·postgresql
Escalating_xu1 小时前
【MySQL】索引原理(上):从数据页到 B+ 树,弄懂聚簇索引、回表与覆盖索引
数据库·mysql
DataY轻量数据平台2 小时前
MongoDB 怎么做 CDC?不用 Debezium,Change Streams 就是它的“binlog”
数据库·mongodb
djjjx.2 小时前
Mysql数据库基础篇(十):从磁盘 IO 到 B+ 树:图解 MySQL 索引的底层演进、Page 结构、目录机制与 IO 优化原理
数据库·mysql
En^_^Joy3 小时前
LangChain消息系统全解析:从基础到实战(四)
数据库·microsoft·langchain
CoderYanger3 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第三弹):统一数据返回格式+统一异常处理
java·数据库·spring boot·后端·spring·java-ee·mybatis
飞奔的瓜牛3 小时前
运维工程师 Redis 日常巡检清单与实战注意事项
运维·数据库·redis
龙腾AI白云3 小时前
视觉运动控制与灵巧抓取:具身智能手眼协调的学习
大数据·数据库·人工智能·机器学习·pygame
多弗朗皮卡丘13 小时前
MySQL表的约束
数据库·mysql
Flynn Liang17 小时前
AI Agent Redis 生产踩坑实录:13 类故障的根因、复现与调优
数据库·人工智能·redis·缓存·ai agent·故障分析