Please help me, how to add a check for an existing duplicate of a unique key, if there is a skip and import the lines with CSV further. Mistake:
psycopg2.IntegrityError: duplicate key value violates uniqueconstraint "dbname_pkey" DETAIL: Key (id)=(1) already exists.
import psycopg2 def importoDB(self): self.conn = psycopg2.connect( host='ip', user='username', password='pass', dbname='dbname') self.cursor = self.conn.cursor() f = open(r'test.csv', 'r') self.cursor.copy_from(f, 'dbname', sep=';') f.close() self.conn.commit()
copy_from(file=f, table='tmp_table', sep=';)2- join tables:'insert into "table" select * from "tmp_table" on conflict do nothing'. Related question To ignore duplicate keys during 'copy from' in postgresql - jfs