如何使用数据(而不是标识符)授予 postgres 权限

How to use data (rather than an identifier) to grant postgres permissions

我想使用以下函数(作为 table 插入的触发器):

CREATE OR REPLACE FUNCTION insert_authid_fn() RETURNS trigger AS $$
BEGIN
  GRANT ALL ON DATABASE testdb TO NEW.username;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

我收到错误:

ERROR:  syntax error at or near "."
LINE 3:   GRANT ALL ON DATABASE testdb TO NEW.username;

因为 New.username 是值,而不是标识符。

我怎样才能做到这一点GRANT

语法上:

CREATE OR REPLACE FUNCTION insert_authid_fn() RETURNS trigger AS $$
BEGIN
  execute format ('GRANT ALL ON DATABASE testdb TO %I',NEW.username);
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

但我怀疑总的来说这是个好主意