简体   繁体   English

将Python脚本连接到PostgreSQL数据库

[英]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)') 嘿,我不确定此连接字符串出了什么问题,我得到了错误-pyodbc.Error :('IM002','[IM002] [Microsoft] [ODBC驱动程序管理器]数据源名称未找到且没有默认值指定的驱动程序(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: 这是我的ODBCINST.INI文件的内容:

[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 通过使用pyodbc.drivers()检查驱动程序列表,并pyodbc.drivers()使用驱动程序名称

the driver name should be like PostgreSQL ANSI(x64) instead of PostgreSQL ANSI . 驱动程序名称应类似于PostgreSQL ANSI(x64)而不是PostgreSQL ANSI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 脚本(数据库 URI)连接到 postgresql,但将“conn”检测为字符串 - Connect to postgresql using python script (database URI) but detects 'conn' as string POSTGRESQL:使用Python将远程服务器连接到主机数据库 - POSTGRESQL: Connect remote server to host database with Python 如何将 Postgresql 数据库与 sqlalchemy python 连接 - How connect Postgresql database with sqlalchemy python 无法将 PostgreSQL 数据库从 docker 连接到 python - Can not connect PostgreSQL database from docker to python Python 3.2 脚本连接到本地 MySQL 数据库 - Python 3.2 script to connect to local MySQL database 在 python 脚本中连接到 MySQL 数据库(通过 SSH) - Connect to MySQL database (over SSH) in python script Python脚本(PyMySQL)无法连接到本地主机上的数据库 - Python script (PyMySQL) does not connect to database on localhost Python 3.2 脚本连接到本地 MySQL 数据库 - Python 3.2 script to connect to local MySQL database 无法使用python和heroku postgres在本地连接到postgresql数据库 - Cannot connect to the postgresql database locally using python and heroku postgres 如何使用 Python 通过 SSL 连接到远程 PostgreSQL 数据库 - How to connect to a remote PostgreSQL database through SSL with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM