pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Lamar Owen <lamar.owen@wgcr.org>
To: mike <mike@cyborg-group.com>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: imorting csv files into postgres
Date: Thu, 18 Jan 2001 15:48:11 -0500
Message-ID: <3A67568B.59572A40@wgcr.org> (raw)
References: <E14JIp9-0003RL-00@mail1.mailshot>

mike wrote:

> Does anyone know if you can import csv text files into postgres

A perl script to do that was just posted to the GENERAL List....
Hmmmm....

Tim Uckun just posted the following:
-------
Here is your steps.
  1) Create your destination table in postgres
2)  Export your data to a comma delimeted format
3)  save this perl script to a file

------
#! /usr/bin/perl
$inquotes = 0;
while (<>){
     # Chop the crlf
     chop ($_);
     chop ($_);

     # this first bit goes through and replaces
     # all the commas that re not in  quotes with tildes
     for ($i=0 ; $i < length($_) ; $i++){
         $char=substr($_,$i,1);
         if ($char eq '"' ){
             $inquotes = not($inquotes);
         }else{
             if ( (!$inquotes) && ($char eq ",") ){
                 substr($_,$i,1)="~";
             }
         }
     }
     # this replaces any quotes
     s/"//g;
     print "$_\n";
}
-----

4) type this cat myfile.txt | perl myprog.pl > outfile.dat
5) go to psql
6) COPY "tablename" FROM '/path/to/outfile.dat' USING DELIMITERS '~';
-----

See if that helps any.
--
Lamar Owen
WGCR Internet Radio
1 Peter 4:11



view thread (4+ messages)  latest in thread

Message-ID: <3A67568B.59572A40@wgcr.org>
Permalink:  ../3A67568B.59572A40@wgcr.org/
Also on:    postgresql.org/message-id/3A67568B.59572A40@wgcr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: lamar.owen@wgcr.org, mike@cyborg-group.com
  Subject: Re: imorting csv files into postgres
  In-Reply-To: <3A67568B.59572A40@wgcr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox