public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tom Lane <[email protected]>
To: David G. Johnston <[email protected]>
Cc: Murthy Nunna <[email protected]>
Cc: Pgsql-admin <[email protected]>
Subject: Re: Having trouble passing a shell variable to a query from psql command line
Date: Fri, 29 Aug 2025 14:16:21 -0400
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAKFQuwaAeF52LOpxjUUH0F3PN3w2CX8J-HJ=rV5wrzp1iyL=cQ@mail.gmail.com>
References: <DM8PR09MB6677729E8FC7452DAAD053E5B83AA@DM8PR09MB6677.namprd09.prod.outlook.com>
	<CAKFQuwaAeF52LOpxjUUH0F3PN3w2CX8J-HJ=rV5wrzp1iyL=cQ@mail.gmail.com>

"David G. Johnston" <[email protected]> writes:
> On Fri, Aug 29, 2025 at 10:52 AM Murthy Nunna <[email protected]> wrote:
>> psql -d mydb -t -A -c "SELECT relkind FROM pg_class WHERE relname =
>> :'SHELL_VAR' ;" -v SHELL_VAR="$SHELL_VAR"
>> 
>> ERROR:  syntax error at or near ":"
>> 
>> LINE 1: SELECT relkind FROM pg_class WHERE relname = :'SHELL_VAR' ;

> I provide the documentation.  Under psql -c:
> "command must be either a command string that is completely parsable by the
> server (i.e., it contains no psql-specific features)"

Yeah.  The argument of a -c switch is just sent to the server as-is.

However, you don't need a script file to fix this.  You can do
something like

echo "SELECT relkind FROM pg_class WHERE relname = :'SHELL_VAR' ;" | psql -d mydb -t -A -v SHELL_VAR="$SHELL_VAR"

			regards, tom lane





reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Having trouble passing a shell variable to a query from psql command line
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox