Re: syntax of \copy

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: rterry(at)pacific(dot)net(dot)au
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: syntax of \copy
Date: 2009-10-09 20:37:12
Message-ID: 200910092037.n99KbCK19795@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

richard terry wrote:
> Hi,
>
> I'm trying at the command line (linux) to import a csv file with this sort of
> format
>
> "A", "name","etc","1"
>
> If I simply eliminate the quotes in a text editor, replace the commas with |
> then this snytax works
>
> If I have a file say import.sql containing this line:
>
> \copy schema.tablename from '/tmp/myfile.CSV' delimiter ',' NULL AS ''
>
> and at the command line type:
>
> psql mydb -f import.sql
>
> then it works fine.
>
> But I want to be able to just use the file 'as is', ie with its quotes so I
> don't have to manually do a global replace.
>
> I've pored over the docs but always find it difficult to interpret them, it
> mentions a CSV mode but I can't figure out how to use it.
>
> The internet is equally unrewarding as whilst there are trillions of
> references as to \copy etc, no-one seems to give a concrete example when
> importing a csv file that has been exported with quotes,

I was able to load your file using the following command:

test=> create table test(c1 text, c2 text, c3 text, c4 text);
CREATE TABLE

test=> \copy test from '/rtmp/x' with csv

test=> select * from test;
c1 | c2 | c3 | c4
----+-------+-----+----
A | name | etc | 1
(1 row)

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Susan M Farley 2009-10-11 01:30:59 How to run queries without double quotes on tables and columns
Previous Message Tom Lane 2009-10-09 14:05:16 Re: Functions which use an argument to decide which table to read