Created
December 16, 2022 05:12
-
-
Save kilfu0701/efd19af29602b56afc4989cad4f815e2 to your computer and use it in GitHub Desktop.
[Postgres] Event trigger on table created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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