I would like to create users (if not exists) in postgresql server in this way:
$block$
DECLARE
user_name varchar := 'qa_user';
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_catalog.pg_roles
WHERE rolname = user_name)
THEN
CREATE USER user_name WITH PASSWORD 'a_random_password';
END IF;
END
$block$
This is an anonymous block because I would like to execute in a more complex database migration
task. I have simplified it to better understanding.
The problem this anonymous block, even executed on PgAdmin
, is creating the user user_name
in database instead of qa_user
.
Any idea what I am doing wrong?