Re: Add partial :-variable expansion to psql \copy

Daniel Verite <daniel@manitou-mail.org>

From: "Daniel Verite" <daniel@manitou-mail.org>
To: "Christoph Berg" <myon@debian.org>
Cc: Fabien Coelho <coelho@cri.ensmp.fr>, Tom Lane <tgl@sss.pgh.pa.us>, PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-04-01T12:06:28Z
Lists: pgsql-hackers
	Christoph Berg wrote:

> Perhaps this form could be improved by changing `\copy (select) to file`
> to something like `select \gcopy (to file)`. That might make :expansion
> in the "select" part easier to handle.

In this direction (COPY TO), it was already taken care of by
commit 6d3ede5f1c654f923b2767b0b0c3b09569adaa18  [1]
a few years ago.

That is, the following sequence already works fine:

\set filename '/tmp/foo'
\set column relname
\set table pg_class

COPY (select :"column" from :"table") TO STDOUT \g :filename

It's also mentioned in the manual through that paragraph in \copy
"
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.
"

[1]
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=6d3ede5f1c654f923b2767b0b0c3b09569adaa18


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



Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix psql's "\g target" meta-command to work with COPY TO STDOUT.