PostgreSQL對現有,建立的表和視圖授權給使用者

來源:互聯網
上載者:User

標籤:PostgreSQL

由於開發提出需求:

(1)在多個PostgreSQL的instacne上面建立一個readonly使用者,僅對資料庫裡面的表或者視圖(包括物化視圖)有僅有select許可權,並且對以後建立的表和視圖也要有select許可權,我們知道PostgreSQL

    在schema下建立的表,對於一個已經存在的使用者不會自動賦予select的許可權的,需要我們使用grant select...手動去執行,這樣就比較麻煩,總不能每次建立表,我們就賦權一次,要知道我們有很多執行個體,總不能把時間都放在這樣沒有       意義的事情上,再說了我們也不能時刻監督有哪些PostgreSQL有建立的表,所以我們需要對未來的表提前授權,查看了一下PostgreSQL網站,發現命令alter default privileges...可以實現這個功能.


(2)alter default privileges沒有對已經存在的表和視圖授權的功能,所以想要對現在和未來的對象都賦權,還需要使用grant select對現有的表賦權.


(3)由於需要執行的db/schenma非常多,一條一條命令執行的話不太現實,需要編寫指令碼,批量執行.


(4)具體如何?可以參考測試過程:

http://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.logwhile read db_namedo   /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.logdone < /tmp/db_list.logexit 0


然後就可以在伺服器上批量執行了。


參考連結:

https://www.postgresql.org/docs/9.3/static/sql-grant.html

https://www.postgresql.org/docs/9.4/static/sql-alterdefaultprivileges.html


PostgreSQL對現有,建立的表和視圖授權給使用者

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.