How do I parametrize DDL statements in a plpgsql procedure? Namely, I want to use the org_label
param as the role and schema name but the code below takes it literally and I end up with the role "org_label":
create or replace procedure create_org_schema (org_label varchar(63)) -- max identifier len in pgsql is 63
language plpgsql AS $$ BEGIN
create role org_label IN GROUP organization;
GRANT org_label TO ardoq_api_customers; -- so that AUTHORIZATION... below works
CREATE SCHEMA AUTHORIZATION org_label;
SET ROLE org_label;
GRANT USAGE on SCHEMA org_label to ardoq_api_customers; -- so that we can list it
RESET ROLE;
END $$;
I have tried to change the first line to:
EXECUTE 'create role $1 IN GROUP organization' USING org_label;
But that fails with:
ERROR: syntax error at or near "$1" at character 13 QUERY: create role $1 IN GROUP organization