示例用法:实施继承的权限

以下步骤显示了用户 Joe 如何针对给定架构启用权限继承,以便其他用户可以访问该架构中的表。

  1. Joe 创建架构 schema1,并在其中创建表 table1

    
    =>\c - Joe
    You are now connected as user Joe
    => CREATE SCHEMA schema1;
    CRDEATE SCHEMA
    => CREATE TABLE schema1.table1 (id int);
    CREATE TABLE
    
  2. Joe 将对 schema1 的 USAGE 和 CREATE 权限授予 Myra

    
    => GRANT USAGE, CREATE ON SCHEMA schema1 to Myra;
    GRANT PRIVILEGE
    
  3. Myra 查询 schema1.table1,但查询失败:

    
    =>\c - Myra
    You are now connected as user Myra
    => SELECT * FROM schema1.table1;
    ERROR 4367: Permission denied for relation table1
    
  4. Joe 授予 Myraschema1SELECT ON SCHEMA 权限:

    
    =>\c - Joe
    You are now connected as user Joe
    => GRANT SELECT ON SCHEMA schema1 to Myra;
    GRANT PRIVILEGE
    
  5. Joe 使用 ALTER TABLE 包含 table1 的 SCHEMA 权限:

    
    => ALTER TABLE schema1.table1 INCLUDE SCHEMA PRIVILEGES;
    ALTER TABLE
    
  6. Myra的查询现在成功:

    
    =>\c - Myra
    You are now connected as user Myra
    => SELECT * FROM schema1.table1;
    id
    ---
    (0 rows)
    
  7. Joe 修改 schema1 以包含权限,以便在 schema1 中创建的所有表都继承架构权限:

    
    =>\c - Joe
    You are now connected as user Joe
    => ALTER SCHEMA schema1 DEFAULT INCLUDE PRIVILEGES;
    ALTER SCHEMA
    => CREATE TABLE schema1.table2 (id int);
    CREATE TABLE
    
  8. 启用继承的权限后,Myra 可以查询 table2,而 Joe 不必显式授予对表的权限:

    
    =>\c - Myra
    You are now connected as user Myra
    => SELECT * FROM schema1.table2;
    id
    ---
    (0 rows)