By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,688 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,688 IT Pros & Developers. It's quick & easy.

Transfer database tables to a schema

P: n/a
Folks,
I have a couple of databases that should really be schemas in the same
database. I tried to find suggestions on how to easily move all the
tables/sequences/etc. from the public schema in a database to a different
schema in a different database, but came up blank. If anyone has a
suggestion it would be appreciated.
Thanks,
Peter Darley
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
Try this:
(tested for PostgreSQL 7.4)
CREATE OR REPLACE FUNCTION move_relation(VARCHAR, VARCHAR, VARCHAR)
RETURNS BOOL
AS '
-- $1 is the table name
-- $2 is the source schema
-- $3 is the destination schema
--
UPDATE pg_catalog.pg_class
SET relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
WHERE nspname = $3)
WHERE relnamespace = (SELECT oid FROM pg_catalog.pg_namespace
WHERE nspname = $2)
AND relname = $1;

UPDATE pg_catalog.pg_type
SET typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
WHERE nspname = $3)
WHERE typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
WHERE nspname = $2)
AND typname = $1;

SELECT TRUE;
' LANGUAGE SQL;
----- Original Message -----
From: "Peter Darley" <pd*****@kinesis-cem.com>
To: "Pgsql-General" <pg***********@postgresql.org>
Sent: Tuesday, February 03, 2004 2:47 AM
Subject: [GENERAL] Transfer database tables to a schema

Folks,
I have a couple of databases that should really be schemas in the same
database. I tried to find suggestions on how to easily move all the
tables/sequences/etc. from the public schema in a database to a different
schema in a different database, but came up blank. If anyone has a
suggestion it would be appreciated.
Thanks,
Peter Darley
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 22 '05 #2

P: n/a
"Chris Travers" <ch***@travelamericas.com> writes:
Try this:


Er ... what about the indexes belonging to the relation?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #3

P: n/a
Ok, I see what you mean. I was looking at pg_index, when the indexes are
also listed in pg_class. I will have to add this and the pg_depend stuff.

Best Wishes,
Chris Travers

----- Original Message -----
From: "Tom Lane" <tg*@sss.pgh.pa.us>
To: "Chris Travers" <ch***@travelamericas.com>
Cc: "Peter Darley" <pd*****@kinesis-cem.com>; "Pgsql-General"
<pg***********@postgresql.org>
Sent: Wednesday, February 04, 2004 12:47 PM
Subject: Re: [GENERAL] Transfer database tables to a schema

"Chris Travers" <ch***@travelamericas.com> writes:
Try this:


Er ... what about the indexes belonging to the relation?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.