简体   繁体   中英

Connecting to PostgreSQL using pyodbc

I have a PostgreSQL running on localhost. I have psqlODBC Driver installed. Now when I try to connect to the server using pyodbc as:

import pyodbc
connection = pyodbc.connect("DRIVER={psqlOBDC};SERVER=localhost;DATABASE=weather;UID=postgres;PWD=password")

but I get this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('08001', '[08001] Already connected. (202) (SQLDriverConnect)')

Whats the problem?

Thanks.

Of course you must install ODBC driver. I don't know Mac, but in Windows there is ODBC Manager where you configure Data Sources. For PostgreSQL driver there is also "Test" button to check if connection can be established.

As fog noticed you can also use other technology to connect to PostgreSQL. If you can use Jython then you can use JDBC driver. There are also native drivers like psycopg, PyGreSQL and pg8000 (pure Python). Can you give them try?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM