您好,登錄后才能下訂單哦!
由于開發(fā)提出需求:
(1)在多個PostgreSQL的instacne上面創(chuàng)建一個readonly用戶,僅對數(shù)據(jù)庫里面的表或者視圖(包括物化視圖)有僅有select權(quán)限,并且對以后新建的表和視圖也要有select權(quán)限,我們知道PostgreSQL
在schema下新建的表,對于一個已經(jīng)存在的用戶不會自動賦予select的權(quán)限的,需要我們使用grant select...手動去執(zhí)行,這樣就比較麻煩,總不能每次新建表,我們就賦權(quán)一次,要知道我們有很多實例,總不能把時間都放在這樣沒有 意義的事情上,再說了我們也不能時刻監(jiān)督有哪些PostgreSQL有新建的表,所以我們需要對未來的表提前授權(quán),查看了一下PostgreSQL網(wǎng)站,發(fā)現(xiàn)命令alter default privileges...可以實現(xiàn)這個功能.
(2)alter default privileges沒有對已經(jīng)存在的表和視圖授權(quán)的功能,所以想要對現(xiàn)在和未來的對象都賦權(quán),還需要使用grant select對現(xiàn)有的表賦權(quán).
(3)由于需要執(zhí)行的db/schenma非常多,一條一條命令執(zhí)行的話不太現(xiàn)實,需要編寫腳本,批量執(zhí)行.
(4)具體如何實現(xiàn)可以參考測試過程:
https://blog.51cto.com/darrenmemos/2086198
腳本如下:
#!/bin/ksh -x # ########################################################################### # Name: postgreSQL_grant_readonly_privileges.sh # Location: # Function: PostgreSQL grant readonly privileges # Author: # Create Date: # update Date: ############################################################################# /usr/local/pgsql/bin/psql -d postgres -q -t -c "select datname from pg_catalog.pg_database where datname not in('postgres','template1','template0');" | grep -v "^$" > /tmpb_list.log while read db_name do /usr/local/pgsql/bin/psql -d ${db_name} -q -t -c "select schema_name from information_schema.schemata where schema_name not in pg_catalog','information_schema','pg_toast','pg_temp_1','pg_toast_temp_1');" | grep -v "^$" > /tmp/schema_list.log while read schema_name do /usr/local/pgsql/bin/psql -d ${db_name} -q -t -c "grant select on all tables in schema ${schema_name} to readonly;" /usr/local/pgsql/bin/psql -d ${db_name} -q -t -c "grant usage on schema ${schema_name} to readonly;" /usr/local/pgsql/bin/psql -d ${db_name} -q -t -c "alter default privileges in schema ${schema_name} grant select on tables to readonly;" done < /tmp/schema_list.log done < /tmp/db_list.log exit 0
然后就可以在服務(wù)器上批量執(zhí)行了。
參考鏈接:
https://www.postgresql.org/docs/9.3/static/sql-grant.html
https://www.postgresql.org/docs/9.4/static/sql-alterdefaultprivileges.html
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。