I have a Django application that runs from a virtual environment. I am writing a bash script to test the virtual environment.
The postgresql username and password are environment variables in the virtual environment, that Django pulls into settings.py.
In order to test the connection to the database, I have written the following:
echo -n 'Testing database connection ...'
export COMMAND="from psycopg2 import connect; import sys; c=connect(dbname='myappdb',\
user='$POSTGRESQL_USER', password='$POSTGRESQL_PWD', host='127.0.0.1', port=5432); \
sys.exit(c.closed)"
test `python -c "$COMMAND" 2>/dev/null && echo true `
Where test is a function:
function test {
[ $1 ] && echo "[OK]" || echo "[FAIL]"
}
It seems that even with the wrong credentials, no exception is raised and c.closed is always zero. Is there a way I can test if the credentials are right in the virtual environment?