How to exit psql depending on the outcome of a query

Viewed 130
cd C:\Program Files\PostgreSQL\12\bin
psql.exe -v v1="test" -h localhost -d postgres -U postgres -p 5432 -a -q -f  /elan/Validate_files.sql
....

Select case when current_date-date(a.timestamp_1) >1 then 'No' Else 'Yes' End as Check
from elan.temp_file_names a
where a.filename not in (select b.filename from elan.temp_previous_names b)
and position('ELAN_CLAIMS' in a.filename)>1 order by timestamp_1 desc

\gset

If Check is No, I want to end the psql process, how do I do that?

1 Answers

Change the CASE expression to return a boolean:

CASE WHEN ... THEN TRUE ELSE FALSE
END AS check ... \gset

Then use \if:

\if :check
\q
\endif
Related