dbTalk Databases Forums  

making a copy of a table within the same database

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss making a copy of a table within the same database in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sally Sally
 
Posts: n/a

Default making a copy of a table within the same database - 03-03-2004 , 09:00 AM






I wanted to dump the contents of one table to another (with a different
name) within the same database. I am looking at pg_restore and it doesn't
seem to have the option of specifying the name of the table we want to dump
to, only the name we want to dump from. Does this mean I have to create the
table and do an sql statement to copy the table? Is this the best way?
Sally

__________________________________________________ _______________
Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
(Limited-time Offer) http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/


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


Reply With Quote
  #2  
Old   
Pavel Stehule
 
Posts: n/a

Default Re: making a copy of a table within the same database - 03-03-2004 , 09:11 AM






Hello

select into is usefull cmd for you

http://developer.postgresql.org/docs...electinto.html

select * into newtable from oldtable;

regards
Pavel Stehule


On Wed, 3 Mar 2004, Sally Sally wrote:

Quote:
I wanted to dump the contents of one table to another (with a different
name) within the same database. I am looking at pg_restore and it doesn't
seem to have the option of specifying the name of the table we want to dump
to, only the name we want to dump from. Does this mean I have to create the
table and do an sql statement to copy the table? Is this the best way?
Sally

__________________________________________________ _______________
Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
(Limited-time Offer) http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/


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


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #3  
Old   
Mike Mascari
 
Posts: n/a

Default Re: making a copy of a table within the same database - 03-03-2004 , 09:19 AM



Sally Sally wrote:

Quote:
I wanted to dump the contents of one table to another (with a different
name) within the same database. I am looking at pg_restore and it
doesn't seem to have the option of specifying the name of the table we
want to dump to, only the name we want to dump from. Does this mean I
have to create the table and do an sql statement to copy the table? Is
this the best way?
If you want a copy of the data and the fundamental table design, not
including constraints, triggers, indexes, foreign keys, etc.:

CREATE TABLE foo AS
SELECT * FROM bar;

....

Mike Mascari


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



Reply With Quote
  #4  
Old   
Jan Poslusny
 
Posts: n/a

Default Re: making a copy of a table within the same database - 03-03-2004 , 09:19 AM



create table newtable as select * from oldtable;


Sally Sally wrote:

Quote:
I wanted to dump the contents of one table to another (with a
different name) within the same database. I am looking at pg_restore
and it doesn't seem to have the option of specifying the name of the
table we want to dump to, only the name we want to dump from. Does
this mean I have to create the table and do an sql statement to copy
the table? Is this the best way?
Sally

__________________________________________________ _______________
Fast. Reliable. Get MSN 9 Dial-up - 3 months for the price of 1!
(Limited-time Offer)
http://click.atdmt.com/AVE/go/onm00200361ave/direct/01/


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


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
  #5  
Old   
Karl O. Pinc
 
Posts: n/a

Default Re: making a copy of a table within the same database - 03-03-2004 , 09:22 AM




On 2004.03.03 09:00 Sally Sally wrote:
Quote:
I wanted to dump the contents of one table to another (with a
different name) within the same database. I am looking at pg_restore
and it doesn't seem to have the option of specifying the name of the
table we want to dump to, only the name we want to dump from. Does
this mean I have to create the table and do an sql statement to copy
the table? Is this the best way?
You might want to look at SELECT INTO ... .

Karl <kop (AT) meme (DOT) com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

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



Reply With Quote
  #6  
Old   
Nick Barr
 
Posts: n/a

Default Re: making a copy of a table within the same database - 03-03-2004 , 09:25 AM



Karl O. Pinc wrote:

Quote:
On 2004.03.03 09:00 Sally Sally wrote:

I wanted to dump the contents of one table to another (with a
different name) within the same database. I am looking at pg_restore
and it doesn't seem to have the option of specifying the name of the
table we want to dump to, only the name we want to dump from. Does
this mean I have to create the table and do an sql statement to copy
the table? Is this the best way?


You might want to look at SELECT INTO ... .

Karl <kop (AT) meme (DOT) com
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

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

http://www.postgresql.org/docs/7.4/s...electinto.html
http://www.postgresql.org/docs/7.4/s...tetableas.html

for more info.

Nick



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



Reply With Quote
  #7  
Old   
Oliver Elphick
 
Posts: n/a

Default Re: making a copy of a table within the same database - 03-03-2004 , 09:37 AM



On Wed, 2004-03-03 at 15:00, Sally Sally wrote:
Quote:
I wanted to dump the contents of one table to another (with a different
name) within the same database. I am looking at pg_restore and it doesn't
seem to have the option of specifying the name of the table we want to dump
to, only the name we want to dump from. Does this mean I have to create the
table and do an sql statement to copy the table? Is this the best way?
There are several ways to do it, depending on what you want.

You can create a new table (with no constraints):

SELECT * INTO new_table FROM old_table;

Or create the new table with any necessary constraints, then:

INSERT INTO new_table SELECT * FROM old_table;

Or dump to text and edit the dump file to change all occurrences of the
table name:

pg_dump -d my_database -t old_table > dump.sql
vi dump.sql
psql -d my_database < dump.sql

or edit on the fly (if the old table name doesn't occur except as a
table name):

pg_dump -d my_database -t old_table |
sed -e 's/old_table/new_table/g' |
psql -d my_database
--
Oliver Elphick <olly (AT) lfix (DOT) co.uk>
LFIX Ltd


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.