I need to check for errors when running psql from a bash script. Here's an example of how we're running things in the script:
return_value=$(psql \
-X \
$POSTGRES_CONNECTION_STRING \
-f ./build_table.sql \
-w \
-b \
-A \
-q \
-t \
)
psql_exit_status=$?
The above statement works fine unless there's an error in the sql script, in which case I get some error output on the console but return_value is set to zero, as is psql_exit_status.
The build_table sql script creates a table and imports data from a csv file-- if there are errors in the csv file, or if, say, I intentionally misspell create tableeeee in the sql script I see psql errors on the screen but no error info is returned as best I can tell.
I've tried using the -o flag in psql to output to a file. Nothing shows, it's a blank file. I've also tried adding a 2>&1 bit after the psql statement to see if I could get some error info that way, but nothing doing.
What I need is some way to tell that the sql script has exited abnormally and/or crashed, without having to look at the output on the screen. Is that possible with the way I'm executing psql? Possible something's up with one of my psql flags?
psql, but its man page suggests you may need to enableON_ERROR_STOPfor it to exit with error in such cases.