Question

I'm trying to import words from a CSV file. some of its records are already in the table. My Query is as below:

COPY words(word,definition,category) FROM '/home/username/Downloads/New1Names.csv' DELIMITER ',' CSV;  

Word column is unique. I receive an error in case of duplicate as below:

ERROR:  duplicate key value violates unique constraint "words_word_u"
DETAIL:  Key (word)=(johnson) already exists.
CONTEXT:  COPY words, line 1: "word definition is here,male"
Was it helpful?

Solution

UPDATED: You can do it this way

-- begin a transaction
BEGIN;
-- create a temporary table based on a factual table `words`
CREATE TEMP TABLE words_temp AS
SELECT word, definition, category 
  FROM words 
  WITH NO DATA
-- import data from the file into the temporary table
COPY words(word,definition,category) 
FROM '/home/username/Downloads/New1Names.csv' DELIMITER ',' CSV;  
-- prevent other concurrent writer processes to make changes while allowing to select from it
LOCK TABLE words IN EXCLUSIVE MODE;
-- insert from temporary into factual table only words that don't yet exist
INSERT INTO words(word,definition,category)
SELECT word,definition,category 
  FROM words_temp t 
 WHERE NOT EXISTS
(
  SELECT * 
    FROM words 
   WHERE word = t.word
);
-- commit the transaction and release the lock
COMMIT;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top