CREATEROLE readonly LOGIN ENCRYPTED PASSWORD'readonly' nosuperuser noinherit nocreatedb nocreaterole ; grantconnectondatabase skytf to readonly; grantusageonschema skytf to readonly; grantselecton all tablesinschema skytf to readonly; grantconnectondatabase beha_db to readonly; grantusageonschema beha_db to readonly; grantselecton all tablesinschema beha_db to readonly;
postgres=# drop role readonly; ERROR: role "readonly" cannot be dropped because some objects depend onit DETAIL: privileges for database beha_db privileges for database skytf 2506 objects in database beha_db 265 objects in database skytf
beha_db=> /c beha_db postgres You are now connected to database "beha_db" as user "postgres".
beha_db=# drop owned by readonly; DROP OWNED
连接 skytf 库,删除用户 readonly 的权限信息
1 2 3 4 5
postgres=# /c skytf postgres You are now connected to database "skytf" as user "postgres"..
skytf=# drop owned by readonly; DROP OWNED
尝试再次删除用户 readonly
1 2 3
postgres=# drop role readonly; ERROR: role "readonly" cannot be dropped because some objects depend on it DETAIL: privileges for database beha_db
备注:提示用户 readonly 还有权限信息存在,删除失败。
删除 readonly 用户的数据库连接权限
1 2 3 4
postgres=# revoke connect on database beha_db from readonly; REVOKE postgres=# revoke connect on database skytf from readonly; REVOKE
再次删除用户,终于删除成功
1 2
postgres=# droprole readonly; DROPROLE
方法二:REVOKE 权限信息
创建用户并赋予权限
1 2 3 4 5
CREATEROLE readonly LOGIN ENCRYPTED PASSWORD'readonly' nosuperuser noinherit nocreatedb nocreaterole ; grantconnectondatabase skytf to readonly; grantusageonschema skytf to readonly; grantselecton all tablesinschema skytf to readonly;
REVOKE权限
1 2 3 4 5 6 7 8
skytf=# /c skytf skytf You are now connected to database "skytf" as user "skytf". skytf=> revoke connect on database skytf from readonly; REVOKE skytf=> revoke select on all tables in schema skytf from readonly; REVOKE skytf=> revoke usage on schema skytf from readonly; REVOKE
删除用户
1 2
postgres=# droprole readonly; DROPROLE
备注:在删除用户前,需先删除用户的对像和权限信息,才能删除用户,关于这一点,可以参考手册上的
附:手册上的解释
Drop Role A role cannot be removed if it is still referenced in any database of the cluster; an error will be raised if so. Before dropping the role, you must drop all the objects it owns (or reassign their ownership) and revoke any privileges the role has been granted.