Skip to content

Instantly share code, notes, and snippets.

@Jonty
Created June 10, 2016 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonty/ef8943e6a89aa37b30d2816f0e07a84a to your computer and use it in GitHub Desktop.
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.
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