How to save psql error message output in bash variable?
You can't, directly. At least, not without either commingling it with or discarding standard output. However, there is a way!
#!/bin/bash
errorlog=$(mktemp)
trap 'rm -f "$errorlog"' EXIT
pwcheck="$(psql -q -U postgres -h $ip -d $database 2> "$errorlog")"
if [[ 0 -ne $? ]]; then
echo "Something went wrong; error log follows:"
cat "$errorlog"
fi