postgresql Pyodbc - “未找到数据源名称,且未指定默认驱动程序”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16280304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:10:13  来源:igfitidea点击:

Pyodbc - "Data source name not found, and no default driver specified"

postgresqlpython-2.7pyodbcunixodbc

提问by atman

I have trouble getting pyodbcwork. I have unixodbc, unixodbc-dev, odbc-postgresql, pyodbcpackages installed on my Linux Mint 14. I am losing hope to find solution on my own, any help appreciated. See details below:

我很难找到pyodbc工作。我在我的 Linux Mint 14 上安装了unixodbc, unixodbc-dev, odbc-postgresql,pyodbc软件包。我对自己找到解决方案失去了希望,感谢任何帮助。请参阅下面的详细信息:

Running:

跑步:

>>> import pyodbc
>>> conn = pyodbc.connect("DRIVER={PostgreSQL};SERVER=localhost;DATABASE=test;USER=openerp;OPTION=3;")

Gives me:

给我:

>>> pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')

# odbcinst -j gives:

# odbcinst -j 给出

unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/atman/.odbc.ini
SQLULEN Size.......: 4
SQLLEN Size........: 4
SQLSETPOSIROW Size.: 2

Which makes me think there is a unixodbcconfiguration problem. Here are my unixodbcconfig file contents:

这让我觉得存在unixodbc配置问题。这是我的unixodbc配置文件内容:

File/etc/odbcinst.ini:

文件/etc/odbcinst.ini

[PostgreSQL ANSI]
Description     = PostgreSQL ODBC driver (ANSI version)
Driver      = psqlodbca.so
Setup       = libodbcpsqlS.so
Debug       = 0
CommLog     = 1
UsageCount      = 2

[PostgreSQL Unicode]
Description     = PostgreSQL ODBC driver (Unicode version)
Driver      = psqlodbcw.so
Setup       = libodbcpsqlS.so
Debug       = 0
CommLog     = 1
UsageCount      = 2

File/etc/odbc.ini:

文件/etc/odbc.ini

[PostgreSQL test]
Description         = PostgreSQL 
Driver              = PostgreSQL ANSI
Trace               = No
TraceFile           = /tmp/psqlodbc.log
Database            = template1
Servername          = localhost
UserName            =
Password            =
Port                =
ReadOnly            = Yes
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No
ConnSettings        =

File~/.odbc.ini:

文件~/.odbc.ini

[DEFAULT]
Driver = PostgreSQL

[PostgreSQL]
Description         = Test to Postgres
Driver              = PostgreSQL
Trace               = Yes
TraceFile           = sql.log
Database            = nick
Servername          = localhost
UserName            =
Password            =
Port                = 5432
Protocol            = 6.4
ReadOnly            = No
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No
ConnSettings        =

采纳答案by Benny Hill

I believe the answer to your problem is that in your ~/.odbc.ini file you are saying to use driver PostgreSQL- but you have not defined that driver in your /etc/odbcinst.ini file. Try changing PostgreSQLto PostgreSQL ANSIor PostgreSQL Unicode(both of which are defined in /etc/odbcinst.ini).

我相信您的问题的答案是在您的 ~/.odbc.ini 文件中您说要使用驱动程序PostgreSQL- 但您尚未在 /etc/odbcinst.ini 文件中定义该驱动程序。尝试更改PostgreSQLPostgreSQL ANSIPostgreSQL Unicode(两者都在/etc/odbcinst.ini 中定义)。

回答by tandy

For me, the issue was the actual location of my odbc.ini and odbcinst.ini files.

对我来说,问题是我的 odbc.ini 和 odbcinst.ini 文件的实际位置。

On many systems, the install location of these files is in /etc/

在许多系统上,这些文件的安装位置在 /etc/

However, in my case, these files were located under /usr/local/etc/

但是,就我而言,这些文件位于 /usr/local/etc/

The could be determined by typing
odbcinst -j

可以通过键入来确定
odbcinst -j

Which yielded:

这产生了:

unixODBC 2.3.0
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /usr/local/etc/odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

My odbc.ini files already exists in /etc, so the solution was to copy them over over from /etc/ to /usr/local/etc/

我的 odbc.ini 文件已经存在于 /etc 中,所以解决方案是将它们从 /etc/ 复制到 /usr/local/etc/

cp /etc/odbc.ini /etc/odbcinst.ini /usr/local/etc/

cp /etc/odbc.ini /etc/odbcinst.ini /usr/local/etc/

Edit: It's also worth noting that the path outputted by the odbcinst -j command can change depending on using sudoor not.

编辑:还值得注意的是,odbcinst -j 命令输出的路径可以根据使用sudo与否而改变。

回答by Andy G

For me, it was all down to a single whitespace character.

对我来说,这一切都归结为一个空白字符。

$cat /home/ec2-user/.odbc.ini
[DSNNAME]
Driver =FreeTDS
Description=description
Server =serverpath
Port =1433
Database =dbname

Gave me the “Data source name not found, and no default driver specified” error.

给了我“未找到数据源名称,并且没有指定默认驱动程序”错误。

Removing all the whitespaces before the '=' character made it work.

删除 '=' 字符之前的所有空格使其工作。

On a secondary note, using osql for DSN connection testing gives you a much more verbose description of any errors. It helped me a lot in the process.

其次,使用 osql 进行 DSN 连接测试可以为您提供对任何错误的更详细的描述。在这个过程中对我帮助很大。

$ osql -S DSNNAME -U username -P password
checking shared odbc libraries linked to isql for default directories...
    trying /txM ... no
    trying /tmp/sql ... no
    trying /tmp/sql ... no
    trying /w}H ... no
    trying /usr/loc ... no
    trying /tmp/sql.log ... no
    trying /home ... no
    trying /.odbc.ini ... no
    trying /usr/local/etc ... OK
checking odbc.ini files
    reading /home/ec2-user/.odbc.ini
[DSNNAME] found in /home/ec2-user/.odbc.ini
found this section:
    [DSNNAME]
    Driver?=FreeTDS
    Description=description
    Server?=serverpath
    Port?=1433
    Database?=dbname

looking for driver for DSN [DSNNAME] in /home/ec2-user/.odbc.ini
  no driver mentioned for [DSNNAME] in .odbc.ini
looking for driver for DSN [default] in /home/ec2-user/.odbc.ini
osql: error: no driver found for [DSNNAME] in .odbc.ini

Comparing the error message against my ini file made triaging the issue a lot easier.

将错误消息与我的 ini 文件进行比较使得对问题进行分类变得更加容易。

回答by DavidJ

For others troubleshooting this same generic error, ensure that you didn't accidentally add extraneous characters at the top of your odbc.ini file - which causes it to be invalid and all data-source declarations to be silentlyignored (the error giving no indication of the specific problem).

对于其他解决相同一般错误的人,请确保您没有在 odbc.ini 文件的顶部意外添加无关字符 - 这会导致它无效并且所有数据源声明都被静默忽略(该错误没有给出任何指示具体问题)。

回答by yzark

I have the same problem. It turns out that the data source file missed: /etc/ODBCDataSources. Just touch that file and it works.

我也有同样的问题。原来是数据源文件漏掉了:/etc/ODBCDataSources. 只需触摸该文件,它就可以工作。