I am using psql tool on Postgres Version 11 and I am trying to use a variable defined in a „main“ script in another script that I am including. Including the 01_doesWork.sql script works fine, but I cannot get script 02_doesNotWork.sql running.
How do I need to define my variables in 00_main.sql and how to need to refer to them in the 02 script?
00_main.sql:
\set roleName 'my_role'
\set dataBase 'my_db'
\i 01_doesWork.sql
\i 02_doesNotWork.sql
01_doesWork.sql:
GRANT CONNECT ON DATABASE :dataBase TO :roleName;
02_doesNotWork.sql:
DO
$$BEGIN
IF EXISTS (SELECT FROM pg_roles WHERE rolname = :roleName) THEN
EXECUTE format('REVOKE CONNECT ON DATABASE %s; FROM %s', :dataBase, :roleName);
END IF;
END$$;
I have tried various ways to set the variables such as
\set roleName = '''my_role'''
\set roleName = '\'my_role\''
and also on the 02 script without format
or using a DECLARE
, e.g.
DO
$$BEGIN
IF EXISTS (SELECT FROM pg_roles WHERE rolname = :'roleName') THEN
EXECUTE 'REVOKE CONNECT ON DATABASE :dataBase FROM :roleName';
END IF;
END$$;