4

I'm using PostgreSQL database, after I've created my table I have to populate them with a CSV file. However the CSV file is corrupted and it violates the primary key rule and so the database is throwing an error and I'm unable to populate the table. Any ideas how to tell the database to ignore the duplicates when importing from CSV? Writing a script to remove them from the CSV file is no acceptable. Any workarounds are welcome too. Thank you! : )

jww
  • 97,681
  • 90
  • 411
  • 885
Anton Belev
  • 11,963
  • 22
  • 70
  • 111

1 Answers1

3

On postgreSQL, duplicate rows are not permitted if they violate a unique constraint. I think that your best option, is to import your CSV file on to temp table that has no constraint, delete from it duplicate values, and finally import from this temp table to your final table.

Houari
  • 5,326
  • 3
  • 31
  • 54
  • That was me other option ok, I'll do that then : ) Thanks! – Anton Belev Jan 31 '14 at 17:11
  • 4
    @AntonBelev To learn more, search for "postgresql bulk upsert". See http://stackoverflow.com/questions/17267417/how-do-i-do-an-upsert-merge-insert-on-duplicate-update-in-postgresql, http://stackoverflow.com/questions/7019831/bulk-batch-update-upsert-in-postgresql – Craig Ringer Feb 01 '14 at 04:27