title: Psycopg2 access PostgreSQL database on remote host without manually opening ssh tunnel
body: <p>My standard procedure for accessing a PostgreSQL database on a remote server is to first create an ssh tunnel as:</p>
  
  <pre><code>ssh username1@remote.somewhere.com -L 5432:localhost:5432 -p 222
  </code></pre>
  
  <p>and then run my query in python from another shell as:</p>
  
  <pre><code>conn = psycopg2.connect("host=localhost" + " dbname=" +
                           conf.dbname + " user=" + conf.user + 
                           " password=" + conf.password)
  
  cur = conn.cursor()
  
  cur.execute(query)
  </code></pre>
  
  <p>This piece of python code works nicely once the tunnel is created. However, I would like psycopg2 to already open the SSH tunnel or reach "somehow" the remote database without need to redirect it on my localhost.</p>
  
  <p>Is it possible to do this with psycopg2?</p>
  
  <p>Is otherwise possible open the ssh tunnel in my python code?</p>
  
  <p>if I use:</p>
  
  <pre><code>os.system("ssh username1@remote.somewhere.com -L 5432:localhost:5432 -p 222")
  </code></pre>
  
  <p>The shell will be redirected to the remote host blocking the execution of main thread.</p>