postgres=# create role test0 createdb createrole login;
CREATE ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
test0 | Create role, Create DB | {}
test_role | | {}
postgres=# \du+
List of roles
Role name | Attributes | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |
test0 | Create role, Create DB | {} |
test_role | | {} |
postgres=# create role test1 inherit;
CREATE ROLE
postgres=# grant test0 to test1;
GRANT ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
test0 | Create role, Create DB | {}
test1 | Cannot login | {test0}
test_role | | {}
postgres=# set role test0;
SET
postgres=> create table test0(id int);
CREATE TABLE
postgres=> set role test1;
SET
postgres=> insert into test0 select 1;
INSERT 0 1
postgres=> set role test0;
SET
postgres=> set role postgres
postgres-> ;
SET
postgres=# create role test2 login;
CREATE ROLE
postgres=# set role test2;
SET
postgres=> insert into test0 select 1;
ERROR: permission denied for relation test0
postgres=> \d
原文:https://www.cnblogs.com/kuang17/p/10243482.html