最近在學習PostgreSQL,看了用戶權限管理文檔,涉及到的知識點比較多,順便寫篇文章進行整理並不定時更新,也方便本身後續進行查閱。html
注意:建立好用戶(角色)以後須要鏈接的話,還須要修改2個權限控制的配置文件(pg_hba.conf、pg_ident.conf)。而且建立用戶(user)和建立角色(role)同樣,惟一的區別是用戶默承認以登陸,而建立的角色默認不能登陸。建立用戶和角色的各個參數選項是同樣的。sql
Tip:安裝PostgreSQL會自動建立一個postgres用戶,須要切換到該用戶下訪問PostgreSQL。數據庫
CREATE USER/ROLE name [ [ WITH ] option [ ... ] ] : 關鍵詞 USER,ROLE; name 用戶或角色名; where option can be: SUPERUSER | NOSUPERUSER :超級權限,擁有全部權限,默認nosuperuser。 | CREATEDB | NOCREATEDB :建庫權限,默認nocreatedb。 | CREATEROLE | NOCREATEROLE :建角色權限,擁有建立、修改、刪除角色,默認nocreaterole。 | INHERIT | NOINHERIT :繼承權限,能夠把除superuser權限繼承給其餘用戶/角色,默認inherit。 | LOGIN | NOLOGIN :登陸權限,做爲鏈接的用戶,默認nologin,除非是create user(默認登陸)。 | REPLICATION | NOREPLICATION :複製權限,用於物理或則邏輯複製(複製和刪除slots),默認是noreplication。 | BYPASSRLS | NOBYPASSRLS :安全策略RLS權限,默認nobypassrls。
| CONNECTION LIMIT connlimit :限制用戶併發數,默認-1,不限制。正常鏈接會受限制,後臺鏈接和prepared事務不受限制。 | [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL :設置密碼,密碼僅用於有login屬性的用戶,不使用密碼身份驗證,則能夠省略此選項。能夠選擇將空密碼顯式寫爲PASSWORD NULL。
加密方法由配置參數password_encryption肯定,密碼始終以加密方式存儲在系統目錄中。 | VALID UNTIL 'timestamp' :密碼有效期時間,不設置則用不失效。 | IN ROLE role_name [, ...] :新角色將當即添加爲新成員。 | IN GROUP role_name [, ...] :同上 | ROLE role_name [, ...] :ROLE子句列出一個或多個現有角色,這些角色自動添加爲新角色的成員。 (這實際上使新角色成爲「組」)。 | ADMIN role_name [, ...] :與ROLE相似,但命名角色將添加到新角色WITH ADMIN OPTION,使他們有權將此角色的成員資格授予其餘人。 | USER role_name [, ...] :同上 | SYSID uid :被忽略,可是爲向後兼容性而存在。
示例:安全
建立不須要密碼登錄的用戶zjy:服務器
postgres=# CREATE ROLE zjy LOGIN; CREATE ROLE
建立該用戶後,還不能直接登陸。須要修改 pg_hba.conf 文件(後面會對該文件進行說明),加入:
網絡
①:本地登錄:local all all trust
②:遠程登錄:host all all 192.168.163.132/32 trust併發
建立須要密碼登錄的用戶zjy1:
dom
postgres=# CREATE USER zjy1 WITH PASSWORD 'zjy1'; CREATE ROLE
和ROLE的區別是:USER帶LOGIN屬性。也須要修改 pg_hba.conf 文件(後面會對該文件進行說明),加入:
host all all 192.168.163.132/32 md5
ide
建立有時間限制的用戶zjy2:
函數
postgres=# CREATE ROLE zjy2 WITH LOGIN PASSWORD 'zjy2' VALID UNTIL '2019-05-30'; CREATE ROLE
和2的處理方法同樣,修改 pg_hba.conf 文件,該用戶會的密碼在給定的時間以後過時不可用。
建立有建立數據庫和管理角色權限的用戶admin:
postgres=# CREATE ROLE admin WITH CREATEDB CREATEROLE; CREATE ROLE
注意:擁有建立數據庫,角色的用戶,也能夠刪除和修改這些對象。
postgres=# CREATE ROLE admin WITH SUPERUSER LOGIN PASSWORD 'admin'; CREATE ROLE
建立複製帳號:repl
postgres=# CREATE USER repl REPLICATION LOGIN ENCRYPTED PASSWORD 'repl'; CREATE ROLE
建立複製用戶 CREATE USER abc REPLICATION LOGIN ENCRYPTED PASSWORD ''; CREATE USER abc REPLICATION LOGIN ENCRYPTED PASSWORD 'abc'; ALTER USER work WITH ENCRYPTED password ''; 建立scheme 角色 CREATE ROLE abc; CREATE DATABASE abc WITH OWNER abc ENCODING UTF8 TEMPLATE template0; \c abc 建立schema CREATE SCHEMA abc; ALTER SCHEMA abc OWNER to abc; revoke create on schema public from public; 建立用戶 create user abc with ENCRYPTED password ''; GRANT abc to abc; ALTER ROLE abc WITH abc; ##建立讀寫帳號 CREATE ROLE abc_rw; CREATE ROLE abc_rr; ##賦予訪問數據庫權限,schema權限 grant connect ON DATABASE abc to abc_rw; GRANT USAGE ON SCHEMA abc TO abc_rw; ##賦予讀寫權限 grant select,insert,update,delete ON ALL TABLES IN SCHEMA abc to abc; 賦予序列權限 GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA abc to abc; 賦予默認權限 ALTER DEFAULT PRIVILEGES IN SCHEMA abc GRANT select,insert,update,delete ON TABLES TO abc; 賦予序列權限 ALTER DEFAULT PRIVILEGES IN SCHEMA abc GRANT ALL PRIVILEGES ON SEQUENCES TO abc; #用戶對db要有鏈接權限 grant connect ON DATABASE abc to abc; #用戶要對schema usage 權限,否則要select * from schema_name.table ,不能用搜索路徑 GRANT USAGE ON SCHEMA abc TO abc; grant select ON ALL TABLES IN SCHEMA abc to abc; ALTER DEFAULT PRIVILEGES IN SCHEMA abc GRANT select ON TABLES TO abc; create user abc_w with ENCRYPTED password ''; create user abc_r with ENCRYPTED password ''; GRANT abc_rw to abc_w; GRANT abc_rr to abc_r;
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER } [, ...] | ALL [ PRIVILEGES ] } ON { [ TABLE ] table_name [, ...] | ALL TABLES IN SCHEMA schema_name [, ...] } TO role_specification [, ...] [ WITH GRANT OPTION ]
##單表受權:受權zjy帳號能夠訪問schema爲zjy的zjy表
grant select,insert,update,delete on zjy.zjy to zjy;
##全部表受權:
grant select,insert,update,delete on all tables in schema zjy to zjy;
GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] ) [, ...] | ALL [ PRIVILEGES ] ( column_name [, ...] ) } ON [ TABLE ] table_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##列受權,受權指定列(zjy schema下的zjy表的name列)的更新權限給zjy用戶
grant update (name) on zjy.zjy to zjy;
##指定列授不一樣權限,zjy schema下的zjy表,查看更新name、age字段,插入name字段
grant select (name,age),update (name,age),insert(name) on zjy.xxx to zjy;
GRANT { { USAGE | SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON { SEQUENCE sequence_name [, ...] | ALL SEQUENCES IN SCHEMA schema_name [, ...] } TO role_specification [, ...] [ WITH GRANT OPTION ]
##序列(自增鍵)屬性受權,指定zjy schema下的seq_id_seq 給zjy用戶
grant select,update on sequence zjy.seq_id_seq to zjy;
##序列(自增鍵)屬性受權,給用戶zjy受權zjy schema下的全部序列
grant select,update on all sequences in schema zjy to zjy;
GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] } ON DATABASE database_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##鏈接數據庫權限,受權cc用戶鏈接數據庫zjy
grant connect on database zjy to cc;
GRANT { USAGE | ALL [ PRIVILEGES ] } ON DOMAIN domain_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##
GRANT { USAGE | ALL [ PRIVILEGES ] } ON FOREIGN DATA WRAPPER fdw_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##
GRANT { USAGE | ALL [ PRIVILEGES ] } ON FOREIGN SERVER server_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##
GRANT { EXECUTE | ALL [ PRIVILEGES ] } ON { { FUNCTION | PROCEDURE | ROUTINE } routine_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...] | ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...] } TO role_specification [, ...] [ WITH GRANT OPTION ]
##
GRANT { USAGE | ALL [ PRIVILEGES ] } ON LANGUAGE lang_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##
GRANT { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON LARGE OBJECT loid [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##
GRANT { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] } ON SCHEMA schema_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ]
##鏈接schema權限,受權cc訪問zjy schema權限
grant usage on schema zjy to cc;
GRANT { CREATE | ALL [ PRIVILEGES ] } ON TABLESPACE tablespace_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ] GRANT { USAGE | ALL [ PRIVILEGES ] } ON TYPE type_name [, ...] TO role_specification [, ...] [ WITH GRANT OPTION ] where role_specification can be: [ GROUP ] role_name | PUBLIC | CURRENT_USER | SESSION_USER GRANT role_name [, ...] TO role_name [, ...] [ WITH ADMIN OPTION ]
##把zjy用戶的權限授予用戶cc。
grant zjy to cc;
權限說明:
SELECT:容許從指定表,視圖或序列的任何列或列出的特定列進行SELECT。也容許使用COPY TO。在UPDATE或DELETE中引用現有列值也須要此權限。對於序列,此權限還容許使用currval函數。對於大對象,此權限容許讀取對象。
INSERT:容許將新行INSERT到指定的表中。若是列出了特定列,則只能在INSERT命令中爲這些列分配(所以其餘列將接收默認值)。也容許COPY FROM。
UPDATE:容許更新指定表的任何列或列出的特定列,須要SELECT權限。
DELETE:容許刪除指定表中的行,須要SELECT權限。
TRUNCATE:容許在指定的表上建立觸發器。
REFERENCES:容許建立引用指定表或表的指定列的外鍵約束。
TRIGGER:容許在指定的表上建立觸發器。
CREATE:對於數據庫,容許在數據庫中建立新的schema、table、index。
CONNECT:容許用戶鏈接到指定的數據庫。在鏈接啓動時檢查此權限。
TEMPORARY、TEMP:容許在使用指定數據庫時建立臨時表。
EXECUTE:容許使用指定的函數或過程以及在函數。
USAGE:對於schema,容許訪問指定模式中包含的對象;對於sequence,容許使用currval和nextval函數。對於類型和域,容許在建立表,函數和其餘模式對象時使用類型或域。
ALL PRIVILEGES:一次授予全部可用權限。
REVOKE [ GRANT OPTION FOR ] { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER } [, ...] | ALL [ PRIVILEGES ] } ON { [ TABLE ] table_name [, ...] | ALL TABLES IN SCHEMA schema_name [, ...] } FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##移除用戶zjy在schema zjy上全部表的select權限
revoke select on all tables in schema zjy from zjy;
REVOKE [ GRANT OPTION FOR ] { { SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] ) [, ...] | ALL [ PRIVILEGES ] ( column_name [, ...] ) } ON [ TABLE ] table_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##移除用戶zjy在zjy schema的zjy表的age列的查詢權限
revoke select (age) on zjy.zjy from zjy;
REVOKE [ GRANT OPTION FOR ] { { USAGE | SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON { SEQUENCE sequence_name [, ...] | ALL SEQUENCES IN SCHEMA schema_name [, ...] } FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##序列
REVOKE [ GRANT OPTION FOR ] { { CREATE | CONNECT | TEMPORARY | TEMP } [, ...] | ALL [ PRIVILEGES ] } ON DATABASE database_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##庫
REVOKE [ GRANT OPTION FOR ] { USAGE | ALL [ PRIVILEGES ] } ON DOMAIN domain_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT]
##
REVOKE [ GRANT OPTION FOR ] { USAGE | ALL [ PRIVILEGES ] } ON FOREIGN DATA WRAPPER fdw_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT]
## REVOKE [ GRANT OPTION FOR ] { USAGE | ALL [ PRIVILEGES ] } ON FOREIGN SERVER server_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT]
##
REVOKE [ GRANT OPTION FOR ] { EXECUTE | ALL [ PRIVILEGES ] } ON { { FUNCTION | PROCEDURE | ROUTINE } function_name [ ( [ [ argmode ] [ arg_name ] arg_type [, ...] ] ) ] [, ...] | ALL { FUNCTIONS | PROCEDURES | ROUTINES } IN SCHEMA schema_name [, ...] } FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##
REVOKE [ GRANT OPTION FOR ] { USAGE | ALL [ PRIVILEGES ] } ON LANGUAGE lang_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##
REVOKE [ GRANT OPTION FOR ] { { SELECT | UPDATE } [, ...] | ALL [ PRIVILEGES ] } ON LARGE OBJECT loid [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##
REVOKE [ GRANT OPTION FOR ] { { CREATE | USAGE } [, ...] | ALL [ PRIVILEGES ] } ON SCHEMA schema_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##schena權限
REVOKE [ GRANT OPTION FOR ] { CREATE | ALL [ PRIVILEGES ] } ON TABLESPACE tablespace_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##
REVOKE [ GRANT OPTION FOR ] { USAGE | ALL [ PRIVILEGES ] } ON TYPE type_name [, ...] FROM { [ GROUP ] role_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]
##
REVOKE [ ADMIN OPTION FOR ] role_name [, ...] FROM role_name [, ...] [ CASCADE | RESTRICT ]
##
##移除全部用戶(public),superuser除外,對指定DB下的public schema的create 權限。 zjy=# revoke create on schema public from public; REVOKE
ALTER USER role_specification [ WITH ] option [ ... ] where option can be: SUPERUSER | NOSUPERUSER | CREATEDB | NOCREATEDB | CREATEROLE | NOCREATEROLE | INHERIT | NOINHERIT | LOGIN | NOLOGIN | REPLICATION | NOREPLICATION | BYPASSRLS | NOBYPASSRLS | CONNECTION LIMIT connlimit | [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL | VALID UNTIL 'timestamp' ALTER USER name RENAME TO new_name ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT } ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET configuration_parameter ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET ALL where role_specification can be: role_name | CURRENT_USER | SESSION_USER
示例:
注意:option選項裏的用戶均可以經過alter role進行修改
修改用戶爲超級/非超級用戶
alter role caocao with superuser/nosuperuser;
alter role caocao with nologin/login;
alter role caocao rename to youxing;
alter role youxing with password 'youxing';
alter role zjy in database zjy SET geqo to 0/default;
local database user auth-method [auth-options] host database user address auth-method [auth-options] hostssl database user address auth-method [auth-options] hostnossl database user address auth-method [auth-options] host database user IP-address IP-mask auth-method [auth-options] hostssl database user IP-address IP-mask auth-method [auth-options] hostnossl database user IP-address IP-mask auth-method [auth-options]
local:匹配使用Unix域套接字的鏈接,若是沒有此類型的記錄,則不容許使用Unix域套接字鏈接。
host:匹配使用TCP/IP進行的鏈接,主機記錄匹配SSL或非SSL鏈接,須要配置listen_addresses。
hostssl:匹配使用TCP/IP進行的鏈接,僅限於使用SSL加密進行鏈接,須要配置ssl參數。
hostnossl:匹配經過TCP/IP進行的鏈接,不使用SSL的鏈接。
database:匹配的數據庫名稱,all指定它匹配全部數據庫。若是請求的數據庫與請求的用戶具備相同的名稱則可使用samerole值。複製(replication)不指定數據庫,多個數據庫能夠用逗號分隔。
user:匹配的數據庫用戶名,值all指定它匹配全部用戶。 能夠經過用逗號分隔來提供多個用戶名。
address:匹配的客戶端計算機地址,能夠包含主機名,IP地址範圍。如:172.20.143.89/3二、172.20.143.0/2四、10.6.0.0/1六、:: 1/128。 0.0.0.0/0表示全部IPv4地址,:: 0/0表示全部IPv6地址。要指定單個主機,請使用掩碼長度32(對於IPv4)或128(對於IPv6)。all以匹配任何IP地址。
IP-address、IP-mask:這兩個字段可用做IP地址/掩碼長度,如:127.0.0.1 255.255.255.255。
auth-method:指定鏈接與此記錄匹配時要使用的身份驗證方法:trust、reject、scram-sha-25六、md五、password、gss、sspi、ident、peer、ldap、radius、cert、pam、bsd。
trust:容許無條件鏈接,容許任何PostgreSQL用戶身份登陸,而無需密碼或任何其餘身份驗證。 reject:拒絕任何條件鏈接,這對於從組中「過濾掉」某些主機很是有用。 scram-sha-256:執行SCRAM-SHA-256身份驗證以驗證用戶的密碼。 md5:執行SCRAM-SHA-256或MD5身份驗證以驗證用戶的密碼。 password:要提供未加密的密碼以進行身份驗證。因爲密碼是經過網絡以明文形式發送的,所以不該在不受信任的網絡上使用。 gss:使用GSSAPI對用戶進行身份驗證,這僅適用於TCP / IP鏈接。 sspi:使用SSPI對用戶進行身份驗證,這僅適用於Windows。 ident:經過聯繫客戶端上的ident服務器獲取客戶端的操做系統用戶名,並檢查它是否與請求的數據庫用戶名匹配。 Ident身份驗證只能用於TCP / IP鏈接。爲本地鏈接指定時,將使用對等身份驗證。 peer:從操做系統獲取客戶端的操做系統用戶名,並檢查它是否與請求的數據庫用戶名匹配。這僅適用於本地鏈接。 ldap:使用LDAP服務器進行身份驗證。 radius:使用RADIUS服務器進行身份驗證。 cert:使用SSL客戶端證書進行身份驗證。 pam:使用操做系統提供的可插入身份驗證模塊(PAM)服務進行身份驗證。 bsd:使用操做系統提供的BSD身份驗證服務進行身份驗證。
auth-options:在auth-method字段以後,能夠存在name = value形式的字段,用於指定認證方法的選項。
例子:
# TYPE DATABASE USER ADDRESS METHOD local all all trust --在本地容許任何用戶無密碼登陸 local all all peer --操做系統的登陸用戶和pg的用戶是否一致,一致則能夠登陸 local all all ident --操做系統的登陸用戶和pg的用戶是否一致,一致則能夠登陸 host all all 192.168.163.0/24 md5 --指定客戶端IP訪問經過md5身份驗證進行登陸 host all all 192.168.163.132/32 password --指定客戶端IP經過passwotd身份驗證進行登陸 host all all 192.168.54.1/32 reject host all all 192.168.0.0/16 ident host all all 127.0.0.1 255.255.255.255 trust ...
設置完以後能夠經過查看錶來查看hba:
zjy=# select * from pg_hba_file_rules; line_number | type | database | user_name | address | netmask | auth_method | options | error -------------+-------+---------------+-----------+---------------+-----------------------------------------+-------------+---------+------- 87 | host | {all} | {all} | 192.168.163.0 | 255.255.255.0 | md5 | | 92 | local | {all} | {all} | | | peer | | 94 | host | {all} | {all} | 127.0.0.1 | 255.255.255.255 | md5 | | 96 | host | {all} | {all} | ::1 | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | md5 | | 99 | local | {replication} | {all} | | | peer | | 100 | host | {replication} | {all} | 127.0.0.1 | 255.255.255.255 | md5 | | 101 | host | {replication} | {all} | ::1 | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | md5 | |
固然,修改完pg_hba.conf文件以後,須要從新加載配置,不用重啓數據庫:
postgres=# select pg_reload_conf(); pg_reload_conf ---------------- t
用戶權限管理涉及到的東西不少,本文也只是大體說明了一小部分,大部分的還得繼續學習。那麼如今按照一個正常項目上線的流程來建立一個應用帳號爲例,看看須要怎麼操做。
好比一個項目zjy上線:用管理帳號來操做
postgres=# create database zjy; CREATE DATABASE
postgres=# create user zjy with password 'zjy'; CREATE ROLE
postgres=# \c zjy You are now connected to database "zjy" as user "postgres". zjy=# create schema zjy; CREATE SCHEMA
#訪問庫 zjy=# grant connect on database zjy to zjy; GRANT #訪問schmea zjy=# grant usage on schema zjy to zjy; GRANT #訪問表 zjy=# grant select,insert,update,delete on all tables in schema zjy to zjy; GRANT #若是訪問自增序列,須要受權 zjy=# grant select,update on all sequences in schema zjy to zjy; GRANT 注意:上面的受權只對歷史的一些對象受權,後期增長的對象是沒有權限的,須要給個默認權限 #默認表權限 zjy=# ALTER DEFAULT PRIVILEGES IN SCHEMA zjy GRANT select,insert,update,delete ON TABLES TO zjy; ALTER DEFAULT PRIVILEGES #默認自增序列權限 zjy=# ALTER DEFAULT PRIVILEGES IN SCHEMA zjy GRANT select,update ON sequences TO zjy; ALTER DEFAULT PRIVILEGES
zjy=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- admin | Superuser, Cannot login | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} zjy | | {} zjy=# select * from pg_roles; rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig | oid ----------------------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+------- pg_signal_backend | f | t | f | f | f | f | -1 | ******** | | f | | 4200 postgres | t | t | t | t | t | t | -1 | ******** | | t | | 10 admin | t | t | f | f | f | f | -1 | ******** | | f | | 16456 pg_read_all_stats | f | t | f | f | f | f | -1 | ******** | | f | | 3375 zjy | f | t | f | f | t | f | -1 | ******** | | f | | 16729 pg_monitor | f | t | f | f | f | f | -1 | ******** | | f | | 3373 pg_read_all_settings | f | t | f | f | f | f | -1 | ******** | | f | | 3374 pg_stat_scan_tables | f | t | f | f | f | f | -1 | ******** | | f | | 3377 (8 rows)
zjy=# select * from information_schema.table_privileges where grantee='zjy'; grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy ----------+---------+---------------+--------------+------------+----------------+--------------+---------------- postgres | zjy | zjy | zjy | zjy | INSERT | NO | NO postgres | zjy | zjy | zjy | zjy | SELECT | NO | YES postgres | zjy | zjy | zjy | zjy | UPDATE | NO | NO postgres | zjy | zjy | zjy | zjy | DELETE | NO | NO postgres | zjy | zjy | zjy | zjy1 | INSERT | NO | NO postgres | zjy | zjy | zjy | zjy1 | SELECT | NO | YES postgres | zjy | zjy | zjy | zjy1 | UPDATE | NO | NO postgres | zjy | zjy | zjy | zjy1 | DELETE | NO | NO postgres | zjy | zjy | zjy | zjy2 | INSERT | NO | NO postgres | zjy | zjy | zjy | zjy2 | SELECT | NO | YES postgres | zjy | zjy | zjy | zjy2 | UPDATE | NO | NO postgres | zjy | zjy | zjy | zjy2 | DELETE | NO | NO postgres | zjy | zjy | zjy | zjy3 | INSERT | NO | NO postgres | zjy | zjy | zjy | zjy3 | SELECT | NO | YES postgres | zjy | zjy | zjy | zjy3 | UPDATE | NO | NO postgres | zjy | zjy | zjy | zjy3 | DELETE | NO | NO
關於用戶權限管理的知識點不少,如role相關member、set role以及pg_ident.conf文件等等,本文說明介紹有限,後面用到就繼續更新該博文。你們仍是多多查閱PostgreSQL的官方文檔。