简体   繁体   中英

Connect Python script to PostgreSQL database

Hey I'm not sure what is wrong with this connection string, i'm getting the error - pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

here is my script:

import pyodbc
conn_str = (
    "DRIVER={PostgreSQL ANSI};"
    "DATABASE=postgres;"
    "UID=postgres;"
    "PWD=password;"
    "SERVER=localhost;"
    "PORT=5432;"
    )
conn = pyodbc.connect(conn_str)
crsr = conn.execute("SELECT * FROM initialdata limit 50")
row = crsr.fetchone()
print(row)
crsr.close()
conn.close()

and here is my ODBCINST.INI file contents:

[PostgreSQL ANSI(x64) (32 bit)]
Driver=C:\Program Files\psqlODBC\0905\bin\psqlodbc30a.dll
Setup=C:\Program Files\psqlODBC\0905\bin\psqlodbc30a.dll
32Bit=1

[ODBC 32 bit Drivers]
PostgreSQL ANSI(x64) (32 bit)=Installed
PostgreSQL Unicode(x64) (32 bit)=Installed

[PostgreSQL Unicode(x64) (32 bit)]
Driver=C:\Program Files\psqlODBC\0905\bin\psqlodbc35w.dll
Setup=C:\Program Files\psqlODBC\0905\bin\psqlodbc35w.dll
32Bit=1

Any help would be appreciated!

check list of drivers by using pyodbc.drivers() and use driver name from it

the driver name should be like PostgreSQL ANSI(x64) instead of PostgreSQL ANSI .

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