python 连接到 PostgreSQL 时无法使用 .pgpass

标签 python postgresql

使用 python 脚本连接到 postgresql 数据库时无法使用 .pgpass 文件((( 我的连接 python 脚本没有密码:

conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");

我的.pgpass:

*:*:*:postgres:password

位于/var/lib/postgresql - postgres 用户的主目录。 但是,当我本地连接到数据库时:

psql -U postgres

不询问密码。但对于 python 会引发错误:

root@debian:/python_codes/Junior/Level1/DB1/Generator_py# python connect_db.py 
Unable to connect to db...
Traceback (most recent call last):
  File "connect_db.py", line 34, in connectdb
    conn = psycopg2.connect("dbname='postgres' user='postgres' host='192.168.136.129'");
  File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
    connection_factory=connection_factory, async=async)
OperationalError: fe_sendauth: no password supplied

最佳答案

根据此输出的提示:

root@debian:/python_codes/Junior/Level1/DB1/Generator_py# python connect_db.py

您以 Unix 用户 root 身份运行该命令,因此相应的 .pgpass 文件必须位于 root 的 HOME 目录中,该目录表示不是 /var/lib/postgresql 而是可能 /root/

此外,其权限必须限制为所有者可读,即 chmod 600 $HOME/.pgpass

关于python 连接到 PostgreSQL 时无法使用 .pgpass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28931828/

相关文章:

python - 用于查找域的 pagerank 的脚本

python - Keras 密集层输入未展平

python - 如何在 Python 中计算真正大整数的 exp(x)?

postgresql - 开始...每 50 行提交一次

unit-testing - cakePHP 中的固定装置名称(测试)

Python plotly |禁用具有子图的图形中的所有范围 slider

python - 存储大量数据的最智能方式

ruby-on-rails - 迁移无法识别在 schema_search_path 上创建的 hstore 扩展

python - Celery 在将我的查询集 obj 作为参数传递时引发错误

sql - Postgres 值 '' 单引号与 NULL