我无法GRANT
将current_database()
其作为参数。这些都失败并出现相同的错误:
GRANT ALL PRIVILEGES ON DATABASE current_database() to justintestin;
GRANT ALL PRIVILEGES ON DATABASE (current_database()) to justintestin;
GRANT ALL PRIVILEGES ON DATABASE (Select current_database()) to justintestin;
GRANT ALL PRIVILEGES ON DATABASE format('%I', current_database()) to justintestin;
SQL 错误 [42601]:错误:在“(”处或附近出现语法错误
目前,我使用EXECUTE
:
Do
$$
Begin
Execute Format('GRANT ALL PRIVILEGES ON DATABASE %I TO %I', current_database(), 'justintestin');
End;
$$;
但挑剔的我知道一定有更简单的方法。在官方 postgres:16 Docker 镜像上运行:
x86_64-pc-linux-gnu 上的 PostgreSQL 16.2(Debian 16.2-1.pgdg120+2),由 gcc(Debian 12.2.0-14)12.2.0,64 位”*)编译。
对于“实用程序”命令,字符串插值通常是不可能的。您必须以文本形式插入值,即使它们只是数据值。
抱歉,您内心如此挑剔,但使用动态 SQL
EXECUTE
(就像您拥有的那样)已经是可行的方法,这也许仍然令人感到安慰。看: