Re: INSERTing rows from external file

From: "David Johnston" <polobo(at)yahoo(dot)com>
To: "'Rich Shepard'" <rshepard(at)appl-ecosys(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: INSERTing rows from external file
Date: 2011-08-16 21:53:18
Message-ID: 005a01cc5c5e$e8fa9800$baefc800$@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>> -----Original Message-----
>> From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Rich Shepard
>> Sent: Tuesday, August 16, 2011 5:34 PM
>> To: pgsql-general(at)postgresql(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2011-08-16 22:13:56 Re: INSERTing rows from external file
Previous Message Greg Smith 2011-08-16 21:51:53 Re: INSERTing rows from external file