Re: COPY from temp table to main table insted of INSERT INTO

From: Stephen Froehlich <s(dot)froehlich(at)cablelabs(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: COPY from temp table to main table insted of INSERT INTO
Date: 2017-07-11 17:04:02
Message-ID: DM5PR06MB2891B4BF64C2B418AE6862F6E5AE0@DM5PR06MB2891.namprd06.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I have already copied the source data over from R in an unnormalized form and then am normalizing it in SQL (via a series of queries).

Thanks,
Stephen

From: David G. Johnston [mailto:david(dot)g(dot)johnston(at)gmail(dot)com]
Sent: Tuesday, July 11, 2017 11:00 AM
To: Stephen Froehlich <s(dot)froehlich(at)cablelabs(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: [NOVICE] COPY from temp table to main table insted of INSERT INTO

On Tue, Jul 11, 2017 at 9:45 AM, Stephen Froehlich <s(dot)froehlich(at)cablelabs(dot)com<mailto:s(dot)froehlich(at)cablelabs(dot)com>> wrote:
I tend to do mass inserts to my database, but INSERT INTO is taking quite a while for 100k values.

What is the syntax for using the COPY command to copy a well formatted temp table to the “end” of the primary table? I am having trouble understanding https://www.postgresql.org/docs/9.5/static/sql-copy.html.

​Tables don't have beginnings or ends.

Are you intending to use client software to access the source data or are you planning on putting the source data in a location where the server o/s user can see it?​

If you already have an actual temporary table inside the database you wouldn't use COPY. COPY is intended to transfer data from/to an external file (including stdin/stdout).

Generally:

(in psql)
BEGIN;
CREATE TEMP TABLE tmptbl ( cols );
\copy tmptbl from '/tmp/file-to-load.csv' with ( ... )
INSERT INTO tbl SELECT * FROM tmptbl;
COMMIT;

\copy in psql constructs an appropriate "COPY" SQL command, executes it on the server, and then funnels the contents of "file" to the server.

David J.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2017-07-11 17:42:45 Re: COPY from temp table to main table insted of INSERT INTO
Previous Message David G. Johnston 2017-07-11 16:59:40 Re: COPY from temp table to main table insted of INSERT INTO