psql: print values and return the COUNT(*) value to bash?

From: Ron Johnson <ronljohnsonjr(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: psql: print values and return the COUNT(*) value to bash?
Date: 2025-12-29 07:21:41
Message-ID: CANzqJaBX-SZZcHn71xgqtSKx6ekZGdHPSgCmP2co5=-KDu4Q-Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

Currently I do this in a bash script:
Tbl=table_1
Fld=field_1
Sql="SELECT CURRENT_TIMESTAMP - MIN($Fld) AS days_ago
, MIN($Fld) as oldest_date
, COUNT(*) AS cnt
FROM $Tbl;"
psql -h <host> <db> -Xc "${Sql}"
days_ago | oldest_date | cnt
----------+-------------+-----
| | 0
(1 row)

More stuff happens after this. I want to print out these four lines, but
also test the COUNT(*) value and bypass other bash commands if the count ==
0.

Is there any relatively simple way to do this (psql print and return
count(*))?

--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jehan-Guillaume de Rorthais 2025-12-29 08:12:36 Re: psql: print values and return the COUNT(*) value to bash?
Previous Message Tom Lane 2025-12-29 03:41:19 Re: Combining metavariables and table names