【问题标题】:Auto reassign owner when create table in Postgres在 Postgres 中创建表时自动重新分配所有者
【发布时间】:2021-01-10 17:29:18
【问题描述】:

我有架构 sandbox 的 Postgres 数据库,其中角色 bi_developer 拥有所有授权。

真实用户具有个人角色,其中bi_developer 是父级。

但是当一些用户创建一个新表时,其他用户没有权限。我不时为bi_developer 制作GRANT ALL ....

有没有办法自动完成?

【问题讨论】:

  • Postgres 还是 Greenplum?它们完全不同
  • 看看这个Default Privileges是否适合你。

标签: postgresql greenplum grant


【解决方案1】:

我们尝试了不同的变体,包括默认权限,但在我们的 Greenplum 安装中没有任何效果,现在我们使用触发器,将所有新表转移到 read_only 角色:

-- CREATE FUNCTION
CREATE OR REPLACE FUNCTION sandbox.set_object_owner()
  RETURNS event_trigger
  LANGUAGE plpgsql
AS $$
  DECLARE
    obj record;
BEGIN
  FOR obj IN SELECT schemaname, objname, subtype 
    from pg_stat_operations 
        where schemaname = 'sandbox' 
    and actionname = 'CREATE'
    and subtype in ('TABLE', 'VIEW') 
  loop
    if obj.subtype = 'TABLE'
    then
      EXECUTE format('ALTER TABLE %s.%s OWNER TO read_only', obj.schemaname, obj.objname);
    end if;
    if obj.subtype = 'VIEW'
    then 
      EXECUTE format('ALTER VIEW %s.%s OWNER TO read_only', obj.schemaname, obj.objname);
    end if;
  END LOOP;
END;
$$

-- CREATE TRIGGER
CREATE EVENT TRIGGER trg_set_owner
  ON ddl_command_end
  WHEN tag IN ('CREATE TABLE', 'CREATE TABLE AS', 'CREATE VIEW')
EXECUTE PROCEDURE sandbox.set_object_owner();

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    相关资源
    最近更新 更多