序列的权限定义
官方文档中可以看到序列的所有权限为rwU,分别对应SELECT、UPDATE、USAGE;
对于序列,SELECT权限允许使用currval 函数
对于序列,UPDATE权限允许使用 nextval 和 setval 函数
对于序列,USAGE权限允许使用currval 和 nextval 函数
序列权限测试
序列seq_temp的owner为test用户,未给yhru用户赋权时,执行序列函数提示无权限
test_db=> \ds+ seq_temp
List of relations
Schema | Name | Type | Owner | Size | Description
--------+----------+----------+-------+------------+-------------
public | seq_temp | sequence | test | 8192 bytes |
(1 row)
test_db=> \c - yhru
You are now connected to database "test_db" as user "yhru".
test_db=> select currval('seq_temp');
ERROR: permission denied for sequence seq_temp
test_db=> select nextval('seq_temp');
ERROR: permission denied for sequence seq_temp
test_db=> select setval('seq_temp',100);
ERROR: permission denied for sequence seq_temp
test_db=>
复制
只赋权select时,可以执行currval函数,但是会报错,因为没有执行过nextval函数
test_db=> grant select on SEQUENCE seq_temp TO yhru ;
GRANT
test_db=> \dp+ seq_temp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+----------+----------+-------------------+-------------------+----------
public | seq_temp | sequence | test=rwU/test +| |
| | | yhru=r/test | |
(1 row)
test_db=> \c - yhru
You are now connected to database "test_db" as user "yhru".
test_db=> select currval('seq_temp');
ERROR: currval of sequence "seq_temp" is not yet defined in this session
test_db=>
复制
赋权select和update时,可以使用所有序列操作函数
test_db=> grant update on SEQUENCE seq_temp TO yhru ;
GRANT
test_db=> \c - yhru
You are now connected to database "test_db" as user "yhru".
test_db=> \dp+ seq_temp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+----------+----------+-------------------+-------------------+----------
public | seq_temp | sequence | test=rwU/test +| |
| | | yhru=rw/test | |
(1 row)
test_db=> select setval('seq_temp',10);
setval
--------
10
(1 row)
test_db=> select currval('seq_temp');
currval
---------
10
(1 row)
test_db=> select nextval('seq_temp');
nextval
---------
11
(1 row)
test_db=>
复制
只赋权usage时,nextval和currval可以使用,setval不可以使用
test_db=> grant usage on SEQUENCE seq_temp TO yhru ;
GRANT
test_db=> \c - yhru
You are now connected to database "test_db" as user "yhru".
test_db=> \dp+ seq_temp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+----------+----------+-------------------+-------------------+----------
public | seq_temp | sequence | test=rwU/test +| |
| | | yhru=U/test | |
(1 row)
test_db=> select nextval('seq_temp');
nextval
---------
12
(1 row)
test_db=> select currval('seq_temp');
currval
---------
12
(1 row)
test_db=> select setval('seq_temp',10);
ERROR: permission denied for sequence seq_temp
test_db=>
复制
结论
如果需要有修改序列的权限可以直接赋权ALL
如果只是使用序列不需要修改可以赋权USAGE
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
9.9 分高危漏洞,尽快升级到 pgAdmin 4 v9.2 进行修复
严少安
351次阅读
2025-04-11 10:43:23
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
328次阅读
2025-04-15 14:48:05
外国CTO也感兴趣的开源数据库项目——openHalo
小满未满、
322次阅读
2025-04-21 16:58:09
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
294次阅读
2025-04-07 12:14:29
postgresql+patroni+etcd高可用安装
necessary
161次阅读
2025-03-28 10:11:23
从 Oracle 到 PostgreSQL迁移成本评估揭秘
梧桐
150次阅读
2025-03-27 17:21:42
手把手教你在 openKylin 上部署 IvorySQL 4.4
严少安
150次阅读
2025-03-27 20:41:28
转发有奖 | PostgreSQL 16 PGCM高级认证课程直播班招生中!
墨天轮小教习
143次阅读
2025-04-14 15:58:34
墨天轮PostgreSQL认证证书快递已发(2025年3月批)
墨天轮小教习
125次阅读
2025-04-03 11:43:25
从Percona 发布Pro级产品得到的一些启发
库海无涯
123次阅读
2025-03-26 08:45:23