Unix Technical Forum

Copy data from table to table

This is a discussion on Copy data from table to table within the pgsql Novice forums, part of the PostgreSQL category; --> Hi All, I can not think of a way to copy data directly betweeen two tables. I can only ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 08:45 PM
Keith Worthington
 
Posts: n/a
Default Copy data from table to table

Hi All,

I can not think of a way to copy data directly betweeen two tables.

I can only think of two ways to copy the data indirectly.

Method 1: Write a function that loops through the results of a select query
running individual inserts for each record.

Method 2: Use SELECT INTO to put the desired data in a temporoary table; then
use COPY to store the data in a file and finally use COPY to read the data
from the file to the target table.

I have implemented the first method for situations where the source data ends
up in multiple tables but I want to check with all of you prior to creating a
function to do this for a single target table.

As a curious point of information I worked with a database once that allowed
you to SELECT INTO an existing table. It was handy.

Kind Regards,
Keith

---------------------------(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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 08:45 PM
Michael Fuhr
 
Posts: n/a
Default Re: Copy data from table to table

On Wed, Apr 27, 2005 at 09:16:09AM -0400, Keith Worthington wrote:
>
> I can not think of a way to copy data directly betweeen two tables.


Have you tried INSERT ... SELECT?

INSERT INTO foo (a, b, c) SELECT a, b, c FROM bar;

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

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

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 08:45 PM
George Weaver
 
Posts: n/a
Default Re: Copy data from table to table

Hi Keith,

Why not:

INSERT INTO tablea SELECT * FROM tableb;

Or am I missing something about what you're trying to achieve?

Regards,
George


----- Original Message -----
From: "Keith Worthington" <keithw@narrowpathinc.com>
To: "PostgreSQL Novice" <pgsql-novice@postgresql.org>
Sent: Wednesday, April 27, 2005 8:16 AM
Subject: [NOVICE] Copy data from table to table


> Hi All,
>
> I can not think of a way to copy data directly betweeen two tables.
>
> I can only think of two ways to copy the data indirectly.
>
> Method 1: Write a function that loops through the results of a select
> query
> running individual inserts for each record.
>
> Method 2: Use SELECT INTO to put the desired data in a temporoary table;
> then
> use COPY to store the data in a file and finally use COPY to read the data
> from the file to the target table.
>
> I have implemented the first method for situations where the source data
> ends
> up in multiple tables but I want to check with all of you prior to
> creating a
> function to do this for a single target table.
>
> As a curious point of information I worked with a database once that
> allowed
> you to SELECT INTO an existing table. It was handy.
>
> Kind Regards,
> Keith
>
> ---------------------------(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
>




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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 08:45 PM
Frank Bax
 
Posts: n/a
Default Re: Copy data from table to table

At 09:16 AM 4/27/05, Keith Worthington wrote:

>Hi All,
>
>I can not think of a way to copy data directly betweeen two tables.
>
>I can only think of two ways to copy the data indirectly.
>
>Method 1: Write a function that loops through the results of a select query
>running individual inserts for each record.
>
>Method 2: Use SELECT INTO to put the desired data in a temporoary table; then
>use COPY to store the data in a file and finally use COPY to read the data
>from the file to the target table.



INSERT INTO ... (SELECT FROM ...)

http://www.postgresql.org/docs/8.0/i...ql-insert.html

What is the last word in synopsis?


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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 08:46 PM
Keith Worthington
 
Posts: n/a
Default Re: Copy data from table to table

Frank Bax wrote:
> At 09:16 AM 4/27/05, Keith Worthington wrote:
>
>> Hi All,
>>
>> I can not think of a way to copy data directly betweeen two tables.
>>
>> I can only think of two ways to copy the data indirectly.
>>
>> Method 1: Write a function that loops through the results of a select
>> query
>> running individual inserts for each record.
>>
>> Method 2: Use SELECT INTO to put the desired data in a temporoary
>> table; then
>> use COPY to store the data in a file and finally use COPY to read the
>> data
>> from the file to the target table.

>
>
>
> INSERT INTO ... (SELECT FROM ...)
>
> http://www.postgresql.org/docs/8.0/i...ql-insert.html
>


Thank you all very much. I had not run across this command yet. I have
run a few queries using it and it is going to work quite nicely for me.

--
Kind Regards,
Keith

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:52 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com