postgresql影子用户实践场景分析( 三 )

  
 7788 | test  | ANALYST  | 7566 | 1982-12-09 | 3000.00 |     |   20  
 7876 | ADAMS | CLERK   | 7788 | 1983-01-12 | 1100.00 |     |   20  
 1111 | SMITH | CLERK   | 7902 | 1980-12-17 | 800.00 |     |   20  
(14 rows)  
   
影子用户创建  
如果想让readonly只读用户不在每次 postgres用户在test模式中创建新表后都要手工赋予 grant select on all tables in schema test TO readonly 权限 。则需要授予对test默认的访问权限,对于test模式新创建的也生效 。  
session 1:未来访问test模式下所有新建的表赋权,创建 t5 表 。  
postgres=# alter default privileges in schema test grant select on tables to readonly ;  
ALTER DEFAULT PRIVILEGES  
postgres=# create table test.t5 as select * from test.emp;  
CREATE TABLE  
   
session 2:查询readonly用户  
postgres=> select * from test.t5;  
 empno | ename |  job  | mgr | hiredate |  sal  | comm  | deptno   
——-+——–+———–+——+————+———+———+——–  
 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 |   30  
 7521 | WARD  | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 |   30  
 7566 | JONES | MANAGER  | 7839 | 1981-04-02 | 2975.00 |     |   20  
 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 |   30  
 7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 | 2850.00 |     |   30  
 7782 | CLARK | MANAGER  | 7839 | 1981-06-09 | 2450.00 |     |   10  
 7839 | KING  | PRESIDENT |   | 1981-11-17 | 5000.00 |     |   10  
 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 |  0.00 |   30  
 7900 | JAMES | CLERK   | 7698 | 1981-12-03 | 950.00 |     |   30  
 7902 | FORD  | ANALYST  | 7566 | 1981-12-03 | 3000.00 |     |   20  
 7934 | MILLER | CLERK   | 7782 | 1982-01-23 | 1300.00 |     |   10  
 7788 | test  | ANALYST  | 7566 | 1982-12-09 | 3000.00 |     |   20  
 7876 | ADAMS | CLERK   | 7788 | 1983-01-12 | 1100.00 |     |   20  
 1111 | SMITH | CLERK   | 7902 | 1980-12-17 | 800.00 |     |   20  
(14 rows)  
   
总结:影子用户创建的步骤  
–创建影子用户  
create user readonly with password ‘postgres’;  
–将schema中usage权限赋予给readonly用户,访问所有已存在的表  
grant usage on schema test to readonly;  
grant select on all tables in schema test to readonly;  
–未来访问test模式下所有新建的表  
alter default privileges in schema test grant select on tables to readonly ;  
文章来源:脚本之家  
来源地址:https://www.jb51.net/article/207011.htm  
申请创业报道,分享创业好点子 。,共同探讨创业新机遇!  


特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。