Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1us3h2-00GnF9-Rs for pgsql-admin@arkaria.postgresql.org; Fri, 29 Aug 2025 18:19:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1us3h2-00BvH5-Au for pgsql-admin@arkaria.postgresql.org; Fri, 29 Aug 2025 18:19:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1us3eH-00BpzI-Kh for pgsql-admin@lists.postgresql.org; Fri, 29 Aug 2025 18:16:30 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1us3eF-002PQ0-2c for pgsql-admin@lists.postgresql.org; Fri, 29 Aug 2025 18:16:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 57TIGLYd2335800; Fri, 29 Aug 2025 14:16:21 -0400 From: Tom Lane To: "David G. Johnston" cc: Murthy Nunna , Pgsql-admin Subject: Re: Having trouble passing a shell variable to a query from psql command line In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Fri, 29 Aug 2025 10:58:03 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2335798.1756491381.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 29 Aug 2025 14:16:21 -0400 Message-ID: <2335799.1756491381@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Fri, Aug 29, 2025 at 10:52=E2=80=AFAM Murthy Nunna = wrote: >> psql -d mydb -t -A -c "SELECT relkind FROM pg_class WHERE relname =3D >> :'SHELL_VAR' ;" -v SHELL_VAR=3D"$SHELL_VAR" >> = >> ERROR: syntax error at or near ":" >> = >> LINE 1: SELECT relkind FROM pg_class WHERE relname =3D :'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 =3D :'SHELL_VAR' ;" | psq= l -d mydb -t -A -v SHELL_VAR=3D"$SHELL_VAR" regards, tom lane