原本角色相關的單詞都是能夠小寫的,爲了表示重要性,本文中所有大寫。html
ROLE的概念sql
PostgreSQL經過Role的概念管理數據庫的訪問權限。數據庫
ROLE能夠擁有數據庫對象,如表、函數,也能夠GRANT相關權限給其餘ROLE。安全
Role能夠被當作User使用,也能夠當作Group使用,或者既是User也是Group,徹底看這個Role如何設置。session
PG 8.1版本以前還有User和Group的概念,以後都納入Role。函數
ROLE的分類:post
一、按數量,分類普通ROLE和GROUP測試
二、按權限大小,分爲:ROLE、USER、ADMIN、SUPERUSERui
GROUPspa
在GP系統的用戶管理中,一般會把多個ROLE賦予一個GROUP,這樣能夠對該GROUP中的ROLE作批量的權限操做。
在設置權限時只需給該組設置便可,撤銷權限時也是從該組撤消。在PostgreSQL中,首先須要建立一個表明組的角色,以後再將該角色的membership權限賦給獨立的用戶角色便可。
GROUP關係demo:
rathandb=# CREATE ROLE rathan_group_test; NOTICE: resource queue required -- using default resource queue "pg_default" CREATE ROLE rathandb=# CREATE ROLE rathan_group_member IN ROLE rathan_group_test INHERIT; NOTICE: resource queue required -- using default resource queue "pg_default" CREATE ROLE rathandb=# CREATE ROLE rathan_group_member2 IN ROLE rathan_group_test; NOTICE: resource queue required -- using default resource queue "pg_default" CREATE ROLE rathandb=# CREATE ROLE rathan_no_group_member; NOTICE: resource queue required -- using default resource queue "pg_default" CREATE ROLE rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description ------------------------+--------------+---------------------+------------- rathan_group_member | Cannot login | {rathan_group_test} | rathan_group_member2 | Cannot login | {rathan_group_test} | rathan_group_test | Cannot login | | rathan_no_group_member | Cannot login | | rathandb=# ALTER ROLE rathan_no_group_member IN ROLE rathan_group_test; ERROR: option "addroleto" not recognized (user.c:813) rathandb=# GRANT rathan_group_test TO rathan_no_group_member; GRANT ROLE List of roles Role name | Attributes | Member of | Description ------------------------+-------------------------+---------------------+------------- rathan_group_member | Cannot login | {rathan_group_test} | rathan_group_member2 | Cannot login | {rathan_group_test} | rathan_group_test | | | rathan_no_group_member | Cannot login | {rathan_group_test} | rathandb=# REVOKE rathan_group_test FROM rathan_no_group_member; rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description ------------------------+-------------------------+---------------------+------------- rathan_group_member | Cannot login | {rathan_group_test} | rathan_group_member2 | Cannot login | {rathan_group_test} | rathan_group_test | | | rathan_no_group_member | Cannot login | | rathandb=# DROP ROLE rathan_group_test; rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description ------------------------+-------------------------+---------------------+------------- rathan_group_member | Cannot login | | rathan_group_member2 | Cannot login | | rathan_no_group_member | Cannot login | |
LOGIN賦權操做:
rathandb=# ALTER ROLE rathan_group_test WITH LOGIN; ALTER ROLE rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description ------------------------+--------------+---------------------+------------- rathan_group_member | Cannot login | {rathan_group_test} | rathan_group_test | | | rathan_no_group_member | Cannot login | |
rathan_group_member繼承了rathan_group_test的權限,爲何仍是沒有login屬性?
由於角色屬性LOGIN、SUPERUSER和CREATEROLE被視爲特殊權限,它們不會像其它數據庫對象的普通權限那樣被繼承。
ROLE與USER:
USER表示帶有LOGIN屬性的ROLE。
CREATE ROLE建立的用戶默認不帶LOGIN屬性,而CREATE USER建立的用戶默認帶有LOGIN屬性。
LOGIN權限demo:
rathandb=# CREATE ROLE rathan_role_test; NOTICE: resource queue required -- using default resource queue "pg_default" CREATE ROLE rathandb=# CREATE USER rathan_user_test; NOTICE: resource queue required -- using default resource queue "pg_default" CREATE ROLE rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description -------------------+--------------+---------------------+------------- rathan_group_test | | | rathan_role_test | Cannot login | | rathan_user_test | | |
SUPERUSER
一、只有SUPERUSER能夠建立SUPERUSER,SUPERUSER能夠改寫全部的database限制。
二、數據庫的超級用戶擁有該數據庫的全部權限,爲了安全起見,咱們最好使用非超級用戶完成咱們的正常工做。和建立普通用戶不一樣,建立超級用戶必須是以超級用戶的身份執行如下命令:
rathandb=# CREATE ROLE rathan_normal_role; rathandb=# CREATE ROLE rathan_superuser WITH SUPERUSER; CREATE ROLE rathandb=# CREATE ROLE rathan_superuser2 WITH SUPERUSER LOGIN; CREATE ROLE rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description ------------------------+-------------------------+---------------------+------------- rathan_normal_role | Cannot login | | rathan_superuser | Superuser, Cannot login | | rathan_superuser2 | Superuser | |
ADMIN
能夠GRANT GROUP TO 其餘ROLE,或者REVOKE GROUP FROM 其餘ROLE。
ADMIN權限demo:
rathandb=# SET SESSION AUTHORIZATION 'gpadmin'; rathandb=# CREATE ROLE rathan_role_admin IN ROLE rathan_group_test; rathandb=# CREATE ROLE rathan_role_no_admin IN ROLE rathan_group_test; rathandb=# CREATE ROLE rathan_role_test IN ROLE rathan_group_test; rathandb=# GRANT rathan_group_test TO rathan_role_admin WITH ADMIN OPTION; rathandb=# \du+ rathan_*; List of roles Role name | Attributes | Member of | Description ----------------------+--------------+---------------------+------------- rathan_group_test | | | rathan_role_admin | Cannot login | {rathan_group_test} | rathan_role_no_admin | Cannot login | {rathan_group_test} | rathan_role_test | Cannot login | {rathan_group_test} | rathandb=# SET SESSION AUTHORIZATION 'rathan_role_admin'; rathandb=# SELECT SESSION_USER, CURRENT_USER; session_user | current_user -------------------+------------------- rathan_role_admin | rathan_role_admin (1 row) rathandb=# REVOKE rathan_group_test FROM rathan_role_test; REVOKE ROLE rathandb=# \du+ rathan_role_test; List of roles Role name | Attributes | Member of | Description -------------------+--------------+-----------+------------- rathan_role_test | Cannot login | | rathandb=# GRANT rathan_group_test TO rathan_role_test; GRANT ROLE rathandb=# SET SESSION AUTHORIZATION 'rathan_role_no_admin'; SET rathandb=# SELECT SESSION_USER, CURRENT_USER; session_user | current_user ----------------------+---------------------- rathan_role_no_admin | rathan_role_no_admin (1 row) rathandb=# REVOKE rathan_group_test FROM rathan_role_test; ERROR: must have admin option on role "rathan_group_test"
補充:
設置默認schema:
ALTER USER username SET search_path = schema1,schema2,schema3,etc;
關於一點點權限:
默認狀況下,只有建立database的ROLE纔有權限對database作全量操做;
Superuser能夠訪問全部的object。其餘帳號想要使用須要被GRANT權限;
權限分類: SELECT, INSERT, UPDATE, DELETE, REFERENCES, TRIGGER, CREATE, CONNECT, TEMPORARY, EXECUTE, and USAGE.
上述權限所有GRANT的話能夠用ALL代替。
本文中的測試環境:
psql --version psql (PostgreSQL) 8.2.15
相關官方文檔:
角色:
http://www.postgresql.org/docs/8.2/static/user-manag.html
命令:
CREATE ROLE、ALTER ROLE、DROP ROLE、GRANT、REVOKE、SET ROLE、SET SESSION AUTHORIZATION
權限: