是否有某种方法可以通过 SQL 在 PostgreSQL 中创建用户/角色/组(仅当用户/角色/组尚不存在时)?到目前为止,我找到的唯一解决方案是创建自定义存储过程,因为它支持条件。有没有我忽略的更简单的解决方案?谢谢你!
小智 8
您可以使用 DO 块仅在角色不存在时创建角色,无需过程:
do
$$
begin
if not exists (select * from pg_user where usename = 'new_role') then
create role new_role password '...';
end if;
end
$$
;
Run Code Online (Sandbox Code Playgroud)