Skip to content

Instantly share code, notes, and snippets.

@kilfu0701
Created December 16, 2022 05:12
Show Gist options
  • Save kilfu0701/efd19af29602b56afc4989cad4f815e2 to your computer and use it in GitHub Desktop.
Save kilfu0701/efd19af29602b56afc4989cad4f815e2 to your computer and use it in GitHub Desktop.
[Postgres] Event trigger on table created
CREATE FUNCTION public.on_create_table_funct()
RETURNS event_trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF SECURITY DEFINER
AS $BODY$
DECLARE
sch text;
BEGIN
FOR sch IN SELECT nspname FROM pg_namespace WHERE nspname NOT LIKE 'pg%' AND nspname NOT LIKE 'information%' AND nspname = 'public'
LOOP
EXECUTE format($$ GRANT SELECT,INSERT,UPDATE,REFERENCES,TRIGGER ON ALL TABLES IN SCHEMA %I TO readonlybase $$, sch);
END LOOP;
END;
$BODY$;
ALTER FUNCTION public.on_create_table_funct() OWNER TO postgres;
GRANT EXECUTE ON FUNCTION public.on_create_table_funct() TO postgres;
GRANT EXECUTE ON FUNCTION public.on_create_table_funct() TO PUBLIC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment