与数据库连接时psycopg2.connect()失败

时间:2019-07-16 05:49:43

标签: postgresql jupyter-notebook

我正在尝试使用Jupyter笔记本通过postgresql2访问数据库,但收到以下错误消息。

OperationalError:无法创建SSL上下文:没有此类文件


import pandas as pd
import psycopg2

#Connect to postgres
conn_string = "host='xx' sslmode='require' \
               dbname='dbname' port='xx' user='xx' \
               password='xx'"

#Create rework dataset
conn = psycopg2.connect(conn_string)
postgreSQL_select_Query = u'SELECT * FROM "xx"."yy"'
conn.set_client_encoding('UNICODE')
cursor = conn.cursor()
cursor.execute(postgreSQL_select_Query)
colnames = [desc[0] for desc in cursor.description]
df_imp = cursor.fetchall()

df = pd.DataFrame(data=df_imp, columns=colnames)

预期结果是对数据库的访问和数据帧的生成。 实际结果是OperationalError:无法创建SSL上下文:逐步没有此类文件conn = psycopg2.connect(conn_string)

---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-2-932b2fb01c9f> in <module>
      5 
      6 #Create rework dataset
----> 7 conn = psycopg2.connect(conn_string)
      8 postgreSQL_select_Query = u'SELECT * FROM "xx"."xx"'
      9 conn.set_client_encoding('UNICODE')

~\AppData\Local\Continuum\anaconda3\lib\site-packages\psycopg2\__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
    128 
    129     dsn = _ext.make_dsn(dsn, **kwargs)
--> 130     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
    131     if cursor_factory is not None:
    132         conn.cursor_factory = cursor_factory

OperationalError: could not create SSL context: No such process

1 个答案:

答案 0 :(得分:0)

尝试了几种解决方案后,问题出在psycopg2库的版本上。 conda更新不会安装该库的最新版本。但是,pip做到了,然后我的代码再次起作用!