DO $$ DECLARE r RECORD; BEGIN -- Loop through all tables in the schema that have the 'updated_date' column FOR r IN ( SELECT table_name FROM information_schema.columns WHERE column_name = 'updated_date' AND table_schema = 'gepafin_schema' ) LOOP EXECUTE format( 'CREATE OR REPLACE TRIGGER tg_gepafin_schema_updated_date_%I BEFORE UPDATE ON gepafin_schema.%I FOR EACH ROW EXECUTE FUNCTION gepafin_schema.clock_timestamp_updated_date_column()', r.table_name, r.table_name ); END LOOP; -- Loop through all tables in the schema that have the 'created_date' column FOR r IN ( SELECT table_name FROM information_schema.columns WHERE column_name = 'created_date' AND table_schema = 'gepafin_schema' ) LOOP EXECUTE format( 'CREATE OR REPLACE TRIGGER tg_gepafin_schema_created_date_%I BEFORE INSERT ON gepafin_schema.%I FOR EACH ROW EXECUTE FUNCTION gepafin_schema.clock_timestamp_created_date_column()', r.table_name, r.table_name ); END LOOP; END; $$ LANGUAGE plpgsql;