My cool new Blog

Hello World! This is the most epic subtitle ever.
en de

psql create role and assign priviledges

2018-04-18 筆記 Eric Chang

最後有成功,所以先把過程記錄下來,再來研究該怎麼下指令。

建立一個 role 叫 dbreader

postgres=# CREATE ROLE dbreader;
CREATE ROLE

讓dbreader這個 role 可以讀取 public 這個 schema 的{usage | select}

postgres=# GRANT USAGE ON SCHEMA public TO dbreader;
GRANT
postgres=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO dbreader;
GRANT

設定public schema 的default 權限 給 dbreader 這個 role

postgres=# ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO dbreader;
ALTER DEFAULT PRIVILEGES

建立資料庫使用者 readuser

postgres=# CREATE USER readuser WITH PASSWORD 'readuserpassword';
CREATE ROLE

將dbreader這個role 指定給 readuser

postgres=# GRANT dbreader TO readuser;
GRANT ROLE

切到 neo DB

postgres=# \q
postgres@hqs022:~$ psql --u postgres  neo
psql (9.6.8)
Type "help" for help.

neonexus=# GRANT SELECT ON ALL TABLES IN SCHEMA public to readuser;
GRANT

neonexus=# GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readuser;
GRANT

neonexus=# GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO readuser;
GRANT
comments powered by Disqus