数据仓库服务 GAUSSDB(DWS)-ALTER DEFAULT PRIVILEGES:示例

时间:2024-12-24 10:32:41

示例

创建用户:

1
CREATE USER jack PASSWORD '{Password}';

创建模式:

1
CREATE SCHEMA tpcds;
  • 将创建在模式tpcds里的所有表(和视图)的SELECT权限授予每一个用户:
    1
    ALTER DEFAULT PRIVILEGES IN SCHEMA tpcds GRANT SELECT ON TABLES TO PUBLIC;
    
  • 将tpcds下的所有表的插入权限授予用户jack:
    1
    ALTER DEFAULT PRIVILEGES IN SCHEMA tpcds GRANT INSERT ON TABLES TO jack;
    
  • 撤销上述权限:
    1
    2
    ALTER DEFAULT PRIVILEGES IN SCHEMA tpcds REVOKE SELECT ON TABLES FROM PUBLIC; 
    ALTER DEFAULT PRIVILEGES IN SCHEMA tpcds REVOKE INSERT ON TABLES FROM jack;
    
  • 假设有两个用户test1、test2,如果想要test2用户对test1用户未来创建的表都有查询权限可以用如下操作:
    • 创建用户test1、test2:
      1
      2
      CREATE USER test1 PASSWORD '{Password}';
      CREATE USER test2 PASSWORD '{Password}';
      
    • 首先,把test1的schema的权限赋权给test2用户:
      1
      GRANT usage, create ON SCHEMA test1 TO test2;
      
    • 其次,把test1用户下的表的查询权限赋值给test2用户:
      1
      ALTER DEFAULT PRIVILEGES FOR USER test1 IN SCHEMA test1 GRANT SELECT ON tables TO test2;
      
    • 然后,test1用户创建表:
      1
      2
      SET ROLE test1 password '{Password}';
      CREATE TABLE test3( a int, b int);
      
    • 最后,用test2用户去查询:
      1
      2
      SET ROLE test2 password '{Password}';
      SELECT * FROM test1.test3;
      
support.huaweicloud.com/sqlreference-dws/dws_06_0244.html