1、数据库当前拥有的schema

postgres=# \dn+List of schemasName    |  Owner   |  Access privileges   |      Description
-----------+----------+----------------------+------------------------my_schema | postgres | postgres=UC/postgres+| |          | scott=UC/postgres   +| |          | abc=U/postgres      +| public    | postgres | postgres=UC/postgres+| standard public schema|          | =UC/postgres         | u1        | postgres | postgres=UC/postgres+| |          | test1=U/postgres     | u2        | postgres |                      |
(4 rows)postgres=# 

2、创建一个新角色并赋予使用my_schema的权限

postgres=# create role aa login password 'aa';
CREATE ROLE
postgres=#
postgres=#
postgres=# grant USAGE on SCHEMA my_schema to aa;
GRANT
postgres=#
postgres=# 

3、my_schema角色下面有一张T表

postgres=> \c - postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \dtList of relationsSchema | Name | Type  |  Owner
--------+------+-------+----------public | t    | table | postgrespublic | test | table | postgres
(2 rows)postgres=# set search_path=my_schema;
SET
postgres=# \dtList of relationsSchema   | Name | Type  |  Owner
-----------+------+-------+----------my_schema | t    | table | postgres
(1 row)

4、切换到aa角色,并设置search_path

postgres=# \c - aa
You are now connected to database "postgres" as user "aa".
postgres=> set search_path=my_schema;
SET
postgres=>
postgres=> \dtList of relationsSchema   | Name | Type  |  Owner
-----------+------+-------+----------my_schema | t    | table | postgres
(1 row)

5、aa角色查询my_schema下面的表

postgres=> select * from my_schema.t;
ERROR:  permission denied for table t
postgres=>
postgres=> 

6、切换到postgres用户授权t表的select权限给aa角色

postgres=> \c - postgres
You are now connected to database "postgres" as user "postgres".
postgres=# grant select on my_schema.t to aa;
GRANT
postgres=# \c - aa
You are now connected to database "postgres" as user "aa".
postgres=> select * from my_schema.t;id
----20
(1 row)postgres=> 

7、postgres提供了角色可以默认使用其他角色的权限的功能,看下面测试

切换到postgres用户创建一张新表test1

postgres=> \c - postgres
You are now connected to database "postgres" as user "postgres".
postgres=#
postgres=# select * from pg_default_acl ;defaclrole | defaclnamespace | defaclobjtype |                   defaclacl
------------+-----------------+---------------+-----------------------------------------------10 |           24696 | r             | {scott=arwdDxt/postgres,abc=arwdDxt/postgres}
(1 row)postgres=# create table my_schema.test1(id int);
CREATE TABLE
postgres=#

8、如果这个时候不执行grant select on my_schema.test1 to aa;是没有权限访问的

postgres=> \c - aa
You are now connected to database "postgres" as user "aa".
postgres=>
postgres=>
postgres=> select * from my_schema.test1;
ERROR:  permission denied for table test1
postgres=>
postgres=> select * from my_schema.t;id
----20
(1 row)

9、设置aa角色继承postgres权限

postgres=> \c - postgres
You are now connected to database "postgres" as user "postgres".
postgres=# ALTER DEFAULT PRIVILEGES for role postgres in schema my_schema grant select on tables to aa;
ALTER DEFAULT PRIVILEGE

10、再次访问test1表

postgres=# \c - aa
You are now connected to database "postgres" as user "aa".
postgres=> select * from pg_default_acl ;defaclrole | defaclnamespace | defaclobjtype |                          defaclacl
------------+-----------------+---------------+-------------------------------------------------------------10 |           24696 | r             | {scott=arwdDxt/postgres,abc=arwdDxt/postgres,aa=r/postgres}
(1 row)postgres=> select *from my_schema.test1;
ERROR:  permission denied for table test1
postgres=> 

其中pg_default_acl 视图的defaclacl列就显示了类似的关系,比如 aa=r/postgres。角色aa继承了对postgres角色中namespace OID=24696的schema的读表权限。如果是读写权限则为aa=rw/postgres

postgres=# ALTER DEFAULT PRIVILEGES for role postgres in schema my_schema grant update on tables to aa;
ALTER DEFAULT PRIVILEGES
postgres=#
postgres=# select * from pg_default_acl ;
-[ RECORD 1 ]---+-------------------------------------------------------------
defaclrole      | 10
defaclnamespace | 24696
defaclobjtype   | r
defaclacl       | {scott=arwdDxt/postgres,abc=arwdDxt/postgres,aa=rw/postgres}
postgres=> select oid,  * from pg_namespace;oid  |      nspname       | nspowner |                                nspacl
-------+--------------------+----------+-----------------------------------------------------------------------99 | pg_toast           |       10 | 11804 | pg_temp_1          |       10 | 11805 | pg_toast_temp_1    |       10 | 11 | pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}2200 | public             |       10 | {postgres=UC/postgres,=UC/postgres}12941 | information_schema |       10 | {postgres=UC/postgres,=U/postgres}24714 | u2                 |       10 | 24713 | u1                 |       10 | {postgres=UC/postgres,test1=U/postgres}24696 | my_schema          |       10 | {postgres=UC/postgres,scott=UC/postgres,abc=U/postgres,aa=U/postgres}
(9 rows)postgres=> 

11、如果postgres新建一张表,该表的select权限自己会赋予aa角色

postgres=# create table my_schema.test2(id int);
CREATE TABLE
postgres=# \c - aa
You are now connected to database "postgres" as user "aa".
postgres=> select *from my_schema.test2;id
----
(0 rows)

通过步骤10、11可以看到,设置ALTER DEFAULT PRIVILEGES时对之前已经存在的表是不起作用的,只有之后创建的表的权限才能被授权成功。

postgres中schema访问权限设置相关推荐

  1. Nagios 请检查HTTP服务器关于该CGI的访问权限设置

    无权查看任何主机的信息. 请检查HTTP服务器关于该CGI的访问权限设置. 搜索了一下方法 确保 htpasswd.user的所有组为nagios 解决办法: vi /usr/local/nagios ...

  2. 局域网服务器文件夹隐藏,局域网服务器共享文件夹访问权限设置win7隐藏无权限共享文件夹方法.docx...

    局域网服务器共享文件夹访问权限设置win7隐藏无权限共享文件夹方法 ??局域网服务器共享文件夹访问权限设置.win7隐藏无权限共享文件夹的方法在局域网文件管理中,文件服务器共享文件的管理一直是当前网络 ...

  3. 服务器文件权限在哪里,服务器中的文件权限设置在哪里

    服务器中的文件权限设置在哪里 内容精选 换一换 对象存储服务OBS是华为云提供的稳定.安全.高效.易用的云存储服务,具备标准Restful API接口,可存储任意数量和形式的非结构化数据.弹性文件服务 ...

  4. 计算机如何授权访问局域网,局域网权限怎么设置?局域网访问权限设置软件哪个好...

    当前,很多企事业单位的局域网都配置了文件服务器,用于共享单位的一些文件供局域网用户访问.同时,员工工作当中形成的劳动成果.重要文件等也常常存储在文件服务器上,便于文件保存.协同工作等需要.这使得,企业 ...

  5. 服务器禁止访问文件,服务器文件夹访问权限设置 控制访问权限方法

    在企业局域网中,我们经常需要共享文件供局域网用户访问使用,但是为了保证共享文件的安全,我们也需要设置共享文件的访问权限,防止访问共享文件的用户越权访问共享文件的行为.那么,具体如何实现呢?可以通过以下 ...

  6. fso 拒绝访问_windows 服务器安全之磁盘访问权限设置[完整篇]

    asp.net服务器安全之磁盘访问权限设置 硬盘或文件夹: C:\ D:\ E:\ F:\ 类推 主要权限部分: 其他权限部分: Administrators 完全控制 无 如果安装了其他运行环境,比 ...

  7. java访问glusterfs_GlusterFS: 访问权限设置

    GlusterFS: 访问权限设置 GlusterFS支持基于POSIX Access Control Lists的访问权限设置,在Ubuntu或者类似的系统上,只需要使用setfacl或getfac ...

  8. glusterfs java_GlusterFS: 访问权限设置

    GlusterFS: 访问权限设置 GlusterFS支持基于POSIX Access Control Lists的访问权限设置,在Ubuntu或者类似的系统上,只需要使用setfacl或getfac ...

  9. arcgis server地图服务访问权限设置及arcgis server token获取。

    一.地图服务访问权限设置. 当我们发布arcgis地图服务后,由于涉密或者其他原因,不想让所有人看到地图服务.这时候我们可以对地图设置权限.怎末设置那? 进入你们arcgis管理.地址: http:/ ...

最新文章

  1. 从Delphi 7升级到Delphi XE
  2. 【android】如何让WebView对Video标签的支持更强力
  3. PyCharm安装开发游戏的Pygame
  4. CodeForces 1361E James and the Chase(dfs + 结论)
  5. LeetCode 1465. 切割后面积最大的蛋糕
  6. sender分析之创建请求
  7. linux vnc 安装目录,Linux环境VNC服务安装、配置与使用(图)
  8. dns、网关、IP地址,主要是配置resolv.conf\network\ifcfg-eth0
  9. 华为模拟器ensp与真实机器连接使用的配置内容一
  10. Linux下高效实用的grep命令
  11. Excel VBA 宏编程入门
  12. 某互联网大厂亿级大数据服务平台的建设和实践
  13. 2020年12月31日flash禁用后网页如何播放rtmp视频流
  14. C语言——关于位段的理解
  15. No current assignment for partition 解决
  16. Qt 样式表之QSS
  17. cocos creator制作游戏实战-Flak Cannon(二)
  18. Open3D Voxelization 体素化
  19. Shell 快捷键助你一臂之力
  20. Android 学习笔记之九 下拉刷新

热门文章

  1. java 接收命令行参数_java中的命令行参数_Java中的命令行参数
  2. 二叉树链式结构的实现
  3. MATLAB中多行注释以及取消的快捷键
  4. android ondestory,android – onDestroy()究竟是什么毁灭?
  5. UWB到底是什么技术?
  6. Nginx设置登录验证
  7. 一张图片放两个二维码_这6个宝藏网站,为你提供用不完的免费素材图片!
  8. delphi 国腾GTICR100 二代身份证的读取
  9. Redis 布隆过滤器实战「缓存击穿、雪崩效应」
  10. 【docker】解决 Failed with result ‘exit-code‘和Start request repeated too quickly和Failed to start Docker