我正在尝试使用 psycopg2 将数据从 pgadmin 数据库中提取到 pandas 数据框中。我的 .py 文件在终端中正常运行,但是当我在 python 交互式(vs 代码)或 jupyter 笔记本中运行它时,出现以下错误。
代码:
with psycopg2.connect(conn_string) as conn:
sql = "SELECT * FROM table"
df = pd.read_sql_query(sql, conn)
错误:
OperationalError
---> 19 with psycopg2.connect(conn_string) as conn:
20 sql = "SELECT * FROM table"
21 df = pd.read_sql_query(sql, conn)
//anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py in connect(dsn, connection_factory, cursor_factory, **kwargs)
124
125 dsn = _ext.make_dsn(dsn, **kwargs)
--> 126 conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
127 if cursor_factory is not None:
128 conn.cursor_factory = cursor_factory
OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
有谁知道为什么会这样?当我在 vs 代码中打开 python 交互时,我也看到了这个错误:
Jupyter kernel cannot be started from 'Python 3.7.4 64-bit'.
Using closest match Python 3.7.3 64-bit ('anaconda3': conda) instead.
Error starting original kernel: Error: Module 'notebook' not installed.