Re: problem with copy command

From: Joel Burton <jburton(at)scw(dot)org>
To: Jaruwan Laongmal <jaruwan(at)gits(dot)net(dot)th>
Cc: pgsql-hackers(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: problem with copy command
Date: 2001-04-10 15:36:43
Message-ID: Pine.LNX.4.21.0104101132430.21912-100000@olympus.scw.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

On Tue, 10 Apr 2001, Jaruwan Laongmal wrote:

> dear all,
> I currently using postgresql v7.0.3
> when i import text file to table with command "copy tablename from
> '/tmp/a.txt';
> and it shows
> "copy: line 20, Cannot insert a duplicate key into unique index testpri_pk"
> ,then it exits with doing nothing.
>
> I want to ignore this errors and continue copy the next record. How to do
> that?
> if I don't filter in '/tmp/a.txt' before using copy command.

AFAIK, you can't ignore primary keys, so you can't cheat and get it in,
even for a moment. And if COPY encounters bad data, it ends the
transaction. (Both of these seem like the Right Thing to me, though
perhaps there's an argument for COPY IGNORING ERRORS or something like
that. Ick.)

Either

1) filter /tmp/a.txt to remove duplicates

or

2) drop your unique index, copy the data, get rid of duplicates, the add
the index again

or

2)

Assuming your table you're importing to is

CREATE TABLE names (lname text, fname text, primary key (lname,
fname) );

Create another table w/o the primary key:

CREATE TABLE import (lname text, fname text);

copy to *this* table, then copy from this table to the names table,
ignoring duplicates in the import:

SELECT distinct fname, lname into names from import;

--
Joel Burton <jburton(at)scw(dot)org>
Director of Information Systems, Support Center of Washington

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Mount 2001-04-10 15:55:23 Re: JDBC and Perl compiling problems w/ postgresql-7.1rc4
Previous Message Bruce Momjian 2001-04-10 15:35:19 Re: Re: Re: "--tuning" compile and runtime option (?)

Browse pgsql-sql by date

  From Date Subject
Next Message Albert REINER 2001-04-10 16:38:15 Re: Re: select substr???
Previous Message Christof Glaser 2001-04-10 14:21:07 Re: Copying null values