Created
March 4, 2017 07:00
-
-
Save brygom/131b4ec33402a4d242c1617d16cee0c3 to your computer and use it in GitHub Desktop.
Change owner to objects in PostgreSQL. Code from Anvesh Patel
This file contains hidden or 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 OR REPLACE FUNCTION ChangeObjectsOwnerShip(text) | |
| returns text language plpgsql volatile | |
| AS $f$ | |
| BEGIN | |
| EXECUTE $1; | |
| RETURN $1; | |
| END; | |
| $f$; | |
| -- Tables, Sequences and Views | |
| SELECT ChangeObjectsOwnerShip | |
| ( | |
| 'ALTER TABLE ' || quote_ident(s.nspname) || '.' ||quote_ident(s.relname) || ' owner TO new_user' | |
| ) | |
| 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; | |
| -- All functions | |
| SELECT ChangeObjectsOwnerShip | |
| ( | |
| 'ALTER FUNCTION ' || quote_ident(s.nspname) || '.' ||quote_ident(s.function_name) || '('||s.parms||') owner TO new_user' | |
| ) | |
| FROM | |
| ( | |
| SELECT | |
| nspname | |
| ,proname AS function_name | |
| , pg_catalog.oidvectortypes(proargtypes) AS parms | |
| FROM pg_catalog.pg_proc AS c | |
| JOIN pg_namespace n | |
| ON (c.pronamespace = n.oid) | |
| WHERE nspname != 'information_schema' | |
| AND nspname NOT LIKE E'pg\\_%' | |
| ORDER BY proname | |
| )s; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment