1. 简介



    PG使用角色的概念来管理数据库访问权限。
    一个角色可以是一个用户或者一个组,这是根据这个角色的设置来决定的。
    具有登录权限的角色称为用户。
    一个角色可以是其他角色的一个成员,这种包含其他角色的角色称为组。



  2. 内建roles


    Role

    Allowed Access

    pg_read_all_settings

    Read all configuration variables, even those normally visible only to superusers.

    读取所有配置参数,即使通常只能被superusers读取的那些

    pg_read_all_stats

    Read all pg_stat_* views and use various statistics related extensions, even those normally visible only to superusers.

    读取所有pg_stat_*视图,使用各种统计的相关扩展

    pg_stat_scan_tables

    Execute monitoring functions that may take ACCESS SHARE locks on tables, potentially for a long time.

    执行监控函数,这可能在表上加access share锁

    pg_signal_backend

    Send signals to other backends (eg: cancel query, terminate).

    发送信号到其他后台进程

    pg_read_server_files

    Allow reading files from any location the database can access on the server with COPY and other file-access functions.

    使用COPY或其他文件操作函数读取数据库可以access的所有位置的文件

    pg_write_server_files

    Allow writing to files in any location the database can access on the server with COPY and other file-access functions.

    使用COPY或其他文件操作函数写入数据库可以access的所有位置的文件

    pg_execute_server_program

    Allow executing programs on the database server as the user the database runs as with COPY and other functions which allow executing a server-side program.

    允许执行数据库服务器的程序

    pg_monitor

    Read/execute various monitoring views and functions. This role is a member of pg_read_all_settings, pg_read_all_stats and pg_stat_scan_tables.

    读取/执行各种监控视图和函数。



  3. 管理PG roles


    从8.1版本开始,PG使用角色的概念合并了用户与组的概念。

    $ psql postgres
    psql (11.3)
    Type "help" for help.
    
    postgres=# drop role cf_role ;
    DROP ROLE
    postgres=# create role cf_role ;
    CREATE ROLE
    postgres=# select rolname from pg_roles order by 1 ;
              rolname          
    ---------------------------
     cf_role
    postgres=# \du
                                       List of roles
     Role name |                 Attributes                         | Member of 
    ----------+--------------------------------------------------+-----------
     cf_role    | Cannot login                                       | {}
     pgsql     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    
    


    1. role的属性

      数据库角色的属性定义了角色的权限,包括登录、超级用户、创建数据库、创建角色、密码、复制等等。

      权限

      说明

      示例SQL 

      登录权限角色是否可作为用户登录
      postgres=# create role name login ;
      postgres=# create user  name ;
      --上面两种语句是等价的。
      超级用户权限

      拥有该权限的角色可以绕过除登录权限以外的所有权限检查;必须谨慎对待这个权限。

      如果需要创建具有superuser的角色,你当前的角色必须具有superuser权限。

      postgres=# create role cf_dba superuser ;
      CREATE ROLE
      postgres=# \du
                      List of roles
       Role name |    Attributes                    | Member of 
      -----------+----------------------------------+-----------
       cf_dba    | Superuser, Cannot login          | {}
      创建数据库权限角色是否可创建数据库
      postgres=# create role cf_dba2 createdb ;
      CREATE ROLE
      postgres=# alter role cf createdb ;
      ALTER ROLE
      
      
      创建角色权限角色是否可以创建其他角色
      postgres=# create role cf_dba3 createrole ;
      CREATE ROLE
      复制权限用于streaming replication的角色必须被显式的赋予replication、login权限
      postgres=# create role repl replicaiton login ;
      CREATE ROLE

      登录密码和有效期


      postgres=# create role cf_role2 
      with password 'Chaofeng521' valid until '2019-05-23';
      CREATE ROLE
      postgres=# \du
                        List of roles
       Role name |     Attributes                 | Member of 
      -----------+------------------------------- +-----------
       cf_role    | Cannot login                  | {}
       cf_role2   | Cannot login ,Password valid 
                    until 2019-05-23 00:00:00+08  | {}


      建议:创建一个role,赋予createdb、createrole权限,而不赋予superuser权限,用于日常管理。

    2. Role 成员关系

      将roles作为组来管理更容易理解,这样就可以把组作为一个整体来grant或revoke权限。

      在PG中,创建一个role就代表了一个组,然后将组里的membership赋权给独立的用户roles。

      默认情况下,一个group role没有login权限。

      --使用create role命令来创建一个group role
      postgres=# create role sales;
      CREATE ROLE
      postgres=# \du
                                         List of roles
       Role name |                         Attributes                         | Member of 
      -----------+------------------------------------------------------------+-----------
       pgsql     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
       sales     | Cannot login                                               | {}
      
      --然后可以将一个user role 添加到group role中或从中移除:
      postgres=# create role cf ;
      CREATE ROLE
      postgres=# \du  
                                         List of roles
       Role name |                         Attributes                         | Member of 
      -----------+------------------------------------------------------------+-----------
       cf        | Cannot login                                               | {}
       pgsql     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
       sales     | Cannot login                                               | {}
      
      postgres=# grant sales to cf ;
      GRANT ROLE
      postgres=# \du
                                         List of roles
       Role name |                         Attributes                         | Member of 
      -----------+------------------------------------------------------------+-----------
       cf        | Cannot login                                               | {sales}
       pgsql     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
       sales     | Cannot login                                               | {}
      
      postgres=# revoke sales from cf ;
      REVOKE ROLE
      postgres=# \du
                                         List of roles
       Role name |                         Attributes                         | Member of 
      -----------+------------------------------------------------------------+-----------
       cf        | Cannot login                                               | {}
       pgsql     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
       sales     | Cannot login                                               | {}
      
      

      注意,PG中不允许互为组成员。即不允许A∈B,B∈A的情况。

    3. group和user role的继承


      一个user role可以在以下情况时使用group role的权限:

      • 使用set role语句来临时作为group role使用,这表示这个user role使用group role的权限,而不是他原本的权限。

      另外,在session中创建的任何数据库对象时属于group role的,而不是属于用户role。

      • 其次,具有INHERIT属性的user role 将自动拥有其所属的group role 的权限,包括继​​承的group role的所有权限。

      例:

      postgres=# create role doe login inherit ;
      CREATE ROLE
      postgres=# create role sales noinherit ;
      CREATE ROLE
      postgres=# create role marketing noinherit ;
      CREATE ROLE
      postgres=# 
      postgres=# 
      postgres=# grant sales to doe ;
      GRANT ROLE
      postgres=# grant marketing to sales ;
      GRANT ROLE
      postgres=# 
      postgres=# \du
                                          List of roles
       Role name |                         Attributes                         |  Member of  
      -----------+------------------------------------------------------------+-------------
       doe       |                                                            | {sales}
       marketing | No inheritance, Cannot login                               | {}
       pgsql     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
       sales     | No inheritance, Cannot login                               | {marketing}
      
      
      --如果使用doe连接到PG,那么会有doe的权限和sales的权限,因为doe的user role有inherit属性;
      --但是不会具有marketing的权限,因为sales的user role指定了noinherit属性。
      
      
      --执行了set role sales之后,你将只拥有sales role具有的权限,而不是doe的权限;
      --执行了set role marketing之后,你将只拥有marketing具有的权限,而不是doe的或者sales的。
      --执行reset role命令来恢复原始权限。
      
      
      

      注意,只有作用于数据库对象之上的权限可以被继承(inheritable),诸如login 、 superuser 、 createrole 和 created等原始的特殊role不能被继承。

    4. removing role

      postgres=# drop role sales ;
      
      
      --在移除role之前,必须将这个role拥有的所有对象重新分配权限或者移除。
      --如果移除group role,那么PG将自动移除group中的所有membership。
      --Group中的user role不受影响。
      



  4. 相关SQL      

用户授权


  • No labels