Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E2F579FB36C for ; Sat, 19 May 2007 19:30:00 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 93758-01 for ; Sat, 19 May 2007 19:29:56 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id C88529F9CCD for ; Sat, 19 May 2007 19:29:26 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mail23.syd.optusnet.com.au (mail23.syd.optusnet.com.au [211.29.133.164]) by svr4.postgresql.org (Postfix) with ESMTP id 742D75AF861 for ; Sat, 19 May 2007 22:29:24 +0000 (GMT) Received: from [127.0.0.1] (d220-238-20-190.dsl.vic.optusnet.com.au [220.238.20.190]) by mail23.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id l4JMTHJ1011022 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 20 May 2007 08:29:19 +1000 Message-ID: <464F7A31.6020501@autoledgers.com.au> Date: Sun, 20 May 2007 06:29:05 +0800 From: Paul Lambert User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: ignoring primary key violations in COPY command References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/147 X-Sequence-Number: 28427 singh.srajendra@wipro.com wrote: > Hi all, > > We are importing the data from the CSV file into the database using COPY > command. But when the ‘primary key ‘ violation occurs , it stops > > Then and there and data is not updated into the tables. Also in the CSV > file the number of columns is not fixed , its varies > > In number , it can range anywhere between 1 -22, I need to figure out a > way such that I need to update only those columns which are present > > In the CSV file . This of course I would like to accomplish using COPY > command , Please let me know if at all this is possible in postgresql 8.1 > > /Thanks and regards,/ > > Rajendra Singh > In my opinion your best bet in terms of getting around the primary key violation is to create a temporary table without a primary key, copy your data into that table, then do a select into your main table from that table. Eg.. I do the following: CREATE TABLE creditors_temp_load AS SELECT * FROM creditors WHERE 1=0; TRUNCATE TABLE creditors; COPY creditors_temp_load FROM 'c:/temp/autodrs_creditors.txt' WITH DELIMITER AS '^' QUOTE '\f' CSV HEADER; INSERT INTO creditors (SELECT DISTINCT ON (dealer_id,supplier_no) * FROM creditors_temp_load WHERE (dealer_id,supplier_no) is not null); The first statement creates a copy of the 'creditors' table without any records (none in the creditors table have 1 equal to zero) The second copies the data from the file into the temp table. Finally an insert into the 'creditors' table is done by a select distinct on the temp table where the two fields listed are the primary key for that table. I don't believe there is any way of getting around not having all the fields present - copy expects to find a match between fields in the file and fields in the destination table. If your record length in the load file is going to vary you may need to consider writing a program to read the data from the file and load it in. Regards, Paul. -- Paul Lambert Database Administrator AutoLedgers