update table from csv file

From: Kirk Wythers <kwythers(at)umn(dot)edu>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: update table from csv file
Date: 2012-12-27 16:31:43
Message-ID: 9A144583-72ED-4B9B-BC9A-B89970750DBA@umn.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have been using COPY FROM to do a mass import of records from CSV files into a new database. I have discover however, a small number of records ( a few thousand) in one of the files that contain new data that needs to be added to the database, but on rows that have a primary key and have already been inserted (so I can't use COPY FROM because it violates the primary key).

If the structure of the table is

id data1 data2 data3

and the structure of the CSV file is

id data1 data2 data3

and I need to update all the rows in data3 where the id = id.

I have created a temporary table and used COPY FROM to load the update data into the temporary table. I seem to be stuck however. I thought I should be able to use the UPDATE command to update all columns and all rows the table.id = tmp_table.id

Something like:

UPDATE table FROM tmp_table WHERE table.id = tmp_table.id;

Or am I completely off course?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2012-12-27 16:39:00 Re: update table from a csv file
Previous Message Kirk Wythers 2012-12-27 16:27:01 update table from a csv file