PostgreSQL14中预定义角色pg_write_all_data和pg_read_all_data
2022-05-09 21:32 abce 阅读(626) 评论(0) 编辑 收藏 举报场景用例:
两个数据库database1、database2;
database1有两个schema,database2有一个schema;
每个schema都属于不同的user;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | database database1: schema schema1: owner: user1 table table1: columns: - id: integer - data: text table table2: columns: - id: integer - data: text schema schema2: database database1: schema schema1: owner: user1 table table1: columns: - id: integer - data: text table table2: columns: - id: integer - data: text schema schema2: owner: user2 table table1: columns: - id: integer - data: text database database2: schema schema1: owner: user1 table table1: columns: - id: integer - data: text owner: user2 table table1: columns: - id: integer - data: text database database2: schema schema1: owner: user1 table table1: columns: - id: integer - data: text |
假设有个用户appuser需要访问所有的表。
在PostgreSQL13中:
首先创建user:
1 2 3 4 | pg13-database1 postgres =# create user appuser; CREATE ROLE pg13-database1 postgres =# alter user appuser password 'secretsuperpasswd' ; ALTER ROLE |
如果我们使用新创建的用户登录,并尝试访问schema1中的表:
1 2 3 4 5 6 7 8 9 | pg13-database1 user1 => \c database1 appuser Password for user appuser: psql (14.1, server 13.5 (Debian 13.5-1.pgdg110+1)) You are now connected to database "database1" as user "appuser" . pg13-database1 user1 => select * from schema1.table1 limit 5; ERROR: permission denied for schema schema1 LINE 1: select * from schema1.table1 limit 5; ^ |
没有权限。那我们来给apuser授与usage权限:
1 2 | Pg13-database1 postgres =# grant usage on schema schema1 to appuser; GRANT |
再次尝试查看表:
1 2 | pg13-database1 appuser => select * from schema1.table1 limit 5; ERROR: permission denied for table table1 |
再来,授予select权限:
1 2 3 4 5 6 7 8 9 10 11 12 | pg13-database1 user1 => grant select on schema1.table1 to appuser; GRANT pg13-database1 appuser => select * from schema1.table1 limit 5; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (5 rows ) |
再来尝试访问相同schema中的其它表:
1 2 | pg13-database1 appuser => select * from schema1.table2 limit 5; ERROR: permission denied for table table2 |
我们授与访问所有表的select权限:
1 2 3 4 5 6 7 8 9 10 11 12 | pg13-database1 user1 => grant select on all tables in schema schema1 to appuser; GRANT pg13-database1 appuser => select * from schema1.table2 limit 5 ; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (5 rows ) |
成功了!
假如,我们发现需要新增加一个表呢?
1 2 3 4 5 6 | pg13-database1 user1 => create table schema1.table3 ( like schema1.table1) ; CREATE TABLE pg13-database1 user1 => insert into schema1.table3(id, data) select i,i::text from generate_series(1,1000) i; INSERT 0 1000 |
如果这时候使用appuser来查看表table3(之前已经授予了select所有表的权限)
1 2 | pg13-database1 appuser => select * from schema1.table3 limit 5; ERROR: permission denied for table table3 |
还是会报没有权限。如果想查看新增加的表,还得继续按照之前的操作执行一次授权。
为了让我们的用户即使在新表也能保持读取访问权限,我们必须更改schema的默认权限:
1 2 | pg13-database1 user1 => alter default privileges in schema schema1 grant select on tables to appuser; ALTER DEFAULT PRIVILEGES |
现在可以查看新加的表了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | pg13-database1 user1 => create table schema1.table4 ( like schema1.table1) ; CREATE TABLE pg13-database1 user1 => insert into schema1.table4(id, data) select i,i::text from generate_series(1,1000) i; INSERT 0 1000 pg13-database1 appuser => select * from schema1.table4 limit 5 ; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (5 rows ) |
但是别忘了,还有别的schema,appuser也要能访问。
1 2 | pg13-database1 appuser => select * from schema2.table1 limit 5; ERROR: permission denied for schema schema2 |
同样,也还要按照上面的操作,再次执行一次。
别忘了,还有其它的数据库呢!
1 2 3 4 5 6 | pg13-database1 appuser => \c database2 appuser psql (14.1, server 13.5 (Debian 13.5-1.pgdg110+1)) You are now connected to database "database2" as user "appuser" . pg13-database2 appuser => select * from schema1.table1 limit 5; ERROR: permission denied for schema schema1 |
我们,还是要按照上面的操作,再次执行一次操作!!!
在postgresql14中,提供预先定义的角色:
角色 | 允许访问的范围 |
---|---|
pg_read_all_data | 读所有的数据(表、视图、序列等),即在这些对象上有select权限,在所有schemas上有usage权限;该角色不包含BYPASSRLS设置的属性; |
pg_write_all_data | 写所有的数据(表、视图、序列等),即在这些对象上有update、insert、delete权限,在所有schemas上有usage权限;该角色不包含BYPASSRLS设置的属性; |
现在,来看看在postgresql 14中是如何完成上面的场景用例的。
先创建用户
1 2 3 4 | pg14-database1 postgres =# create user appuser; CREATE ROLE pg14-database1 postgres =# alter user appuser password 'supersecretpasswd' ; ALTER ROLE |
如果,这个时候就想访问表,肯定会报错:
1 2 3 4 5 6 7 | pg14-database1 user1 => \c database1 appuser Password for user appuser: You are now connected to database "database1" as user "appuser" . pg14-database1 appuser => select * from schema1.table1 limit 5; ERROR: permission denied for schema schema1 LINE 1: select * from schema1.table1 limit 5 ; |
神奇之旅开始,我们可以使用postgresql 14中预先定义的角色:
1 2 | pg14-database1 postgres =# grant pg_read_all_data to appuser ; GRANT ROLE |
1 2 3 4 5 6 7 8 9 | pg14-database1 appuser => select * from schema1.table1 limit 5 ; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (5 rows ) |
即使访问其它的schema也没有问题:
1 2 3 4 5 6 7 8 9 | pg14-database1 appuser => select * from schema2.table1 limit 5 ; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (5 rows ) |
现在,我们新加一个表:
1 2 3 4 5 6 | pg14-database1 user1 => create table schema1.table3 ( like schema1.table1) ; CREATE TABLE pg14-database1 user1 => insert into schema1.table3(id, data) database1-> select i,i::text from generate_series(1,1000000) i; INSERT 0 1000000 |
仍然可以查询:
1 2 3 4 5 6 7 8 9 | pg14-database1 appuser => select * from schema1.table3 limit 5; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (10 rows ) |
如果是其它数据库呢:
1 2 3 4 5 6 7 8 9 10 11 12 | pg14-database1 appuser => \c database2 appuser You are now connected to database "database2" as user "appuser" . pg14-database2 appuser => select * from schema1.table1 limit 5; id | data ----+------ 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 (5 rows ) |
还是可以访问。
pg_write_all_data的用户类似。
不过,只是直接授予DML权限,或pg_write_all_data角色,而没有read权限,用户不能对数据的子集执行update、delete操作,因为这两个操作需要过滤数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | pg14-database1 write_appuser => \dg write_appuser List of roles Role name | Attributes | Member of ----------------+------------+--------------------- write_appuser | | {pg_write_all_data} pg14-database1 write_appuser => insert into schema1.table1(id, data) values (2000000, 'database1.schema1.table1-2000000' ); INSERT 0 1 pg14-database1 write_appuser => update schema1.table1 set data = 'database1.schema1.table1-2000000-upd' where id = 2000000 ; ERROR: permission denied for table table1 pg14-database1 write_appuser => delete from schema1.table1 where id = 2000000 ; ERROR: permission denied for table table1 pg14-database1 write_appuser => update schema1.table1 set data = '' ; UPDATE 1000001 pg14-database1 write_appuser => delete from schema1.table1; DELETE 1000001 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2021-05-09 日志文件不存在,导致归档进程启动失败