Home:ALL Converter>Connect to PostgreSQL using Python

Connect to PostgreSQL using Python

Ask Time:2014-10-17T04:03:01         Author:Ravi Mehta

Json Formatter

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.

I first tried to this using psycopg2, using the following code

try:
    conn = psycopg2.connect("dbname='ravi' user='potsgres' host='localhost' password='***'")
except:
    print ("I am unable to connect to the database")

Next I tried to connect using pyodbc using this code

try:
    connection = pyodbc.connect("DRIVER={psqlOBDC};SERVER=localhost:5432;DATABASE=ravi;UID=postgres;PWD=***")
except:
    print("not working")

And no connection was made.

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.

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.

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

Opening a postgres connection in psycopg2 causes python to crash

Connecting to PostgreSQL using pyodbc

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!

EDIT: Using univerio's example here's the error message when I try to use psycopg2 I get the following error SyntaxError: invalid syntax but I can't see where I messed up the syntax.

If I try to use pyODBC here's the error I get ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

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.

Author:Ravi Mehta,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26412897/connect-to-postgresql-using-python
yy