Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Last active December 14, 2015 11:28
Show Gist options
  • Select an option

  • Save sandcastle/be4ee59c3b7ec6c0792f to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/be4ee59c3b7ec6c0792f to your computer and use it in GitHub Desktop.
A PostgreSQL helper script for changing the owner of all objects in a schema.
-- change the "new_owner" role before running
SELECT 'ALTER TABLE '|| schemaname || '.' || tablename ||' OWNER TO new_owner;'
FROM pg_tables WHERE NOT schemaname IN ('pg_catalog', 'information_schema')
ORDER BY schemaname, tablename;
SELECT 'ALTER SEQUENCE '|| sequence_schema || '.' || sequence_name ||' OWNER TO new_owner;'
FROM information_schema.sequences WHERE NOT sequence_schema IN ('pg_catalog', 'information_schema')
ORDER BY sequence_schema, sequence_name;
SELECT 'ALTER VIEW '|| table_schema || '.' || table_name ||' OWNER TO new_owner;'
FROM information_schema.views WHERE NOT table_schema IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment