Created
June 10, 2016 16:02
-
-
Save Jonty/ef8943e6a89aa37b30d2816f0e07a84a to your computer and use it in GitHub Desktop.
Making postgres tables be default-owned by one "master" user, regardless of who created them.
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
Run as `postgres`: | |
------------------ | |
BEGIN; | |
DO | |
$body$ | |
BEGIN | |
IF NOT EXISTS (SELECT 1 FROM pg_user WHERE usename = 'table_owner') THEN | |
CREATE GROUP table_owner; | |
END IF; | |
END | |
$body$; | |
CREATE OR REPLACE FUNCTION change_owner() RETURNS event_trigger AS $$ | |
BEGIN | |
IF NOT EXISTS (SELECT 1 FROM pg_user WHERE usename = current_user and usesuper = true) THEN | |
REASSIGN OWNED BY current_user TO table_owner; | |
END IF; | |
END; | |
$$ LANGUAGE plpgsql; | |
DROP EVENT TRIGGER IF EXISTS change_owner; | |
CREATE EVENT TRIGGER change_owner ON ddl_command_end EXECUTE PROCEDURE change_owner(); | |
COMMIT; | |
----------- | |
Then on each user who will be creating/modifying tables etc: | |
grant table_owner to USERNAME; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment