Created
May 21, 2014 02:23
-
-
Save johngrimes/75b0bc16c05ba9310f4b to your computer and use it in GitHub Desktop.
Change the owner of all relations within a PostgreSQL database
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
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; |
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 exec(text) returns text language plpgsql volatile | |
AS $f$ | |
BEGIN | |
EXECUTE $1; | |
RETURN $1; | |
END; | |
$f$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Borrowed from http://stackoverflow.com/a/6852484/1760540