版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/ctypyb2002/article/details/84328623
os: centos 7.4
db: postgresql 9.6
postgresql 使用角色的概念管理数据库访问权限。
一个角色可以被看成是一个数据库用户或者是一个数据库用户组,这取决于角色被怎样设置。
角色可以拥有数据库对象(例如,表和函数)并且能够把那些对象上的权限赋予给其他角色来控制谁能访问哪些对象。此外,还可以把一个角色中的成员资格授予给另一个角色,这样允许成员角色使用被赋予给另一个角色的权限。
角色的概念把"用户"和"组"的概念都包括在内。
默认角色
postgres=# select * from pg_roles order by rolname;
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig | oid
-------------------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+------
pg_signal_backend | f | t | f | f | f | f | -1 | ******** | | f | | 4200
postgres | t | t | t | t | t | t | -1 | ******** | | t | | 10
(2 rows)
postgres 这个角色的 rolcanlogin 为 true,说明已经是可以登录的用户,且是超级用户。
pg_signal_backend 这个角色表示可以给其他后端发送信号(比如: 取消查询、终止)。
9.6 的默认角色还比较少,在权限分离这块还不是特别清晰。
10、11在数据库的权限分离上已经逐渐清晰了。
授予默认角色
创建用户
postgresq 的用户就等于具有登录权限的角色。
postgres=# create role peiyb with login password 'peiybpeiyb';
CREATE ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
peiyb | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
授予 pg_signal_backend 角色
postgres=# grant pg_signal_backend to peiyb;
GRANT ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+---------------------
peiyb | | {pg_signal_backend}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=#
通过 Member of 可以看到已经具有 pg_signal_backend 权限。