Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIdz7-0000No-5t for pgsql-general@arkaria.postgresql.org; Thu, 10 Oct 2019 19:20:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iIdz5-000576-NU for pgsql-general@arkaria.postgresql.org; Thu, 10 Oct 2019 19:20:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iIdz4-00056h-3D for pgsql-general@lists.postgresql.org; Thu, 10 Oct 2019 19:20:23 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iIdz0-0003q4-V8 for pgsql-general@postgresql.org; Thu, 10 Oct 2019 19:20:20 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 4FEE62208C; Thu, 10 Oct 2019 15:20:17 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 10 Oct 2019 15:20:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=k xJOq/VVdzdxaWU0LyVfaJwEzmxmRc9HR66WlxMW7nY=; b=kzT7M5FMz63SWd36h KzT5vv5x9vNOMfnoTFgYpOSSjkjcNVi8InmibG+o/ArKxHt/c3s47nwYPF776gmI rKToqtc2dR54pXmJlJ1l35GH72l+DgTvKg7seX5m7wjviO5Uu/rxzxEWQOlYWOtI 0qGGVYRGiDIrMo8FtFQ2ulMhnB9djEtsuzMAuPuIEaFsH/e0PpioqNpVEAtKRogH bhvpHU8iJAiDPvaxf5HkBEmSNNFgyfPcrAvzlTUEWHZSIhAHJONvEeIZINANKFVU 8TZq0WLDN4Ol8qmYc9Cq8tCHAxu/y2KojP6J/wFx5y+cun+RnC9zS+SnIuQfozM+ 2nKXQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=kxJOq/VVdzdxaWU0LyVfaJwEzmxmRc9HR66WlxMW7 nY=; b=PW8XjXkF0ja3MMeTaJTmYYrsIs3y5eU0X/sIl24i+czk2Eb3axUsrC8lB kCG0pq8cQno8hpeuYLIOyJUmVXIFr+n4LJ27DnMq0hLjQ5bUPP9JYTfisHDootqK ZdIfTQrt5quMzyfBkRcnVfFXw/9eMlS/1j/l2H4eeI27epfsUYVbFdm5FIh6XuDe NGKCCzWrEykKxlgwHTD0V38HTRaFuu5mnZ5fnKDdShEW8sDgCl8G4p2tpqmnPBLw 5EpRsjoZtaAKNmaMQZMXHeYL6Aj6B7ywez93C7z5D2HmJmwtrNRa1gZB1U1oitOa eW+3lcIWwhqLqCAMlwSid0VCn8ybw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrieefgddufeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefheenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgpdhquhgrlhhishdqtgho nhhsuhhlthhinhhgrdgtohhmpdhfqhgunhdrrggunecukfhppedujeegrddvuddrkeelrd dvudeknecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegr khhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (174-21-89-218.tukw.qwest.net [174.21.89.218]) by mail.messagingengine.com (Postfix) with ESMTPA id 766C9D60057; Thu, 10 Oct 2019 15:20:16 -0400 (EDT) Subject: Re: plpgsql copy import csv double quotes To: PASCAL CROZET , PG-General Mailing List Cc: Tom Lane References: <453a8fd209f4444aacf7214b96c6eaa9@qualis-consulting.com> <17097.1570660277@sss.pgh.pa.us> From: Adrian Klaver Message-ID: Date: Thu, 10 Oct 2019 12:20:15 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/10/19 8:30 AM, PASCAL CROZET wrote: > Thanks, > > > As it's write in the page, > the file format is > more a convention than a standard. > > > With collegues, we've find the solution with a regex in sed > > > sed -e 's/\([^,]\)"\([^,]\)/\1\2/g' -e  's/\([^,]\)"\([^,]\)/\1\2/g' > > Because file contains values like > > ,"hostname1.fqdn.ad|\"\"\"\"\"\"\"\"\"\"0Clean|OK" The issue is less the file format then the data value format. I am struggling to figure out what the above is doing. > > > But, other csv file, that contains this value ","NAME=\"UBUNTU\"","| > works well. I find this value in destination column |NAME=\UBUNTU\| in > the destination table. > > The main lines in the plpgsql function are : You might want to look at dollar quoting: https://www.postgresql.org/docs/9.3/plpgsql-development-tips.html 40.11.1. Handling of Quotation Marks If it where me I would separate out the commands below into distinct EXECUTES, it would be easier to follow. That will still result in the TRUNCATE being rolled back as it is part of the function transaction and: https://www.postgresql.org/docs/11/sql-truncate.html "TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit." If you want to deal with errors then: https://www.postgresql.org/docs/9.3/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING > > CREATE OR REPLACE FUNCTION insert_into_db_sources_csv( >     filename character varying, >     tablename character varying, >     delimiter character varying, >     header character varying) >   RETURNS void AS > .... > request := 'TRUNCATE ' || tablename || '; COPY ' || tablename || ' FROM > ''' || filename || ''' CSV ' || header || ' DELIMITER ''' || delimiter > || ''' ENCODING ''UTF-8'';'; > EXECUTE request; > > The function call : > > select > insert_into_db_sources_csv('/DATA/input/files/Extract_software.csv', > 't_m03_software', ',', 'HEADER'); > > > If the import fails, TRUNCATE isn't executed. The previous data's that > was in table remains the same. > > > *_________________________________* > > Cordialement, *Pascal CROZET** > * > > *DBA * > > •www.qualis-consulting.com •04 78 22 > 74 90 > > •Le Bois des Côtes 1 – Bâtiment A > •300 Route Nationale 6 – 69760 LIMONEST > *_________________________________* > > > > ------------------------------------------------------------------------ > *De :* Tom Lane > *Envoyé :* jeudi 10 octobre 2019 00:31 > *À :* PASCAL CROZET > *Cc :* PG-General Mailing List > *Objet :* Re: plpgsql copy import csv double quotes > PASCAL CROZET writes: >> I’ve experience issues with double quotes \34 inside fields, in a csv file. > >> Ex : >> "value1","some text","other text with "double quotes" inside","last field" > > I don't know of any definition of CSV format by which that's legal data. > The typical rule is that double quotes that are data must be doubled; > at least, that's what COPY expects by default.  You can also get COPY > to handle variants like backslash-quote. > >                         regards, tom lane -- Adrian Klaver adrian.klaver@aklaver.com