pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <Kevin.Grittner@wicourts.gov>
To: Lance Campbell <lance@illinois.edu>
To: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: Re: How to change all owners on all objects in a schema
Date: Thu, 23 Jun 2011 15:39:12 -0500
Message-ID: <4E035E20020000250003EB41@gw.wicourts.gov> (raw)
In-Reply-To: <B75CD08C73BD3543B97E4EF3964B7D70077BB4@CITESMBX2.ad.uillinois.edu>
References: <B75CD08C73BD3543B97E4EF3964B7D70077BB4@CITESMBX2.ad.uillinois.edu>

"Campbell, Lance" <lance@illinois.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



view thread (4+ messages)  latest in thread

Message-ID: <4E035E20020000250003EB41@gw.wicourts.gov>
Permalink:  ../4E035E20020000250003EB41@gw.wicourts.gov/
Also on:    postgresql.org/message-id/4E035E20020000250003EB41@gw.wicourts.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: Kevin.Grittner@wicourts.gov, lance@illinois.edu
  Subject: Re: How to change all owners on all objects in a schema
  In-Reply-To: <4E035E20020000250003EB41@gw.wicourts.gov>
  References: <B75CD08C73BD3543B97E4EF3964B7D70077BB4@CITESMBX2.ad.uillinois.edu> <4E035E20020000250003EB41@gw.wicourts.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox