Connecting to PostgreSQL with Python

I am trying to connect to PostgreSQL using Python and I have a lot of problems. My os is Windows 7, I am using postgresql 9.3 and python 3.3 and 3.4.

At first I tried using this with 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 there were no connections.

I have already tried a few things. I went into environment variables to make sure that there are paths for postgresql and python. I tried connecting to the SQL Server database that I had already created using pyodbc and was able to connect without any problems. I went to the ODBC Data Source Administrator to test the PostgreSQL ODBC driver that was installed, and the test says that the connection was successful. I'm not sure if this is relevant, but I can easily connect to the postgresql database using geoserver. I am not sure if it is ODBC, JDBC or some other type of connection. In addition, I initially tried to establish a connection using PHP, but I know very little about PHP, so I assumed there was something wrong with my code.

, ? , , , , ​​ . , , .

, , , linux, , Windows

postgres psycopg2 python

PostgreSQL pyodbc

, , , - , , . !

EDIT: univerio , psycopg2, SyntaxError: invalid syntax, , .

pyODBC , ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

ODBC Data Source Administrator SSL, . . , PostgreSQL35W. 35W .

+4
1

psycopg2 , , , . .

0

All Articles