I'm writing a bash/shell script that has a psql database connection and the program fails with psql: FATAL:  database "foo" does not exist. This is fine because the program exits since it doesn't exist, but what I would like to do instead is trap or capture that FATAL message and perhaps echo or do something else.
Like if message == "database "foo" does not exist; then echo or run another function.
Here's my line of code that does db connection:
PGPASSWORD=$TARGET_PW "$PSQL" -h "$HOST2" -U masteruser -d "$database" -p 5439 << EOF 
        \a
        \t
        \o $LISTNEW
    SELECT column_name
    FROM information_schema.columns
    WHERE table_schema = '$SCHEMAMAIN'
    AND table_name   = '$TBL'
    order by ordinal_position;
EOF
Can I get an example of how to do this? thanks.
 
     
    