Check if a single request fails using the command line

I have two requests in one file:

select id from "user" where id = 1; // request passes
select from "user"; // request fails

      

When I run the following commands in my shell:

psql -U username -d db_name -h host -p port -f test.sql
echo $?

      

This will always prompt me to 0 (assuming the credentials are correct and the command psql

ran without an execution error). This looks pretty fair.

I was wondering if there is a way to get a different return code if one of the requests inside the linked file failed?

Thanks for the help.

+3


source to share


1 answer


On the first line of your script, add \set ON_ERROR_STOP on

. psql will return exit code 3 if the script failed.



+3


source







All Articles