未编译Psycopg2 Python SSL支持 [英] Psycopg2 Python SSL Support is not compiled in
问题描述
我正在尝试使用带有sslmode ='required'参数的psycopg2连接到我的postgres数据库;但是,出现以下错误
I am trying to connect to my postgres database using psycopg2 with sslmode='required' param; however, I get the following error
psycopg2.OperationalError: sslmode value "require" invalid when SSL support is not compiled in
这里有一些有关我的系统的详细信息
Heres a couple details about my system
- Mac OS X El Capitan
- Python 2.7
- 通过pip安装了psycopg2
- 通过自制软件安装python
- Mac OS X El Capitan
- Python 2.7
- Installed psycopg2 via pip
- Installed python via homebrew
这是我试图解决此问题的方法
Here is what I tried to do to fix the problem
-
brew卸载python
-
哪个python
仍然显示python生活在/ usr / local / bin / python
中,试图将其卸载但无法。听说这是操作系统使用的python,无论如何都不应该卸载 -
brew install python --with-brewed-openssl --build-from-源
-
pip卸载psycopg2
-
pip install psycopg2
brew uninstall python
which python
still shows python living in/usr/local/bin/python
, tried to uninstall this but couldnt. And heard that this is the python that the OS uses and should not be uninstalled anywaysbrew install python --with-brewed-openssl --build-from-source
pip uninstall psycopg2
pip install psycopg2
完成所有这些操作后,仍然会发生异常。我正在通过#!/ usr / bin / env python
运行此python脚本,不确定是否很重要,但与显示哪个python
After doing all of this, the exception still happens. I am running this python script via #!/usr/bin/env python
Not sure if it matters, but that is a different directory than the one that which python
shows
推荐答案
由于是通过pip安装的,因此您应该使用最多的最新版本的psycopg2(2.6.1)。
在仔细研究代码之后,似乎在connection_int.c中引发了异常,该异常直接调用postgresql-c-libraries建立数据库连接。呼叫发生的方式如下:
Since you're installing via pip, you should be using the most recent version of psycopg2 (2.6.1). After a little digging through the code, it seems that the exception is being thrown in connection_int.c, which directly calls the postgresql-c-libraries to set up the db-connection. The call happens like so:
self->pgconn = pgconn = PQconnectStart(self->dsn);
Dprintf("conn_connect: new postgresql connection at %p", pgconn);
if (pgconn == NULL)
{
Dprintf("conn_connect: PQconnectStart(%s) FAILED", self->dsn);
PyErr_SetString(OperationalError, "PQconnectStart() failed");
return -1;
}
else if (PQstatus(pgconn) == CONNECTION_BAD)
{
Dprintf("conn_connect: PQconnectdb(%s) returned BAD", self->dsn);
PyErr_SetString(OperationalError, PQerrorMessage(pgconn));
return -1;
}
在connect语句中对psycopg2.connect()指定的关键字是
The keywords which were specified in your connect statement to psycopg2.connect() are being handled to that function and errors are returned as OperationalError exception.
错误实际上是直接在postgresql-lib中生成的-您可能要检查您所使用的版本
The error is actually being generated directly in the postgresql-lib - you may want to check which version you are using, how it was built and, if possible, upgrade it to a version with SSL support or rebuilt it from source with SSL enabled.
postgresql-docs还指出,它的使用方式,构建方式以及在可能的情况下将其升级到支持SSL的版本或从启用SSL的源重新构建。如果sslmode设置为 require , verify-ca 或 verify-full ,则缺少SSL支持将引发错误。请参见此处 > sslmode
作为参考。
The postgresql-docs also state that missing SSL support will raise an error, if the sslmode is set to require, verify-ca or verify-full. See here under sslmode
for reference.
postgres网站列出了几种从二进制软件包中安装postgres的方法,因此您可以选择一种适合您需要的方法。我不熟悉OSX,所以我不建议最好。
The postgres-website lists several ways to install postgres from binary packages, so you might choose one which suits your needs. I'm not familiar with OSX, so I don't have a recommendation what's best.
这个问题可能也有帮助。
您还需要重新安装psycopg2-模块,请确保在重建它时使用新安装的库。请参阅链接的问题(总之,您需要将路径 pg_config
放置在新安装的$ PATH中,当运行 pip时)安装psycopg2
)。
You also need to reinstall the psycopg2-module, be sure to use the newly installed lib when rebuilding it. Refer to the linked question (in short, you will need to place the path to pg_config
which is included in your new installation to $PATH when running pip install psycopg2
).
这篇关于未编译Psycopg2 Python SSL支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!