Skip to content

Instantly share code, notes, and snippets.

@fabriciocolombo
Last active October 13, 2015 01:47
Show Gist options
  • Save fabriciocolombo/4119771 to your computer and use it in GitHub Desktop.
Save fabriciocolombo/4119771 to your computer and use it in GitHub Desktop.
PostgreSQL - Change Owner
select 'ALTER FUNCTION ' || quote_ident(routine_schema) || '.' || quote_ident(routine_name) || ' OWNER TO ' ||routine_schema || ';'
from information_schema.routines
where specific_schema NOT LIKE E'pg\\_%'
and specific_schema <> 'information_schema'
and specific_schema <> 'public'
/*AND specific_schema = $YourSchema leave commented for all */
union all
SELECT 'ALTER TABLE ' || quote_ident(s.nspname) || '.' || quote_ident(s.relname) || ' OWNER TO ' || nspname || ';'
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 nspname <> 'public'
/*AND nspname = $YourSchema leave commented for all */
and relkind IN ('r', 'S', 'v')
ORDER BY relkind = 'S') s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment