title: How to test postgresql credentials from bash?
body: <p>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 <code>settings.py</code>.</p>
  
  <p>In order to test the connection to the database, I have written the following:</p>
  
  <pre><code>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&gt;/dev/null &amp;&amp; echo true `
  </code></pre>
  
  <p>Where test is a function:</p>
  
  <pre><code>function test {
    [ $1 ] &amp;&amp; echo "[OK]" || echo "[FAIL]"
  }
  </code></pre>
  
  <p>It seems that even with the wrong credentials, no exception is raised and <code>c.closed</code> is always zero. Is there a way I can test if the credentials are right in the virtual environment?</p>