Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nivtq-0003FB-6j for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Apr 2022 10:24:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nivto-0001iB-Pc for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Apr 2022 10:24:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nivto-0001i1-FW for pgsql-hackers@lists.postgresql.org; Mon, 25 Apr 2022 10:24:56 +0000 Received: from mail.verite.pro ([185.16.44.216]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nivtl-0007be-6D for pgsql-hackers@lists.postgresql.org; Mon, 25 Apr 2022 10:24:55 +0000 Received: by mail.verite.pro (Postfix, from userid 1000) id ABFB28C0983; Mon, 25 Apr 2022 12:24:50 +0200 (CEST) Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 From: "Daniel Verite" Subject: Re: variable filename for psql \copy To: =?UTF-8?Q?Ji=C5=99=C3=AD_Fejfar?= Cc: pgsql-hackers@lists.postgresql.org In-Reply-To: Date: Mon, 25 Apr 2022 12:24:47 +0200 Message-Id: X-Mailer: Manitou v1.7.3 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ji=C5=99=C3=AD 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= =20 documented, so in that sense it's not a bug. The doc also suggests a workaround in a tip section: =46rom 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, --=20 Daniel V=C3=A9rit=C3=A9 https://postgresql.verite.pro/ Twitter: @DanielVerite