Re: How to change all owners on all objects in a schema

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Lance Campbell" <lance(at)illinois(dot)edu>, "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: How to change all owners on all objects in a schema
Date: 2011-06-23 20:39:12
Message-ID: 4E035E20020000250003EB41@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

"Campbell, Lance" <lance(at)illinois(dot)edu> wrote:

> PostgreSQL: 9.0.x
>
> How do I change the ownership of all objects within a schema from
> X to Y? The objects within each schema would be tables and
> sequences.

I would use DO statements. What could be done as a one-liner to
handle the tables is shown below with line breaks for readability.

do $$
declare stmt text;
begin
for stmt in
select 'alter table oldschema.'
|| quote_ident(relname)
|| ' set schema newschema;'
from pg_class
where relkind = 'r'
and relnamespace =
(
select oid from pg_catalog.pg_namespace
where nspname = 'oldschema'
)
loop
raise notice '%', stmt;
end loop;
end;
$$;

Similar for sequences, only ALTER SEQUENCE and relkind = 'S'.

-Kevin

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Kevin Grittner 2011-06-23 20:41:44 Re: How to change all owners on all objects in a schema
Previous Message Kasia Tuszynska 2011-06-23 17:47:25 C collation and Template 1