I want to catch a PostgreSQL error in bash.
For example:
function create_database:
sudo -u postgres psql -c "CREATE DATABASE $1 WITH OWNER $2;"
I want something that can catch any type of postgres errors (not only for create) and echo the error
also in case of error return 1
If I use:
$RESULT=$(sudo -u postgres psql -c "CREATE DATABASE $1 WITH OWNER $2;")
I get the answer from psql but is specific to the operation, so I have to do string matching for each SQL command.