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 1iIaPD-0000eY-S4 for pgsql-general@arkaria.postgresql.org; Thu, 10 Oct 2019 15:31:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iIaPC-0006fG-8a for pgsql-general@arkaria.postgresql.org; Thu, 10 Oct 2019 15:31:06 +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 1iIaPB-0006f3-Mi for pgsql-general@lists.postgresql.org; Thu, 10 Oct 2019 15:31:05 +0000 Received: from smtpout6.r2.mail-out.ovh.net ([54.36.141.6]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iIaP8-0001vE-Aq for pgsql-general@postgresql.org; Thu, 10 Oct 2019 15:31:04 +0000 Received: from ex2.mail.ovh.net (unknown [10.109.159.83]) by mo511.mail-out.ovh.net (Postfix) with ESMTPS id 4A505A56BF36; Thu, 10 Oct 2019 17:30:58 +0200 (CEST) Received: from EX12.indiv2.local (172.16.2.12) by EX11.indiv2.local (172.16.2.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.1466.3; Thu, 10 Oct 2019 17:30:57 +0200 Received: from EX12.indiv2.local ([fe80::4166:c67a:56f6:c34a]) by EX12.indiv2.local ([fe80::4166:c67a:56f6:c34a%12]) with mapi id 15.01.1466.008; Thu, 10 Oct 2019 17:30:57 +0200 From: PASCAL CROZET To: PG-General Mailing List CC: Tom Lane Subject: RE: plpgsql copy import csv double quotes Thread-Topic: plpgsql copy import csv double quotes Thread-Index: AQHVfuZNp4E1BME9HUeRL79RyFsDaadSwzSAgAEYzKU= Date: Thu, 10 Oct 2019 15:30:57 +0000 Message-ID: References: <453a8fd209f4444aacf7214b96c6eaa9@qualis-consulting.com>,<17097.1570660277@sss.pgh.pa.us> In-Reply-To: <17097.1570660277@sss.pgh.pa.us> Accept-Language: fr-FR, en-US Content-Language: fr-FR X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [171.16.208.3] Content-Type: multipart/alternative; boundary="_000_c8185b258c464a059ae09efd5b8e71d7qualisconsultingcom_" MIME-Version: 1.0 X-Ovh-Tracer-Id: 5670594881545152514 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedufedrieefgdelvdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemucehtddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --_000_c8185b258c464a059ae09efd5b8e71d7qualisconsultingcom_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable 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" But, other csv file, that contains this value ","NAME=3D\"UBUNTU\"","| work= s well. I find this value in destination column |NAME=3D\UBUNTU\| in the de= stination table. The main lines in the plpgsql function are : 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 :=3D '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 =95 www.qualis-consulting.com =95 04 78 = 22 74 90 =95 Le Bois des C=F4tes 1 =96 B=E2timent A =95 300 Route Nationale 6 =96 69760 LIMONEST _________________________________ ________________________________ De : Tom Lane Envoy=E9 : jeudi 10 octobre 2019 00:31 =C0 : PASCAL CROZET Cc : PG-General Mailing List Objet : Re: plpgsql copy import csv double quotes PASCAL CROZET writes: > I=92ve experience issues with double quotes \34 inside fields, in a csv f= ile. > 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 --_000_c8185b258c464a059ae09efd5b8e71d7qualisconsultingcom_ Content-Type: text/html; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable

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|\&quo= t;\"\"\"\"\"\"\"\"\"0Clean|OK"


But, other = csv file, that contains this value ","NAME=3D\"UBUNTU\= "","| works well. I find this value in destination co= lumn |NAME=3D\UBUNTU\| in the destination table.

The main lines in the plpgsql function are :

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 :=3D 'TRUNCATE ' || tablename || '; COPY ' || tablename = || ' FROM ''' || filename || ''' CSV ' || header || ' DELIMITER ''' || deli= miter || ''' ENCODING ''UTF-8'';';
EXECUTE request;

The function call :

select insert_into_db_sources_csv('/DATA/input/files/Extract_softw= are.csv', 't_m03_software', ',', 'HEADER');


If the import fails, TRUNCATE isn't executed. The previous data's that w= as in table remains the same.



De : Tom Lane <tgl@sss= .pgh.pa.us>
Envoy=E9 : jeudi 10 octobre 2019 00:31
=C0 : PASCAL CROZET
Cc : PG-General Mailing List
Objet : Re: plpgsql copy import csv double quotes
 
PASCAL CROZET <pascal.crozet@qualis-consulting.= com> writes:
> I=92ve experience issues with double quotes \34 inside fields, in a cs= v file.

> Ex :
> "value1","some text","other text with "d= ouble 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<= br> to handle variants like backslash-quote.

            &nb= sp;           regards, to= m lane
--_000_c8185b258c464a059ae09efd5b8e71d7qualisconsultingcom_--