Re: INSERTing rows from external file - Mailing list pgsql-general

From David Johnston
Subject Re: INSERTing rows from external file
Date
Msg-id 005a01cc5c5e$e8fa9800$baefc800$@yahoo.com
Whole thread Raw
In response to INSERTing rows from external file  (Rich Shepard <rshepard@appl-ecosys.com>)
List pgsql-general
>> -----Original Message-----
>> From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of Rich Shepard
>> Sent: Tuesday, August 16, 2011 5:34 PM
>> To: pgsql-general@postgresql.org
>> Subject: [GENERAL] INSERTing rows from external file
>>
>>    I have a file with 5500 rows formated as 'INSERT INTO <table>
>> (column_names) VALUES <values>;' that I thought I could read using psql
from the command line. However, the syntax, 'psql <database_name> <
filename.sql'
>>
>>
>> throws an error at the beginning of the first INSERT statement.

Prove It...

I do not use psql in this manner but what you are trying to do should work.
One thing that it may behoove you to do is clean up the file so that the

INSERT INTO table (columns) VALUES

Part only appears once.  Then, for each "VALUES (....);" line you should
replace the semi-colon with a comma (except the final one).

Thus:
INSERT INTO table (col) VALUES (1);
INSERT INTO table (col) VALUES (2);
INSERT INTO table (col) VALUES (3);

Becomes:
INSERT INTO table (col) VALUES (1),
(2),
(3);

This is considerably faster to execute.   5500 rows should be OK to do in a
single statement but anything beyond should probably result in a separate
INSERT being added (and maybe a COMMIT).

David J.




pgsql-general by date:

Previous
From: Greg Smith
Date:
Subject: Re: INSERTing rows from external file
Next
From: Rich Shepard
Date:
Subject: Re: INSERTing rows from external file