postgreSQL その19 VACCUM、ANALYZEのつづき
postgreSQL その8 課題リストは2018/4/30時点でのオイラの所見
■GRANT文
[P.187]
GRANT文でテーブルの権限を設定できる。テーブル単位で権限を設定できる。
[構文]
GRANT { { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER } [ , ...] | ALL [ PRIVILEGES ] } ON { [ TABLE ] テーブル名 [ , ...] } TO { ユーザ名 | PUBLIC } [ , ...]
TOに指定できるPUBLICパラメータは、すべてのユーザを意味する。テー売るごとに設定できる主な権限は下表のとおり。
テーブルに設定できる主な権限
|
権限 |
説明 |
|---|---|
|
SELECT |
SELECTを許可。COPY TOの使用も許可 |
|
INSERT |
INSERTを許可。COPY FROMの使用も許可 |
|
UPDATE |
UPDATEを許可 |
|
DELETE |
DELETEを許可 |
|
TRUNCATE |
TRUNCATEを許可(全行DELETE) |
|
REFERENCES |
外部キー制約の作成を許可 |
|
TRIGGER |
トリガの作成を許可 |
|
ALL |
すべての権限 |
テーブル権限はデフォルトではテーブルを作成したユーザにすべての権限があり、その他のユーザにはアクセス権限が与えられていない。
psqlコマンドの\dpで確認すると、「r/user名」と表示される。
※テーブルにUPDATE権限を設定する場合は、参照権限も伴う場合がほとんどなので、SELECT権限も併せて設定する。
■REVOKE文
[P.189]
REVOKE文は、テーブルに設定されたアクセス権限を取り消す。
指定できる権限はGRANT文と同じ。
【検証】 tab1のSELECTを全ユーザに許可
postgres=# select * from pg_tables where schemaname = 'public';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+-----------+------------+------------+------------+----------+-------------+-------------
public | tab1 | postgres | | f | f | f | f
public | tab2 | postgres | | f | f | f | f
public | tab3 | postgres | | f | f | f | f
(3 rows)
postgres=# \dp
アクセス権
スキーマ | 名前 | 型 | アクセス権 | アクセス権限を剥奪する
----------+------+----------+------------+------------------------
public | tab1 | テーブル | |
public | tab2 | テーブル | |
public | tab3 | テーブル | |
(3 行)
postgres=# \c postgres gad2
Password for user gad2:
You are now connected to database "postgres" as user "gad2".
postgres=> select * from tab2;
ERROR: permission denied for relation tab2
せ6> psql -h 192.168.2.7 -U postgres
ユーザ postgres のパスワード:
psql (8.4.20, サーバ 10.2)
注意: psql バージョン 8.4, サーババージョン 10.2.
psql の機能の中で、動作しないものがあるかもしれません。
"help" でヘルプを表示します.
postgres=# grant select on tab2 to public;
GRANT
postgres=> select user;
user
------
gad2
(1 row)
postgres=> select * from tab2;
n | m
---+---
(0 rows)
postgres=# \dp
アクセス権
スキーマ | 名前 | 型 | アクセス権 | アクセス権限を剥奪する
----------+------+----------+---------------------------+------------------------
public | tab1 | テーブル | |
public | tab2 | テーブル | postgres=arwdDxt/postgres |
: =r/postgres
public | tab3 | テーブル | |
(3 行)
postgres=# revoke select on tab2 from public;
REVOKE
postgres=# \dp
アクセス権
スキーマ | 名前 | 型 | アクセス権 | アクセス権限を剥奪する
----------+------+----------+---------------------------+------------------------
public | tab1 | テーブル | |
public | tab2 | テーブル | postgres=arwdDxt/postgres |
public | tab3 | テーブル | |
(3 行)
postgres=> select user;
user
------
gad2
(1 row)
postgres=> select * from tab2;
ERROR: permission denied for relation tab2
postgres=# grant select,insert on tab2 to gad4;
GRANT
postgres=# \dp
アクセス権
スキーマ | 名前 | 型 | アクセス権 | アクセス権限を剥奪する
----------+------+----------+---------------------------+------------------------
public | tab1 | テーブル | |
public | tab2 | テーブル | postgres=arwdDxt/postgres |
: gad4=ar/postgres
public | tab3 | テーブル | |
(3 行)
postgres=# alter user gad4 login;
ALTER ROLE
postgres=> \c postgres gad4
Password for user gad4:
You are now connected to database "postgres" as user "gad4".
postgres=> select user;
user
------
gad4
(1 row)
postgres=> insert into tab2 values(10,11);
INSERT 0 1
postgres=> select * from tab2;
n | m
----+----
10 | 11
(1 row)
postgres=> update tab2 set n = 20,m = 21 where n = 10;
ERROR: permission denied for relation tab2
postgres=> \c postgres gad2
Password for user gad2:
You are now connected to database "postgres" as user "gad2".
postgres=> select user;
user
------
gad2
(1 row)
postgres=> select * from tab2;
ERROR: permission denied for relation tab2
■\dpまたは\z
テーブルのアクセス権限の確認ができる。 引数としてテーブル名を指定しなかった場合は、データベース内のすべてのテーブルについての権限の情報を表示する。
表示される文字、記号の意味
|
文字、記号 |
権限 |
|---|---|
|
ユーザ名=xxxx |
ユーザに与えられた権限 |
|
=xxxx |
PUBLICに与えられた権限 |
|
r |
SELECT(読み取り:read) |
|
w |
UPDATE(書き込み:write) |
|
a |
INSERT(追加:appned) |
|
d |
DELETE |
|
D |
TRUNCATE |
|
x |
REFERENCES |
|
t |
TRIGGER |
|
arwdDxt |
すべての権限(テーブル用。他のオブジェクトでは異なる) |
|
/yyyy |
この権限を付与したユーザ |