亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 數據庫 > PostgreSQL > 正文

PostgreSQL 角色與用戶管理介紹

2020-01-31 15:23:38
字體:
來源:轉載
供稿:網友

一、角色與用戶的區別

角色就相當于崗位:角色可以是經理,助理。
用戶就是具體的人:比如陳XX經理,朱XX助理,王XX助理。
在PostgreSQL 里沒有區分用戶和角色的概念,"CREATE USER" 為 "CREATE ROLE" 的別名,這兩個命令幾乎是完全相同的,唯一的區別是"CREATE USER" 命令創建的用戶默認帶有LOGIN屬性,而"CREATE ROLE" 命令創建的用戶默認不帶LOGIN屬性(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not)。

1.1 創建角色與用戶

CREATE ROLE 語法

CREATE ROLE name [ [ WITH ] option [ ... ] ]
where option can be:
      SUPERUSER | NOSUPERUSER
    | CREATEDB | NOCREATEDB
    | CREATEROLE | NOCREATEROLE
    | CREATEUSER | NOCREATEUSER
    | INHERIT | NOINHERIT
    | LOGIN | NOLOGIN
    | REPLICATION | NOREPLICATION
    | CONNECTION LIMIT connlimit
    | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
    | VALID UNTIL 'timestamp'
    | IN ROLE role_name [, ...]
    | IN GROUP role_name [, ...]
    | ROLE role_name [, ...]
    | ADMIN role_name [, ...]
    | USER role_name [, ...]
    | SYSID uid

創建david 角色和sandy 用戶
postgres=# CREATE ROLE david;  //默認不帶LOGIN屬性
CREATE ROLE
postgres=# CREATE USER sandy;  //默認具有LOGIN屬性
CREATE ROLE
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     | Cannot login                                   | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=#
postgres=# SELECT rolname from pg_roles ;
 rolname 
----------
 postgres
 david
 sandy
(3 rows)

postgres=# SELECT usename from pg_user;         //角色david 創建時沒有分配login權限,所以沒有創建用戶
 usename 
----------
 postgres
 sandy
(2 rows)

postgres=#
1.2 驗證LOGIN屬性
postgres@CS-DEV:~> psql -U david
psql: FATAL:  role "david" is not permitted to log in
postgres@CS-DEV:~> psql -U sandy
psql: FATAL:  database "sandy" does not exist
postgres@CS-DEV:~> psql -U sandy -d postgres
psql (9.1.0)
Type "help" for help.

postgres=> /dt
No relations found.
postgres=>
用戶sandy 可以登錄,角色david 不可以登錄。
1.3 修改david 的權限,增加LOGIN權限
postgres=# ALTER ROLE david LOGIN ;
ALTER ROLE
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=# SELECT rolname from pg_roles ;
 rolname 
----------
 postgres
 sandy
 david
(3 rows)

postgres=# SELECT usename from pg_user;  //給david 角色分配login權限,系統將自動創建同名用戶david
 usename 
----------
 postgres
 sandy
 david
(3 rows)

postgres=#
1.4 再次驗證LOGIN屬性
postgres@CS-DEV:~> psql -U david -d postgres
psql (9.1.0)
Type "help" for help.

postgres=> /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=>
david 現在也可以登錄了。

二、查看角色信息

psql 終端可以用/du 或/du+ 查看,也可以查看系統表 select * from pg_roles;
postgres=> /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 david     | Cannot login                                   | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 sandy     |                                                | {}

postgres=> /du+
                                    List of roles
 Role name |                   Attributes                   | Member of | Description
-----------+------------------------------------------------+-----------+-------------
 david     | Cannot login                                   | {}        |
 postgres  | Superuser, Create role, Create DB, Replication | {}        |
 sandy     |                                                | {}        |

postgres=> SELECT * from pg_roles;
 rolname  | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolconfig |  oid 
----------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+---------------+-----------+-------
 postgres | t        | t          | t             | t           | t            | t           | t              |           -1 | ********    |               |           |    10
 david    | f        | t          | f             | f           | f            | f           | f              |           -1 | ********    |               |           | 49438
 sandy    | f        | t          | f             | f           | f            | t           | f              |           -1 | ********    |               |           | 49439
(3 rows)

postgres=>

三、角色屬性(Role Attributes)

一個數據庫角色可以有一系列屬性,這些屬性定義了他的權限。

屬性說明
login只有具有 LOGIN 屬性的角色可以用做數據庫連接的初始角色名。
superuser數據庫超級用戶
createdb創建數據庫權限
createrole      允許其創建或刪除其他普通的用戶角色(超級用戶除外)
replication做流復制的時候用到的一個用戶屬性,一般單獨設定。
password在登錄時要求指定密碼時才會起作用,比如md5或者password模式,跟客戶端的連接認證方式有關
inherit用戶組對組員的一個繼承標志,成員可以繼承用戶組的權限特性
......

 

四、創建用戶時賦予角色屬性

從pg_roles 表里查看到的信息,在上面創建的david 用戶時,默認沒有創建數據庫等權限。

postgres@CS-DEV:~> psql -U david -d postgres
psql (9.1.0)
Type "help" for help.

postgres=> /du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
sandy | | {}

postgres=> CREATE DATABASE test;
ERROR: permission denied to create database
postgres=>
如果要在創建角色時就賦予角色一些屬性,可以使用下面的方法。
首先切換到postgres 用戶。
4.1 創建角色bella 并賦予其CREATEDB 的權限。
postgres=# CREATE ROLE bella CREATEDB ;
CREATE ROLE
postgres=# /du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB, Cannot login | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
sandy | | {}

postgres=#
4.2 創建角色renee 并賦予其創建數據庫及帶有密碼登錄的屬性。
postgres=# CREATE ROLE renee CREATEDB PASSWORD 'abc123' LOGIN;
CREATE ROLE
postgres=# /du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB, Cannot login | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
renee | Create DB | {}
sandy | | {}

postgres=#

4.3 測試renee 角色

a. 登錄
postgres@CS-DEV:~> psql -U renee -d postgres
psql (9.1.0)
Type "help" for help.

postgres=>
用renee 用戶登錄數據庫,發現不需要輸入密碼既可登錄,不符合實際情況。
b. 查找原因
在角色屬性中關于password的說明,在登錄時要求指定密碼時才會起作用,比如md5或者password模式,跟客戶端的連接認證方式有關。

查看pg_hba.conf 文件,發現local 的METHOD 為trust,所以不需要輸入密碼。

將local 的METHOD 更改為password,然后保存重啟postgresql。

c. 再次驗證

提示輸入密碼,輸入正確密碼后進入到數據庫。

d. 測試創建數據庫

創建成功。

五、給已存在用戶賦予各種權限

使用ALTER ROLE 命令。
ALTER ROLE 語法:
ALTER ROLE name [ [ WITH ] option [ ... ] ]
where option can be:

      SUPERUSER | NOSUPERUSER
    | CREATEDB | NOCREATEDB
    | CREATEROLE | NOCREATEROLE
    | CREATEUSER | NOCREATEUSER
    | INHERIT | NOINHERIT
    | LOGIN | NOLOGIN
    | REPLICATION | NOREPLICATION
    | CONNECTION LIMIT connlimit
    | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
    | VALID UNTIL 'timestamp'

ALTER ROLE name RENAME TO new_name

ALTER ROLE name [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER ROLE name [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
ALTER ROLE name [ IN DATABASE database_name ] RESET configuration_parameter
ALTER ROLE name [ IN DATABASE database_name ] RESET ALL
5.1 賦予bella 登錄權限
a. 查看現在的角色屬性
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 bella     | Create DB, Cannot login                        | {}
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 renee     | Create DB                                      | {}
 sandy     |                                                | {}

postgres=#
b. 賦予登錄權限
postgres=# ALTER ROLE bella WITH LOGIN;
ALTER ROLE
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 bella     | Create DB                                      | {}
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 renee     | Create DB                                      | {}
 sandy     |                                                | {}

postgres=#
5.2 賦予renee 創建角色的權限
postgres=# ALTER ROLE renee WITH CREATEROLE;
ALTER ROLE
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 bella     | Create DB                                      | {}
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 renee     | Create role, Create DB                         | {}
 sandy     |                                                | {}

postgres=#
5.3 賦予david 帶密碼登錄權限
postgres=# ALTER ROLE david WITH PASSWORD 'ufo456';
ALTER ROLE
postgres=#
5.4 設置sandy 角色的有效期
postgres=# ALTER ROLE sandy VALID UNTIL '2014-04-24';
ALTER ROLE
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 bella     | Create DB                                      | {}
 david     |                                                | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 renee     | Create role, Create DB                         | {}
 sandy     |                                                | {}

postgres=# SELECT * from pg_roles ;
 rolname  | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolreplication | rolconnlimit | rolpassword |     rolvaliduntil      | rolconfig |  oid 
----------+----------+------------+---------------+-------------+--------------+-------------+----------------+--------------+-------------+------------------------+-----------+-------
 postgres | t        | t          | t             | t           | t            | t           | t              |           -1 | ********    |                        |           |    10
 bella    | f        | t          | f             | t           | f            | t           | f              |           -1 | ********    |                        |           | 49440
 renee    | f        | t          | t             | t           | f            | t           | f              |           -1 | ********    |                        |           | 49442
 david    | f        | t          | f             | f           | f            | t           | f              |           -1 | ********    |                        |           | 49438
 sandy    | f        | t          | f             | f           | f            | t           | f              |           -1 | ********    | 2014-04-24 00:00:00+08 |           | 49439
(5 rows)

postgres=#

六、角色賦權/角色成員

在系統的角色管理中,通常會把多個角色賦予一個組,這樣在設置權限時只需給該組設置即可,撤銷權限時也是從該組撤銷。在PostgreSQL中,首先需要創建一個代表組的角色,之后再將該角色的membership 權限賦給獨立的角色即可。
6.1 創建組角色
postgres=# CREATE ROLE father login nosuperuser nocreatedb nocreaterole noinherit encrypted password 'abc123';
CREATE ROLE
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 bella     | Create DB                                      | {}
 david     |                                                | {}
 father    | No inheritance                                 | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 renee     | Create role, Create DB                         | {}
 sandy     |                                                | {}

postgres=#
6.2 給father 角色賦予數據庫test 連接權限和相關表的查詢權限。
postgres=# GRANT CONNECT ON DATABASE test to father;
GRANT
postgres=# /c test renee
You are now connected to database "test" as user "renee".
test=> /dt
No relations found.
test=> CREATE TABLE emp (
test(> id serial,
test(> name text);
NOTICE:  CREATE TABLE will create implicit sequence "emp_id_seq" for serial column "emp.id"
CREATE TABLE
test=> INSERT INTO emp (name) VALUES ('david'); 
INSERT 0 1
test=> INSERT INTO emp (name) VALUES ('sandy');
INSERT 0 1
test=> SELECT * from emp;
 id | name 
----+-------
  1 | david
  2 | sandy
(2 rows)

test=> /dt
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 public | emp  | table | renee
(1 row)

test=> GRANT USAGE ON SCHEMA public to father;
WARNING:  no privileges were granted for "public"
GRANT
test=> GRANT SELECT on public.emp to father;
GRANT
test=>
6.3 創建成員角色
test=> /c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# CREATE ROLE son1 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'abc123';
CREATE ROLE
postgres=#
這里創建了son1 角色,并開啟inherit 屬性。PostgreSQL 里的角色賦權是通過角色繼承(INHERIT)的方式實現的。
6.4 將father 角色賦給son1
postgres=# GRANT father to son1;
GRANT ROLE
postgres=#
還有另一種方法,就是在創建用戶的時候賦予角色權限。
postgres=# CREATE ROLE son2 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'abc123' in role father;
CREATE ROLE
postgres=#
6.5 測試son1 角色
postgres=# /c test son1
You are now connected to database "test" as user "son1".
test=> /dt
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 public | emp  | table | renee
(1 row)

test=> SELECT * from emp;
 id | name 
----+-------
  1 | david
  2 | sandy
(2 rows)

test=>
用renee 角色新創建一張表,再次測試
test=> /c test renee
You are now connected to database "test" as user "renee".
test=> CREATE TABLE dept (
test(> deptid integer,
test(> deptname text);
CREATE TABLE
test=> INSERT INTO dept (deptid, deptname) values(1, 'ts');
INSERT 0 1
test=> /c test son1
You are now connected to database "test" as user "son1".
test=> SELECT * from dept ;
ERROR:  permission denied for relation dept
test=>
son1 角色只能查詢emp 表的數據,而不能查詢dept 表的數據,測試成功。
6.6 查詢角色組信息
test=> /c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=#
postgres=# /du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 bella     | Create DB                                      | {}
 david     |                                                | {}
 father    | No inheritance                                 | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}
 renee     | Create role, Create DB                         | {}
 sandy     |                                                | {}
 son1      |                                                | {father}
 son2      |                                                | {father}

postgres=#
“ Member of ” 項表示son1 和son2 角色屬于father 角色組。


 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
福利精品视频在线| 日韩欧美成人精品| 欧美日韩国产中字| 中文字幕日韩欧美在线视频| 欧美大片在线免费观看| 色爱av美腿丝袜综合粉嫩av| 久久久久久成人| 91av在线播放视频| 日本三级韩国三级久久| 亚洲精品久久在线| 综合国产在线观看| 91精品国产免费久久久久久| 538国产精品视频一区二区| 久久99视频免费| 亚洲人成电影在线| 欧美日韩国产丝袜另类| 日韩免费精品视频| 亚洲第一页中文字幕| 午夜精品理论片| 欧美激情精品在线| 国产一区二区美女视频| 精品亚洲一区二区三区在线播放| 亚洲美女免费精品视频在线观看| 精品中文视频在线| 亚洲天堂成人在线视频| 日韩免费在线播放| 97在线免费视频| 韩国19禁主播vip福利视频| 亚洲少妇激情视频| 亚洲娇小xxxx欧美娇小| 8090成年在线看片午夜| 亚洲新声在线观看| xxxxx成人.com| 国产精品中文字幕在线观看| 久久久精品999| 秋霞午夜一区二区| 亚洲激情小视频| 91在线观看免费高清| 九色成人免费视频| 麻豆精品精华液| 欧美日韩国产精品一区二区不卡中文| 国产精品久久一区| 久久久在线观看| 国产精品视频资源| 国产精品一区二区久久久久| 亚洲精品自在久久| 91精品视频网站| 91精品国产综合久久香蕉922| 午夜精品视频网站| 一区二区三区视频观看| 在线观看欧美www| 国产区精品在线观看| 在线观看不卡av| 亚洲一区第一页| 亚洲大胆美女视频| 日韩欧美精品在线观看| 一本大道久久加勒比香蕉| 亚洲最新中文字幕| 日韩av电影在线网| 国产日产欧美a一级在线| 日韩精品999| 一本色道久久综合亚洲精品小说| 国产91色在线| 亚洲成人久久久久| 欧美日韩在线观看视频小说| 国产91成人在在线播放| 国产精品第100页| 久久综合九色九九| 成人a在线视频| 92看片淫黄大片欧美看国产片| 夜夜嗨av色一区二区不卡| 久久精品欧美视频| 国产精品中文字幕久久久| 亚洲成人免费网站| 欧美国产日韩免费| 另类色图亚洲色图| 国产精品电影一区| 亚洲欧美精品一区二区| 国产精品人人做人人爽| 精品动漫一区二区| 亚洲精品福利免费在线观看| 热久久99这里有精品| 亚洲激情在线观看| 欧美激情第1页| 777午夜精品福利在线观看| 91精品国产综合久久香蕉922| 国产精品欧美风情| 成人精品视频久久久久| 久久久久久久网站| 久久久久久中文字幕| 国产极品精品在线观看| 91亚洲精品在线观看| 国产91成人video| 国产精品久久久久久久久久ktv| 国产精品99久久久久久人| 一区国产精品视频| 69久久夜色精品国产69乱青草| 91精品国产99久久久久久| 日本久久久久亚洲中字幕| 懂色aⅴ精品一区二区三区蜜月| 欧美成人一二三| 欧美性猛交xxxx乱大交蜜桃| 亚洲福利影片在线| 久久久久久国产免费| 日本久久久久久久| 国产精品成久久久久三级| 精品视频—区二区三区免费| 午夜精品一区二区三区在线视频| 精品美女久久久久久免费| 日韩经典一区二区三区| 久久久精品久久久| 国产女精品视频网站免费| 国产成人av在线| 亚洲欧美日韩爽爽影院| 91亚洲精品久久久久久久久久久久| 播播国产欧美激情| 亚洲国产精品一区二区三区| 国产做受69高潮| 亚洲少妇激情视频| 国产在线精品播放| 国产精品极品美女粉嫩高清在线| 久久久久久国产三级电影| 中文字幕av一区| 亚洲美女黄色片| 91沈先生作品| 久久精品色欧美aⅴ一区二区| 精品国产91久久久久久| 国内精品模特av私拍在线观看| 亚洲一区中文字幕在线观看| 国产精品爽爽ⅴa在线观看| 欧美久久精品一级黑人c片| 成人激情春色网| 国产精品丝袜一区二区三区| 在线观看亚洲区| 中文字幕在线看视频国产欧美| 91国产美女视频| 亚洲偷欧美偷国内偷| 亚洲男人天堂视频| 欧美专区福利在线| 亚洲色图综合久久| 国产精品影院在线观看| 国产97在线|日韩| 久久久免费精品视频| 国产激情视频一区| 在线视频免费一区二区| 欧美与黑人午夜性猛交久久久| 国产一区二区三区在线观看视频| 日韩在线视频一区| 中文字幕欧美精品日韩中文字幕| 亚洲免费福利视频| 2020欧美日韩在线视频| www国产亚洲精品久久网站| 最好看的2019年中文视频| 亚洲人成啪啪网站| 91黄色8090| 日韩精品日韩在线观看| 91sa在线看| www.欧美精品一二三区| 亚洲视频网站在线观看| 青青a在线精品免费观看| 亚洲伊人一本大道中文字幕| 欧美电影《睫毛膏》| 日韩在线中文视频| 成人日韩av在线|