using COPY table FROM STDIN within script run as psql -f file.sql

From: Kevin Murphy <murphy(at)genome(dot)chop(dot)edu>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: using COPY table FROM STDIN within script run as psql -f file.sql
Date: 2004-09-23 15:56:09
Message-ID: 162A572C-0D79-11D9-8AE5-0003930D3626@genome.chop.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

This is a tip for the record in case it helps somebody else in the
future.

I have an import script that relies on a stored procedure that runs as
a trigger on inserts into a temporary table. The script looks like
this:

-- create table
-- ...
-- define procedure and trigger
-- ...
-- import data via COPY command:
COPY temp_table FROM STDIN WITH NULL AS '';

However, when run as "psql -f import.sql <data.file", it does not work
if you use the SQL "COPY" command, even if you are running psql on the
database server. You get an error like this: ERROR: missing data for
column "somecol". An interesting red-herring is that the column
mentioned is not necessarily the first column in the table!

The solution is to use the psql "\COPY" command instead (and remove the
trailing semi-colon, which cannot be used with psql commands). I.e.
this command will work:

\COPY temp_table FROM STDIN WITH NULL AS '';

-Kevin Murphy

Responses

Browse pgsql-general by date

  From Date Subject
Next Message frederic.germaneau 2004-09-23 15:59:38 Réf. : Re: v8 on AIX5.2
Previous Message Jeff Amiel 2004-09-23 15:37:06 Re: using database for queuing operations?