Postgres import csv duplicate keys

753 views Asked by At

I have a table called measurement with 3 columns: value, moment, seriesid.

51.02|2006-12-31 23:00:00|1
24.88|2006-12-31 23:00:00|2
55|2006-12-31 23:00:00|3
3.34823004011|2006-12-31 23:00:00|5

I am trying to load a csv in this table and I am getting the following error:

Key (moment, seriesid)=(2009-05-25 00:00:00,186) already exists.

After reading some posts here on StackOverflow, best I managed to do was this:

CREATE TEMP TABLE measurement_tmp AS SELECT * FROM measurement LIMIT 0;
COPY measurement_tmp FROM '/home/airquality/dat/import.csv'
WITH DELIMITER ',';

INSERT INTO measurement
SELECT DISTINCT ON (moment,seriesid)
value,moment,seriesid
FROM measurement_tmp

As far as I understand
1) A table measurement_tmp is created.
2) All contents of the measurement table are loaded in measeurement_tmp
3) All contents of the import.csv file are loaded in measurement_tmp without Key (moment, seriesid) restriction.
4) Selecting DISTINCT ON (moment, seriesid) should only return only 'sane' data and import them in measurement.

Still getting the same error,

2014-11-20 10:06:24 GMT-2 ERROR:  duplicate key value violates unique constraint
 "measurement_pkey"
2014-11-20 10:06:24 GMT-2 DETAIL:  Key (moment, seriesid)=(2009-05-25 00:00:00,
186) already exists.

Any ideas?

0

There are 0 answers