python - 无法在 python 中连接到远程 Postgresql 数据库
问题描述
我想用 Python 连接到远程 Postgresql(安装在树莓派上)数据库。我已按照以下链接中的示例进行操作:
https://opensource.com/article/17/10/set-postgres-database-your-raspberry-pi
使用以下代码:
conn = psycopg2.connect('host=192.xxx.x.x user=pi password = raspberry dbname = test')
不知道出了什么问题。任何人的解决方案。
不幸的是,我收到以下错误:
psycopg2.OperationalError:致命:用户“pi”的密码验证失败 致命:用户“pi”的密码验证失败
解决方案
我强烈建议提供凭据作为关键字参数以提高可读性:
psycopg2.connect(
user="...",
password="...",
dbname="...",
host="...",
)
此外,正如评论中提到的@peterh,您提供的凭据可能被错误地解析(但在这种情况下不是)。您可以使用psycopg2
自己的解析器来确认输出:
psycopg2._psycopg.parse_dsn('host=192.xxx.x.x user=pi password = raspberry dbname = test')
返回:
{'user': 'pi',
'password': 'raspberry',
'dbname': 'test',
'host': '192.xxx.x.x'}
考虑到所有因素,您的错误表明用户/密码组合不正确。
推荐阅读
- android-studio - Android 应用程序编译警告“运行时 JAR 文件的版本 1.4 比 API 版本 1.5 所需的版本旧”
- c++ - 如何始终在具有实时数据的 ofstream 文件中包含 N 行
- python - 通过和/或将文本拆分为句子
- c# - 如何将谓词作为参数传递到 ASP.NET MVC 中的 IActionResult
- git - 本地合并分支无法解决 github 上的拉取请求
- apache-spark - Spark Streaming 批处理持续时间
- mysql - Mysql2::Error: Data too long for column 'object' at row 1 尝试使用 Active Record 更新时出错
- reactjs - Firebase消息传递和ReactJS,当网站在前台时,未收到推送通知
- python - Python中的列表切片与索引?
- mysql - MySQL错误即使清除和重新安装也不能解决它