Skip site navigation (1) Skip section navigation (2)

Re: How to copy a schema under another name in same database

From: "Obe, Regina" <robe(dot)dnd(at)cityofboston(dot)gov>
To: "Daniel Gour" <Daniel(dot)Gour(at)adacel(dot)com>,<pgsql-novice(at)postgresql(dot)org>
Subject: Re: How to copy a schema under another name in same database
Date: 2008-07-30 11:14:10
Message-ID: 53F9CF533E1AA14EA1F8C5C08ABC08D20479B7AD@ZDND.DND.boston.cob (view raw or flat)
Thread:
Lists: pgsql-novice
Never had a need for this.  One thought that comes to mind write a
plpgsql function that takes in name of new schema and old schema and
does something like below

--Create new tables in new schema

FOR tbltocopy IN(SELECT table_name 
				FROM information_schema.tables 
				WHERE table_type = 'BASE TABLE' AND
table_schema = param_source_schema ORDER BY table_name)

	EXECUTE('CREATE TABLE ' || param_dest_schema || '.' || tbltocopy
|| '( LIKE ' || param_source_schema || '.' || tbltocopy || ' INCLUDING
DEFAULT CONSTRAINTS INDEXES '); 
INSERT INTO ' || param_dest_schema || '.' || tbltocopy || ' SELECT *
FROM ' param_source_schema || '.' || tbltocopy || ';');

NEXT;


Hope that helps,
Regina



-----Original Message-----
From: pgsql-novice-owner(at)postgresql(dot)org on behalf of Daniel Gour
Sent: Tue 7/29/2008 9:20 AM
To: pgsql-novice(at)postgresql(dot)org
Subject: [NOVICE] How to copy a schema under another name in same
database
 
Hello!  I have a PostgreSQL (8.3.3, WinXP + linux) database with
multiple
schemas.  I would like, on a regular basis, to be able to copy the
structure
and data of one schema under a new schema, using of course a different
name.
What would be the easiest way?

 

-          I have seen the TODO entry "Add CREATE SCHEMA ... LIKE that
copies
a schema", so I guess an easy solution (a single pgsql command) is not
yet
available...

 

-          I guess the alternative is to use pg_dump to dump a single
schema
and use pg_restore, but how can I restore the dumped information under
another schema?  I would like to avoid dumping it as an sql script and
having
it to modify it manually, because this will be a regular operation that
I
would like to automate in my application.

 

Thanks in advance for any insight you can provide!

---------------------------------
Daniel Gour
Adacel Inc.



 


-----------------------------------------
The substance of this message, including any attachments, may be
confidential, legally privileged and/or exempt from disclosure
pursuant to Massachusetts law. It is intended
solely for the addressee. If you received this in error, please
contact the sender and delete the material from any computer.


In response to

pgsql-novice by date

Next:From: Daniel GourDate: 2008-07-30 13:10:11
Subject: Re: How to copy a schema under another name in same database
Previous:From: Bastiaan OlijDate: 2008-07-29 23:05:37
Subject: Re: How to copy a schema under another name in same database

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group