How to say to postgres to ignore words if it's duplicate?

155 views Asked by At

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"
1

There are 1 answers

3
peterm On BEST ANSWER

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;