I have a table that holds clients, I have just one client per country, and for each row in this table I must create a new schema and replicate some tables. Like this:
Clients table's columns:
client country
---------- -----------
john doe US
jane doe UK
Schemas:
clients_US
clients_UK
I need to create a trigger to create those schemas automatically everytime a new client is added to the clients table, using the country column data as part of the schema’s name.
In a perfect world this would work, but it clearly doesn’t:
CREATE OR REPLACE FUNCTION gcpmanager.create_sle_schema()
RETURNS trigger LANGUAGE plpgsql
AS
'begin
CREATE SCHEMA "clients_" + new.country
AUTHORIZATION postgres;
return new;
end;';
You will need to use dynamic SQL
Btw: the string concatenation operator in PostgreSQL is
||not+(that is for numbers)