Re: Having trouble passing a shell variable to a query from psql command line

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ron Johnson <ronljohnsonjr(at)gmail(dot)com>
Cc: Pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: Having trouble passing a shell variable to a query from psql command line
Date: 2025-08-29 19:15:12
Message-ID: 2343915.1756494912@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Ron Johnson <ronljohnsonjr(at)gmail(dot)com> writes:
> Yeah. From the cli KISS and do regular bash variable string expansion.

> psql -d mydb -tAc "SELECT relkind FROM pg_class WHERE relname =
> ${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.

regards, tom lane

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Ron Johnson 2025-08-29 21:11:29 Why might autovacuum not be analyzing chk_image_rp4_y2025m08?
Previous Message Murthy Nunna 2025-08-29 18:31:52 RE: Having trouble passing a shell variable to a query from psql command line