473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Moving tables between schemas


Hi,

In my database, I created a lot of tables before I found out about
schemas. It's a mess!

Suppose I want to get organised, and create a couple of schemas. How do
I move existing tables into my newly created schemas?

e.g.

public.table1 -> mynewschema1.table1
public.table2 -> mynewschema2.table2

?

Thanks.

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

Nov 23 '05 #1
5 1775
On Friday 30 Jul 2004 3:56 pm, m.********@lse.ac.uk wrote:
Hi,

In my database, I created a lot of tables before I found out about
schemas. It's a mess!

Suppose I want to get organised, and create a couple of schemas. How do
I move existing tables into my newly created schemas?

e.g.

public.table1 -> mynewschema1.table1
public.table2 -> mynewschema2.table2


You can dump the tables, edit the dump to alter the create table statement so
that the table is created in proper schema.

This could be very rudimentary depending upon size of each table and number of
tables. The local documentation of 7.4.3 indicates that either alter table or
alter schema does not support moving tables between schemas.

You could resort to catalog hacking and change the schema manually but I am
not sure how advisable and predictable it would be.

HTH

Shridhar

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2
You may be able to play around with the system catalogs, but I don't
know anything about that...

An easy method of moving the tables are

create table mynewschema1.table1 as select * from public.table1;
drop public.table1;

create table mynewschema2.table2as select * from public.table2;
drop public.table2;

You may need to recreate any indexes that the tables used in the public
schema, and you may need to grant rights in order to allow your users to
access the tables...

Hope that helps

John Sidney-Woollett

create

m.********@lse.ac.uk wrote:

Hi,

In my database, I created a lot of tables before I found out about
schemas. It's a mess!

Suppose I want to get organised, and create a couple of schemas. How do
I move existing tables into my newly created schemas?

e.g.

public.table1 -> mynewschema1.table1
public.table2 -> mynewschema2.table2

?

Thanks.

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


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3
Thanks. I hadn't thought of that - that's probably easiest.

On Fri, 2004-07-30 at 10:19, John Sidney-Woollett wrote:
You may be able to play around with the system catalogs, but I don't
know anything about that...

An easy method of moving the tables are

create table mynewschema1.table1 as select * from public.table1;
drop public.table1;

create table mynewschema2.table2as select * from public.table2;
drop public.table2;

You may need to recreate any indexes that the tables used in the public
schema, and you may need to grant rights in order to allow your users to
access the tables...

Hope that helps

John Sidney-Woollett

create

m.********@lse.ac.uk wrote:

Hi,

In my database, I created a lot of tables before I found out about
schemas. It's a mess!

Suppose I want to get organised, and create a couple of schemas. How do
I move existing tables into my newly created schemas?

e.g.

public.table1 -> mynewschema1.table1
public.table2 -> mynewschema2.table2

?

Thanks.

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


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #4
Be careful with any objects (functions, triggers) to make sure that are
updated to reference the new tables in their correct schemas too.

JOhn Sidney-Woollett

m.********@lse.ac.uk wrote:
Thanks. I hadn't thought of that - that's probably easiest.

On Fri, 2004-07-30 at 10:19, John Sidney-Woollett wrote:
You may be able to play around with the system catalogs, but I don't
know anything about that...

An easy method of moving the tables are

create table mynewschema1.table1 as select * from public.table1;
drop public.table1;

create table mynewschema2.table2as select * from public.table2;
drop public.table2;

You may need to recreate any indexes that the tables used in the public
schema, and you may need to grant rights in order to allow your users to
access the tables...

Hope that helps

John Sidney-Woollett

create

m.********@lse.ac.uk wrote:

Hi,

In my database, I created a lot of tables before I found out about
schemas. It's a mess!

Suppose I want to get organised, and create a couple of schemas. How do
I move existing tables into my newly created schemas?

e.g.

public.table1 -> mynewschema1.table1
public.table2 -> mynewschema2.table2

?

Thanks.

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


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #5
On Friday 30 Jul 2004 3:49 pm, John Sidney-Woollett wrote:
You may be able to play around with the system catalogs, but I don't
know anything about that...

An easy method of moving the tables are

create table mynewschema1.table1 as select * from public.table1;
drop public.table1;

create table mynewschema2.table2as select * from public.table2;
drop public.table2;


One can also create a table before hand using 'create table like' and then
inserting rows. This has advantage of preserving any constraints in original
table.

But this does not take care of indexes AFAIK.

Shridhar

---------------------------(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 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1110
by: joshsackett | last post by:
Hi all, I have 5 databases, each about 20GB in size. I need to copy the data to a new server for reporting purposes. Initially I wanted to setup transactional replication; however, the database...
30
9774
by: btober | last post by:
Whenever I create a temporary table, with something like CREATE TEMPORARY TABLE temptable1 AS SELECT * FROM paid.ad_hoc_query; New schemas appear, with names like "pg_temp_1". I guess the...
34
5001
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
2
5546
by: db2dude | last post by:
Hello all, I have a database around 600GB and all tables were self-created by the application. Hence, we have only one huge SMS tablespace where all the tables are stored. To get better...
4
4806
by: Gladiator | last post by:
hi everyone..... Can anyone tell how to move tables between schemas.......... that is changing the schema name of the table................ thanks in advance.... Kamal
2
38909
by: fuzzybr80 | last post by:
I am using MySQL 5.0 with a number of innodb tables whose ibdata files are growing quite quickly and filling up the /var partition (file is /var/mysql/ibdata1). Earlier on I followed instructions...
12
6962
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
7
4636
by: Gary | last post by:
Hello guys! Bear with me, I am a newbie. She is the Data Warehouse manager. She has about 50 users to use the Oracle database from M$ Access via ODBC connection. All those users have only...
0
1391
by: JB | last post by:
Hi All, I'm writing an application that connects to various databases via ODBC. I'd like to get a list of all the available Schemas and Tables in a Database. It looks like the GetSchema...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.