Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 76E58479382 for ; Tue, 28 Jan 2003 22:21:21 -0500 (EST) Received: from beamish.nsd.ca (unknown [205.150.156.194]) by postgresql.org (Postfix) with ESMTP id 81376476FE7 for ; Tue, 28 Jan 2003 20:50:50 -0500 (EST) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id UAA15181; Tue, 28 Jan 2003 20:50:49 -0500 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma015177; Tue, 28 Jan 03 20:50:19 -0500 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id UAA27498; Tue, 28 Jan 2003 20:47:35 -0500 Message-ID: <3E373369.52C862DE@nsd.ca> Date: Tue, 28 Jan 2003 20:50:33 -0500 From: Jean-Luc Lachance X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9-31 i686) X-Accept-Language: en MIME-Version: 1.0 To: Guy Fraser Cc: pgsql-sql Subject: Re: CSV import References: <3E3697AF.2030509@aon.at> <3E370095.3020200@incentre.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/451 X-Sequence-Number: 11316 You can acheive the same result with: tr -d '"\015' < file_name.txt | psql {etc...} Unix EOL is LF not CR. Guy Fraser wrote: > > Hi > > You will need two text utilities {dos2unix and sed} to do this in the simplest > way. They are fairly standard text utilities and are probably already on your > machine. > > This is how I would do it : > > sed "s/\"//g" file_name.txt \ > | dos2unix \ > | pgsql -c "COPY table_name FROM STDIN USING DELIMITERS ',';" db > > Where "file_name.txt" is the csv file you want to import and "table_name" is > the previously created table you want to insert the data into and db is the > database name. > > How this works is "sed" {stream editor} removes all the double quote > characters '"' then pipes the output through "dos2unix" which converts all the > CRLF {DOS EOL} sequences into CR {UNIX EOL} characters, then pipes the data to > "pgsql" with a command that does a bulk insert into the table of the database > you have selected. > > Guy > > Oliver Vecernik wrote: > > Hi again! > > > > After investigating a little bit further my CSV import couldn't work > > because of following reasons: > > > > 1. CSV files are delimited with CR/LF > > 2. text fields are surrounded by double quotes > > > > Is there a direct way to import such files into PostgreSQL? > > > > I would like to have something like MySQL provides: > > > > LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt' > > [REPLACE | IGNORE] > > INTO TABLE tbl_name > > [FIELDS > > [TERMINATED BY '\t'] > > [[OPTIONALLY] ENCLOSED BY ''] > > [ESCAPED BY '\\' ] > > ] > > [LINES TERMINATED BY '\n'] > > [IGNORE number LINES] > > [(col_name,...)] > > > > Has anybody written such a function already? > > > > Regards, > > Oliver > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/users-lounge/docs/faq.html