Re: variable filename for psql \copy

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: Jiří Fejfar <jurafejfar(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: variable filename for psql \copy
Date: 2022-04-25 10:24:47
Message-ID: bdbe1e0b-3b20-4781-a0dc-6922563a1a5b@manitou-mail.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Jiří Fejfar wrote:

> I have found maybe buggy behaviour (of psql parser?) when using psql \copy
> with psql variable used for filename.

While it's annoying that it doesn't work as you tried it, this behavior is
documented, so in that sense it's not a bug.
The doc also suggests a workaround in a tip section:

From psql manpage:

The syntax of this command is similar to that of the SQL COPY
command. All options other than the data source/destination are as
specified for COPY. Because of this, special parsing rules apply to
the \copy meta-command. Unlike most other meta-commands, the entire
remainder of the line is always taken to be the arguments of \copy,
and neither variable interpolation nor backquote expansion are
performed in the arguments.

Tip
Another way to obtain the same result as \copy ... to is to use
the SQL COPY ... TO STDOUT command and terminate it with \g
filename or \g |program. Unlike \copy, this method allows the
command to span multiple lines; also, variable interpolation
and backquote expansion can be used.

Best regards,
--
Daniel Vérité
https://postgresql.verite.pro/
Twitter: @DanielVerite

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2022-04-25 11:03:33 Re: [PATCH] Teach pg_waldump to extract FPIs from the WAL
Previous Message houzj.fnst@fujitsu.com 2022-04-25 08:35:05 RE: Perform streaming logical transactions by background workers and parallel apply