-2

Please help to create postgresql query equal to mysql query

LOAD DATA LOCAL INFILE 'file.txt' REPLACE INTO TABLE newtable  TERMINATED BY ',' IGNORE 1 LINES;
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
user2584292
  • 19
  • 1
  • 5

2 Answers2

3

There is no equivalent feature in PostgreSQL - at least in the current 9.3 or any prior version.

You must do this in a few steps:

  • CREATE TEMPORARY TABLE ...
  • COPY into the temp table
  • Do an UPDATE ... FROM followed by an INSERT INTO ... WHERE NOT EXISTS (...) to merge data
  • DROP the temp table

Search for "postgresql bulk upsert" or "postgresql copy upsert".

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Can you show me exact query in step3 Do an UPDATE ... FROM followed by an INSERT INTO ... WHERE NOT EXISTS (...) to merge data – user2584292 Aug 05 '13 at 10:12
0

you might be looking for COPY

COPY will be run by the PostgreSQL backend (user "postgres"). The backend user requires permissions to read & write to the data file in order to copy from/to it. You need to use an absolute pathname with COPY. \COPY on the other hand, runs under the current $USER, and with that users environment. And \COPY can handle relative pathnames. The psql \COPY is accordingly much easier to use if it handles what you need.

Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70