注:Python解释器版本(32位/64位)和postgresql驱动版本(32位/64位)需一致。
https://www.postgresql.org/ftp/odbc/versions/msi/
(1)32位:
(2)64位:
双击安装。全程默认。
(1)32位
打开 C:\Windows\SysWOW64\odbcad32.exe
(2)64位
打开 C:\Windows\System32\odbcad32.exe
如图即安装成功
def test_conn_sql_db(): source_db_ip = 'ip' source_db_user = '用户名' source_db_pwd = '密码' databasename = '数据库名' dbconn = pyodbc.connect("DRIVER={PostgreSQL Unicode};" "DATABASE="+databasename+";" "SERVER="+source_db_ip+";" "UID="+source_db_user+";" "PWD="+source_db_pwd+";" "PORT=5432;" "QUIETMODE=YES") return dbconn if __name__ == '__main__': test_conn_sql_db()
(2)64位:(端口根据实际情况修改)
def test_conn_sql_db_64(): source_db_ip = 'ip' source_db_user = '用户名' source_db_pwd = '密码' databasename = '数据库名' dbconn = pyodbc.connect("DRIVER={PostgreSQL Unicode(x64)};" "DATABASE="+databasename+";" "SERVER="+source_db_ip+";" "UID="+source_db_user+";" "PWD="+source_db_pwd+";" "PORT=5432;" "QUIETMODE=YES") return dbconn if __name__ == '__main__': test_conn_sql_db_64()
Python64位+postgresql驱动32位报错:
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 (0) (SQLDriverConnect)')