Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
Well instead of catching exceptions and ignoring them, how about printing out the exception? – univerio Oct 16 '14 at 20:15
    
How do you print an exception? When I put in the code for the connection without using the try: exception: format, python acts as if the code was successful but I start getting error when I start trying to actually do something with the connection. – Ravi Mehta Oct 16 '14 at 20:32
    
except Exception as e: print(e) Also python should crash if you had an exception but didn't catch it. – univerio Oct 16 '14 at 20:36
    
If that is really your password in there: change it NOW. – RickyA Oct 16 '14 at 20:38
    
user='potsgres'? -> user='postgres' – RickyA Oct 16 '14 at 20:39
up vote 0 down vote accepted

psycopg2 takes keyword arguments, not a single string, as per the other question you linked to. Just take the double quotes out.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.