Is there some way to create a user/role/group in PostgreSQL via SQL only if it does not exist yet? The only solution I have found so far is creating a custom stored procedure, since that supports conditionals. Is there a simpler solution that I have overlooked? Thank you!
1 Answer
You can use a DO block to only create the role if it doesn't exist, no need for a procedure:
do
$$
begin
if not exists (select * from pg_user where usename = 'new_role') then
create role new_role password '...';
end if;
end
$$
;
-
-
is there any way to wrap this into a loop for creating several roles? dba.stackexchange.com/questions/319704/…– vrmsNov 15, 2022 at 12:13