I am struggling to pass parameter(password) from command line into the script.
I have tested many options, but still it's not working. Should I use some dynamic option here?
\set is not working, declaring it before DO statement also do not work. I am out of ideas, any hint may help! thanks
DO $$
DECLARE
my_password text := **'this need to be passed from CLI';** <--
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'username') THEN
EXECUTE 'CREATE ROLE username LOGIN PASSWORD ' || quote_literal(my_password);
RAISE NOTICE 'Role created successfully.';
ELSE
RAISE NOTICE 'Role already exists.';
END IF;
END $$;
everything from documentation, google
Found a solution ->
set myvars.pass TO :'passwordfromcli';
DO $$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'username') THEN
EXECUTE 'CREATE ROLE username LOGIN PASSWORD ' || quote_literal(current_setting('myvars.pass'));
RAISE NOTICE 'Role created successfully.';
raise notice '%', current_setting('myvars.pass');
ELSE
RAISE NOTICE 'Role already exists.';
raise notice '%', current_setting('myvars.pass');
END IF;
END $$;