溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Case: 一個read-only 角色對某個schema下的新建的表,無需單獨授權,直接擁有只讀權

發(fā)布時間:2020-07-20 13:48:42 來源:網(wǎng)絡 閱讀:1153 作者:Darren_Chen 欄目:數(shù)據(jù)庫

Case: 一個read-only 角色對某個schema下的新建的表,無需單獨授權,直接擁有只讀權限

首先,你要知道PostgreSQL默認schema下新建的表,對于一個普通用戶不會有select的權限的,所以我需要修改PostgreSQl默認的權限,把select提前賦予普通用戶,需要使用到alter default privileges這個命令


Session1 (user:postgres):

#psql

create role role1 password '123456' login;

create schema schema1;

grant usage on schema schema1 to role1;

testdb=# \dn+ schema1;

                     List of schemas

  Name   |  Owner   |  Access privileges   | Description

---------+----------+----------------------+-------------

schema1 | postgres | postgres=UC/postgres+|

         |          | role1=U/postgres     |


alter default privileges in schema schema1 grant select on tables to role1;


testdb=# \ddp+

           Default access privileges

  Owner   | Schema  | Type  | Access privileges

----------+---------+-------+-------------------

postgres | schema1 | table | role1=r/postgres


create table schema1.t1(c1 int,c2 varchar(10));

insert into schema1.t1 values(1,'aaa');


testdb=# \dp+

                                 Access privileges

Schema  | Name | Type  |     Access privileges     | Column privileges | Policies

---------+------+-------+---------------------------+-------------------+----------

schema1 | t1   | table | postgres=arwdDxt/postgres+|                   |

         |      |       | role1=r/postgres          |                   |


Session2 read_only:


#psql -h 172.16.101.54 -p 5432 testdb role1

testdb=> select * from schema1.t1;

c1 | c2  

----+-----

  1 | aaa

(1 row)


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI