Create a pg table from CSV with header rows

Started by Robert Fitzpatrickover 20 years ago4 messagesgeneral
Jump to latest
#1Robert Fitzpatrick
lists@webtent.net

Anyone know a package that can do this? Perferrably a Unix/Linux
package.

--
Robert

#2Michael Fuhr
mike@fuhr.org
In reply to: Robert Fitzpatrick (#1)
Re: Create a pg table from CSV with header rows

On Thu, Sep 15, 2005 at 06:48:59PM -0400, Robert Fitzpatrick wrote:

Anyone know a package that can do this? Perferrably a Unix/Linux
package.

It would be trivial to write a script in a language like Perl to
read the first line of a file and generate a CREATE TABLE statement
from it, then issue a COPY command and send the rest of the file.
Determining the columns' data types would be a different matter:
if they weren't specified in the header then you'd have to guess
or perhaps make them all text.

--
Michael Fuhr

#3Doug Bloebaum
blabes@gmail.com
In reply to: Michael Fuhr (#2)
Re: Create a pg table from CSV with header rows

On 9/15/05, Michael Fuhr <mike@fuhr.org> wrote:

On Thu, Sep 15, 2005 at 06:48:59PM -0400, Robert Fitzpatrick wrote:

Anyone know a package that can do this? Perferrably a Unix/Linux
package.

It would be trivial to write a script in a language like Perl to
read the first line of a file and generate a CREATE TABLE statement
from it, then issue a COPY command and send the rest of the file.
Determining the columns' data types would be a different matter:
if they weren't specified in the header then you'd have to guess
or perhaps make them all text.

I've attached a sample implementation of Michael's suggestion.

Attachments:

loadCsv2.plapplication/octet-stream; name=loadCsv2.plDownload
#4David Fetter
david@fetter.org
In reply to: Doug Bloebaum (#3)
Re: Create a pg table from CSV with header rows

On Fri, Sep 16, 2005 at 10:41:59AM -0400, Doug Bloebaum wrote:

On 9/15/05, Michael Fuhr <mike@fuhr.org> wrote:

On Thu, Sep 15, 2005 at 06:48:59PM -0400, Robert Fitzpatrick wrote:

Anyone know a package that can do this? Perferrably a Unix/Linux
package.

It would be trivial to write a script in a language like Perl to
read the first line of a file and generate a CREATE TABLE statement
from it, then issue a COPY command and send the rest of the file.
Determining the columns' data types would be a different matter:
if they weren't specified in the header then you'd have to guess
or perhaps make them all text.

I've attached a sample implementation of Michael's suggestion.

I've attached what I hope is a slightly improved one :)

Cheers,
D
--
David Fetter david@fetter.org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

Attachments:

load_csv.pltext/plain; charset=us-asciiDownload