A command that fails normally generates a return code. You can pick up this return code using the $? special variable right after executing the command. For instance, you could do :
psql ARGUMENT LIST
result=$?
if
[ $result = 0 ]
then
echo "Success"
else
echo "Failure with error code $result"
fi
The return code should normally be the most reliable indication of success or failure of a command.
A command may also send messages on either one of two standard output channels : standard output (stdout), and standard error (stderr). Both normally appear on your terminal, but they are really separate, and can be handled separately if you want to.
You can collect stderr, stdout, or both, by using command substitution :
stdout_messages="$(command and args 2>/dev/null)"
stderr_messages="$(command and args 2>&1 1>/dev/null)"
all_messages="$(command and args 2>&1)"
These commands have redirections like X>/dev/null to avoid displaying the massages not collected, but you can remove these to see them on the terminal.
The presence of any message on stderr does not indicate the command has failed, as many programs might use stderr for status messages or errors that are not fatal. Command substitution should be used to collect messages, not determine if the command has failed, unless you have a very specific need.
You can combine both mechanisms, by collecting messages, and then using $? to determine status.