Skip to content

Instantly share code, notes, and snippets.

@johngrimes
Created May 21, 2014 02:23
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 johngrimes/75b0bc16c05ba9310f4b to your computer and use it in GitHub Desktop.
Save johngrimes/75b0bc16c05ba9310f4b to your computer and use it in GitHub Desktop.
Change the owner of all relations within a PostgreSQL database
SELECT exec('ALTER TABLE ' || quote_ident(s.nspname) || '.' || quote_ident(s.relname) || ' OWNER TO $NEWUSER')
FROM (SELECT nspname, relname
FROM pg_class c JOIN pg_namespace n ON (c.relnamespace = n.oid)
WHERE nspname NOT LIKE E'pg\\_%' AND
nspname <> 'information_schema' AND
relkind IN ('r','S','v') ORDER BY relkind = 'S'
) s;
CREATE FUNCTION exec(text) returns text language plpgsql volatile
AS $f$
BEGIN
EXECUTE $1;
RETURN $1;
END;
$f$;
@johngrimes
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment