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 1us6Ry-00HRjB-QT for pgsql-admin@arkaria.postgresql.org; Fri, 29 Aug 2025 21:16:00 +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 1us6Rx-00CvGy-86 for pgsql-admin@arkaria.postgresql.org; Fri, 29 Aug 2025 21:15:57 +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 1us6Rw-00CvGo-SQ for pgsql-admin@lists.postgresql.org; Fri, 29 Aug 2025 21:15:57 +0000 Received: from mail-oa1-x2a.google.com ([2001:4860:4864:20::2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1us6Rv-002Qga-0B for pgsql-admin@lists.postgresql.org; Fri, 29 Aug 2025 21:15:56 +0000 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-30cce5da315so934383fac.0 for ; Fri, 29 Aug 2025 14:15:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756502155; x=1757106955; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=wtK8qTmsJwdw9YEnDR+DZKySsdN8yAjnSpOMGUIS7o8=; b=jwY9dMRBeK3kyEJOi9GbZpoCPQS/Alvs5ai9bWFk6eLnWKznEF6cAfq87MJUk3Yu7Z sQADAO5vlFeKx590sC2U39iwY+DtztuSVTaqhYwzeRX7gGXT07jQa8uTPn0guFJEDcHF KZNwesaajoW+uWbAnfDCAIP57MqW4n7qtAhbI/aYTMoq2qHZkv1w9bKJinNOOFq7ayKg pottXG2FuoD32b/VenjwrYYEDhCqsPpZWb2XgY/PMhffAgrRx+ZVC/0DI7SViVipccda qLIhdp0qNgZoUvFkR35OCtbtiTj+QZyPT7iBhSQTWKna5WXwGcBEOUwiubtBYMT7yE3B weiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756502155; x=1757106955; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wtK8qTmsJwdw9YEnDR+DZKySsdN8yAjnSpOMGUIS7o8=; b=ghzBXwIsj1l5zBiEnxbD4mTYaQAiBCX9RiUumDlHcyWdY4tKCNPRmbHCfaCIXrp002 0n5mQbFBzE7Uv7Ht8nCeiye2uC3ImKwc7DraF07qrNk3slxgev2Q3tifmLa7U1Xc4N0F GVHjF2aCdVuGxUkFKnsa3ARfh6KjzOdogvRx8FHweK9XwWWczCefnq2I93JtXZ4xHkXn 3IfNMUBUnHGp0zqOnoNXQCp28QlT0x9OR0v76OzTExQAxvCD9WF5e0rNEt6/VwoytS/p yreFqCXe4GnRucd/bVLjeSNFSK8oBYUTK45csik035LWQNLinYpiJo6XPxwycj7wISkV usvQ== X-Gm-Message-State: AOJu0YxnoUfhJGOIDQ2JZvO+pCOdisytPVAHUi7ySrGqbjdcz/M/Qzca 0/3ahALqAghRxLn4qv0BFwwIZkpjkx3fAMYD9F73a4mJFR+tcGhE+TLm9KByDIWfTyd3lLhvyyR KJerxSDLv9kyvCIi9gu1aFaSuIOhHHramFQ== X-Gm-Gg: ASbGnctInaAqR/2+6i5z8k8v5ElFnVlJjEM+rtF+NMmMFBg7dOQX8XlWixOkxP2ujti 0NNxXXJ++VTNNybptwc39zjZLGqhsTzFopmnL5/o4ZKCUezg0nmwar/xwoVPhYSyc9FV7YOmLmz z4muDmIea9OySntJhHStybzCYQth2ayEMWJcvGoYBvqrvUSqD+lZGW9m+wyDyJIYrHphaKaPgWr PIDYTaK1ZveoZXHEgE= X-Google-Smtp-Source: AGHT+IE+rGtGcF1lWUegIuKkw/aq+NgueqnwSR6A3RzK8Sdxf7C4il47yjEGiYeZ3J54IIyAPbZmIWEJ1ObspIu31TY= X-Received: by 2002:a05:6808:509e:b0:437:d7b0:878c with SMTP id 5614622812f47-437f7cfe2damr33202b6e.20.1756502154861; Fri, 29 Aug 2025 14:15:54 -0700 (PDT) MIME-Version: 1.0 References: <2343915.1756494912@sss.pgh.pa.us> In-Reply-To: <2343915.1756494912@sss.pgh.pa.us> From: Ron Johnson Date: Fri, 29 Aug 2025 17:15:44 -0400 X-Gm-Features: Ac12FXwG9yMcFapRjimAwiye22oqGuTh6jXzVHdt3g_oazqw2Oqdm27IzTKTUOc Message-ID: Subject: Re: Having trouble passing a shell variable to a query from psql command line To: Pgsql-admin Content-Type: multipart/alternative; boundary="000000000000fce2b0063d8786cb" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000fce2b0063d8786cb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Aug 29, 2025 at 3:15=E2=80=AFPM Tom Lane wrote: > Ron Johnson writes: > > Yeah. From the cli KISS and do regular bash variable string expansion. > > > psql -d mydb -tAc "SELECT relkind FROM pg_class WHERE relname =3D > > ${SHELL_VAR} ;" > > This isn't a great recommendation because bash is not aware of > SQL's quoting rules. It'll work in simple cases, but there's > a risk of SQL injection if the value of SHELL_VAR comes from > an untrustworthy source. Well, yeah, if your shell script interacts with the outside world you've got to be a bit more robust than if the script only does db maintenance operations on the db server. --=20 Death to , and butter sauce. Don't boil me, I'm still alive. lobster! --000000000000fce2b0063d8786cb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Aug 29, 2025 at 3:15=E2=80=AFPM T= om Lane <tgl@sss.pgh.pa.us> = wrote:
Ron Johnson <ronljohnsonjr@gmail.com> write= s:
> Yeah.=C2=A0 From the cli KISS and do regular bash variable string expa= nsion.

> psql -d mydb -tAc "SELECT relkind FROM pg_class WHERE relname =3D=
> ${SHELL_VAR} ;"

This isn't a great recommendation because bash is not aware of
SQL's quoting rules.=C2=A0 It'll work in simple cases, but there= 9;s
a risk of SQL injection if the value of SHELL_VAR comes from
an untrustworthy source.

Well, yeah, if you= r shell script interacts with the outside world you've got to be a bit = more robust than if the script only does db maintenance operations on the d= b server.=C2=A0

--
Death to <Redacted>, and butter sauce.
Don't boil me, I&= #39;m still alive.
<Redacted> lobster!
=
--000000000000fce2b0063d8786cb--