【发布时间】:2021-09-24 13:34:30
【问题描述】:
我可以像这样从本地 psql 实例中读取数据:
engine = create_engine('postgresql://postgres:postgres@localhost/db_name')
df = pd.read_sql("select * from table_name;", engine)
我有一个远程 postgresql 服务器,我在 PgAdmin4 和 pycharm 中都通过 ssh 隧道成功访问了该服务器。我使用公钥文件登录到远程服务器。现在,我的问题是如何使用 pandas 访问该数据库。我试过了:
engine = create_engine('postgresql://username:password@localhost/db_name')
这里,用户名和密码是远程数据库的。我得到sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user。但是,使用相同的用户名和密码,我可以访问 PgAdmin 中的表。
根据我的阅读,由于 ssh 隧道,我必须使用 localhost 而不是远程服务器地址,对吧?在 pgAdmin 中,我可以看到服务器正在运行。所以,我的问题是如何使用 ssh 隧道从远程 postgresql 数据库中读取表?在示例中,我看到人们使用不同的端口(不同于 5432),但对我来说,只有在我使用端口 5432 时设置才有效。我已断开所有其他服务器以避免端口冲突,但我得到了同样的错误。
【问题讨论】:
-
你是如何创建隧道的?
-
在 pgAdmin 中,我创建了新服务器,在
Connection下将localhost作为主机名,并在那里提供了远程数据库用户名和密码。最后在SSH Tunnel下,我设置了ssh隧道
标签: python pandas postgresql ssh sqlalchemy