跳到主要内容

使用无超级用户角色的帐户

如果您没有使用具有 superuser 角色(或在使用 Amazon Aurora 或 Amazon RDS 时使用 rds_superuser 角色)的帐户,则需要创建多个对象来捕获数据定义语言 (DDL) 事件。在所选帐户中创建这些对象,然后在主用户帐户中创建触发器。

信息注释在 Google Cloud SQL for PostgreSQL 中使用具有 superuser 角色的帐户需要 Data Movement gateway 2024.11.14 或更高版本。

为此:

  1. 选择要在其中创建对象的模式。默认模式为公共模式。确保模式存在并可由非特权帐户访问。
  2. 运行以下命令创建表 attrep_ddl_audit

    create table <objects_schema>.attrep_ddl_audit
    (
    c_key    bigserial primary key,
    c_time   timestamp,    -- Informational
    c_user   varchar(64),  -- Informational: Current_user
    c_txn    varchar(16),  -- Informational: Current transaction
    c_tag    varchar(24),  -- Either 'CREATE TABLE' or 'ALTER TABLE' or 'DROP TABLE'
    c_oid    integer,      -- For future use - TG_OBJECTID
    c_name   varchar(64),  -- For future use - TG_OBJECTNAME
    c_schema varchar(64),  -- For future use - TG_SCHEMANAME. For now, holds the current_schema
    c_ddlqry  text         -- The DDL query associated with the current DDL event
    );
  3. 运行以下命令创建函数 attrep_intercept_ddl

    CREATE OR REPLACE FUNCTION <objects_schema>.attrep_intercept_ddl()
      RETURNS event_trigger
    LANGUAGE plpgsql
      AS $$
      declare _qry text;
    BEGIN
      if (tg_tag='CREATE TABLE' or tg_tag='ALTER TABLE' or tg_tag='DROP TABLE') then
             SELECT current_query() into _qry;
             insert into <objects_schema>.attrep_ddl_audit
             values
             (
             default,current_timestamp,current_user,cast(TXID_CURRENT()as varchar(16)),tg_tag,0,'',current_schema,_qry
             );
             delete from <objects_schema>.attrep_ddl_audit;
    end if;
    END;
    $$;
    
  4. 如果使用非特权帐户登录,请注销非特权帐户,然后使用已分配 superuser 角色(或使用 Amazon Aurora 或 Amazon RDS 时使用 rds_superuser 角色)的帐户登录。

    信息注释

    如果没有在默认模式中创建 attrep_intercept_ddl 存储的过程,则需要在 PostgreSQL 连接器设置中的以模式创建 DDL 工件字段中指定模式名称。

    有关复制配置参数的更多信息,请参阅 PostgreSQL帮助。

  5. 运行以下命令创建事件触发器 attrep_intercept_ddl

    CREATE EVENT TRIGGER attrep_intercept_ddl ON ddl_command_end

    EXECUTE PROCEDURE <objects_schema>.attrep_intercept_ddl();

  6. 为非特权帐户授予以下权限:

    • GRANT INSERT ON attrep_ddl_audit to <non-privileged-user>;
    • GRANT DELETE ON attrep_ddl_audit to <non-privileged-user>;
    • GRANT USAGE ON attrep_ddl_audit_c_key_seq TO <non-privileged-user>;

    • ALTER ROLE <non-privileged-user> WITH REPLICATION;

    • GRANT rds_replication to <non-privileged-user>;

  7. 授予用户 SELECT 权限:

    GRANT SELECT ON ALL TABLES IN SCHEMA <SCHEMA _NAME> TO <non-privileged-user>;

  8. 授予用户模式使用权:

    GRANT USAGE ON SCHEMA <SCHEMA_NAME> TO <non-privileged-user>;

本页面有帮助吗?

如果您发现此页面或其内容有任何问题 – 打字错误、遗漏步骤或技术错误 – 请告诉我们如何改进!