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/


Reply via email to