1) 啓動數據庫服務,使用超級用戶 postgres 建立應用用戶 appuser,賦權createdb、login,appuser 的密碼設置爲 1qaz@WSX,並體現到.pgpass 文件中, 以便 appuser 免密登陸,appuser 用戶的密碼在 2022 年 05 月 01 日以前是有效的。
2) 建立屬主爲appuser 的表空間並命名爲appuser,指向/appuser(若沒有此目錄請自行建立並管理權限);
3) 建立appdb 數據庫,owner 是appuser(要求appdb 數據庫要在exam 表空間內),並要求實現:
a.回收 appdb 中的 public schema 上的 create object 權限。
b. 以 appuser 用戶在 appdb 數據庫中建立名爲 appuser 的 schema
c. 以 appuser 用戶在 appdb 數據庫中建立 app 表(id int),app 表的 schema是 appuser
其餘的非超級用戶不能 connect 到 appdb 數據庫中
4) 自行創建 readonlyuser 用戶,要求以下:
a. readonlyuser 能鏈接到 appdb 中
b. 密碼設置爲 1qaz@WSX,並體現到.pgpass 文件中,以便 readonlyuser 免密登陸
c. readonlyuser 用戶有對 appuser 用戶下 appuser schema 下全部表(包括未來新創建的表)的只讀權限;sql
[pg10@data01 ~]$ psql -d postgres
psql (10.14)
Type "help" for help.數據庫
postgres=# create user appuser with createdb login valid until '2022-05-01' password '1qaz@WSX';
CREATE ROLE
postgres=# \q
[pg10@data01 ~]$ su - root
Password:
Last login: Mon Jan 4 22:02:58 CST 2021 from gateway on pts/1
[root@data01 ~]# mkdir /appuser
[root@data01 ~]# chown pg10.pg10 /appuser
[root@data01 ~]# su - pg10
Last login: Mon Jan 4 22:03:26 CST 2021 on pts/1
[pg10@data01 ~]$ vim .pgpass
[pg10@data01 ~]$ cat .pgpass br/>data01:5666:postgres:postgres:1qaz@WSX
data01:5666:postgres:appuser:1qaz@WSX
[pg10@data01 ~]$ psql -d postgres -U postgres
psql (10.14)
Type "help" for help.vim
postgres=# create tablespace appuser owner appuser location '/appuser';
CREATE TABLESPACE
postgres=# create database appdb with owner appuser tablespace appuser;
CREATE DATABASEapp
postgres=# \c appdb appuser
You are now connected to database "appdb" as user "appuser".
appdb=> exitide
[pg10@data01 ~]$ psql -d postgres -U postgres
psql (10.14)
Type "help" for help.post
postgres=# \c appdb postgres
You are now connected to database "appdb" as user "postgres".
appdb=# revoke create on schema public from public;
REVOKEspa
appdb=# \c appdb appuser
You are now connected to database "appdb" as user "appuser".
appdb=> create schema appuser;
CREATE SCHEMA
appdb=> create table appuser.app(id int);
CREATE TABLE
appdb=> revoke connect on database appdb from public;
REVOKE
appdb=> get
appdb=> \c appdb postgres
You are now connected to database "appdb" as user "postgres".
appdb=# create user readonlyuser with password '1qaz@WSX';
CREATE ROLE
appdb=# grant connect on database appdb to readonlyuser;
GRANTit
appdb=# \c appdb appuser
You are now connected to database "appdb" as user "appuser".
appdb=> grant usage on schema appuser to readonlyuser;
GRANT
appdb=> grant select on all tables in schema appuser to readonlyuser;
GRANT
appdb=> alter default privileges grant select on tables to readonlyuser;
ALTER DEFAULT PRIVILEGESio