无法通过 Linux 上的节点连接到 Postgresql 数据库

Can't Connect to Postgresql DB Through Node on Linux

当我尝试通过命令行连接到我的数据库(在 Linux 机器上)时,我能够做到这一点:

$ psql -U me -W
Password for user me: 
psql (9.5.7)
Type "help" for help.

me=# 

我也可以不用密码访问:

    $ psql -U me # this works

但是,当我尝试通过 Node 库 knex 进行连接时(它只是在后台使用 pg),我收到以下错误:

error: password authentication failed for user "me"

我认为它与在 Linux 中设置的 Linux users/Postgres 用户的组合有关,但我没有尝试修复它。我尝试更改我的连接 URL 字符串,但即使是正确的(我认为)也不起作用:

DATABASE_URL=postgres://localhost:5432/mydb
DATABASE_URL=postgres://me:mypassword@localhost:5432/mydb
# (neither works)

我也试过添加:

host mydb me 127.0.0.1/32 trust

我的 /etc/postgresql/9.5/main/pg_hba.conf,但这也没有帮助。

我很确定我只需要以某种方式以正确的方式告诉 Node "use this user/password"...但我不知道如何做。

如有任何帮助,我们将不胜感激。

编辑 我尝试将 /etc/postgresql/9.5/main/pg_hba.conf 设置为:

host    all         all         127.0.0.1/32         trust
host    all         al          ::1/128              trust

或者换句话说 "let everyone in without a password"。当我 psql -W 它现在接受我输入的任何密码......但即便如此我仍然得到:

error: password authentication failed for user "me"

Node/knex/pg 尝试连接时出错。

似乎 UNIX 域套接字未获得 node.js 的优惠待遇。

基于test for pg-connection-string,我怀疑

socket:///var/run/postgresql/?db=mydb

应该可以。

我通过以下方式让一切正常运转:

A) 向 /etc/postgresql/9.5/main/pg_hba.conf 添加规则以始终信任 localhost:

上的连接
host    all         all         localhost            trust

B) 使用 URL 的连接:

postgres://me:mypassword@localhost:5432/mydb