title: Connect to PostgreSQL using Python
body: <p>I am trying to connect to PostgreSQL using Python and am running into lots of problems. My os is windows 7, I am using postgresql 9.3, and python 3.3 and 3.4. </p>
  
  <p>I first tried to this using psycopg2, using the following code </p>
  
  <pre><code>try:
      conn = psycopg2.connect("dbname='ravi' user='potsgres' host='localhost' password='***'")
  except:
      print ("I am unable to connect to the database")
  </code></pre>
  
  <p>Next I tried to connect using pyodbc using this code</p>
  
  <pre><code>try:
      connection = pyodbc.connect("DRIVER={psqlOBDC};SERVER=localhost:5432;DATABASE=ravi;UID=postgres;PWD=***")
  except:
      print("not working")
  </code></pre>
  
  <p>And no connection was made.</p>
  
  <p>I have already tried several things. I have gone into environment variables to make sure there were paths for postgresql and python.I tried connecting to a SQL server database that I have already created using pyodbc and was able to connect without any problems. I have gone into the ODBC Data Source  Administrator to test the PostgreSQL ODBC driver that I had installed, and the test says the connection was succesful. I'm not sure if this is relevant, but I can connect to the postgresql database without problems using geoserver. I'm not sure if it was an ODBC, JDBC or some other type of connection. Also, I originally tried to make the connection using PHP, but I know very little about PHP, so I assumed there was something wrong in my code. </p>
  
  <p>Any ideas on what the problem could be? A couple of avenues I thought of but couldn't figure out if there was a way to see in detail what the error stopping the connection was. Also, I tried connecting to the database using the command line, but the command line doesn't let me enter the password.  </p>
  
  <p>I have found a couple of potential solutions but I either didn't understand exactly what they meant, or applied to a linux environment and I didn't know what that meant in windows terms</p>
  
  <p><a href="https://stackoverflow.com/questions/25854060/opening-a-postgres-connection-in-psycopg2-causes-python-to-crash">Opening a postgres connection in psycopg2 causes python to crash</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10827471/connecting-to-postgresql-using-pyodbc">Connecting to PostgreSQL using pyodbc</a> </p>
  
  <p>I've only just started to learn how to code, so it would be really appreciated if any potential solutions were kept as simple as possible, and assumed I know next to nothing about computers. Thanks a lot for any help!</p>
  
  <p>EDIT: Using univerio's example here's the error message when I try to use psycopg2 I get the following error <code>SyntaxError: invalid syntax</code> but I can't see where I messed up the syntax.</p>
  
  <p>If I try to use pyODBC here's the error I get <code>('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')</code></p>
  
  <p>I went into the ODBC Data Source Administrator and changed the SSL to require and allow. Neither helped. Also, the name of the driver is actually PostgreSQL35W. Adding 35W did not change the error message.</p>