Re: psql -f COPY from STDIN

From: Tarlika Elisabeth Schmitz <postgresql2(at)numerixtechnology(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: psql -f COPY from STDIN
Date: 2010-11-13 12:01:35
Message-ID: 20101113120135.6b7fdc9e@dick.coachhouse
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, 12 Nov 2010 22:22:11 -0500
Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

>Tarlika Elisabeth Schmitz <postgresql2(at)numerixtechnology(dot)de> writes:
>> The following command works fine when pasing it to psql via the -c
>> option:
>
>> cat event.csv | \
>> psql -c "COPY (event_id, event_name) FROM STDIN DELIMITER AS ',' NULL
>> AS ''"
>
>> When executed from a file via -f, it does nothing
>
>> event.sql:
>> COPY (event_id, event_name) FROM STDIN DELIMITER AS ',' NULL AS ''
>
>> cat event.csv | psql -f event.sql
>
>[...]
>
>I think you can get the effect you're after using \copy ... from
>pstdin. See the psql man page.

Thank you for your reply, Tom. I had not appreciated the difference
between \COPY and COPY. The psql command is executed on the client; the
import data are on the client, so I ought to use \COPY.

Strangely, cat event.csv |psql -c "COPY ... worked, presumably
because both the COPY command and data came from the same source (client
stdin).

I changed event.sql (3 lines):
\COPY
(event_id, event_name)
FROM STDIN DELIMITER AS ',' NULL AS ''

Now I am getting error messages:
psql:event.sql:1: \copy:parse error at end of line
psql:event.sql:4: ERROR:syntax error at or near "event_id" LINE 1:
(event_id,

When I remove the linefeeds I don't get errors but it does not import
anything.

I'd like the store the COPY command in a separate file without
specifying an input file name. I want to feed it the data from the
shell script that calls psql -f.

--

Best Regards,
Tarlika Elisabeth Schmitz

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2010-11-13 14:54:57 Re: psql -f COPY from STDIN
Previous Message Tom Lane 2010-11-13 03:22:11 Re: psql -f COPY from STDIN