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?

相关推荐
Henry-SAP1 分钟前
SAP PP中的核心业务流程——离散制造、重复制造、流程制造三大模式解析
前端·数据库·人工智能·制造
Pniubi9 分钟前
mysql 一张表可以有多少列
数据库·mysql
阡陌数智26 分钟前
Extended Thinking 深度拆解:Claude Opus 5.5 推理范式、Token 计费陷阱与 Agent 工程落地
数据库
skywalk81631 小时前
不用 SSH、不装数据库:我用 1 个 Python 脚本 + Git,把 15 个 AI Agent 串成了一条流水线
数据库·人工智能·python·ssh·集群
Decay__1 小时前
19_MySQL 告警与自动化运维 —— 告警分级收敛 / Webhook / 定时巡检 / 脚本库
运维·数据库·sql·mysql·架构
代码不停1 小时前
Spring Aop
java·数据库·spring
keyipatience2 小时前
MySQL 学习(4)常用数据类型
数据库·mysql
达梦产品与服务2 小时前
SQLark V4.2 更新 | 新增TiDB、SSL安全连接、数据字典、清空/截断表等40+项功能已上线!
数据库·安全·tidb·ssl
码农阿豪2 小时前
SQL Server数据迁移完成后,凌晨的数据库告警该怎么查?
数据库·sqlserver
bjzhang752 小时前
sqlsugar入门
数据库·sqlsugar