错误:用户" myuser"密码验证失败

时间:2017-04-15 13:31:30

标签: javascript node.js postgresql debian

如果我在/etc/postgresql/9.4/main/pg_hba.conf中有一条特别信任我的特定用户的记录

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             myuser                                trust

因为我正在使用debian,所以我重启了像这样的postgresql

sudo /etc/init.d/postgresql restart

以下是我的整个源文件测试:

const pg = require('pg');
const connectionString = "postgres://myuser:mypassword@localhost/mydbname";

const client = new pg.Client(connectionString);
client.connect();
const query = client.query('SELECT * FROM USERS');
query.on('end', () => { client.end(); });

这是我一直得到的错误:

error: password authentication failed for user "myuser"
    at Connection.parseE (/home/myuser/webserver/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/home/myuser/webserver/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/home/myuser/webserver/node_modules/pg/lib/connection.js:105:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)

还值得注意的是,做以下工作:

psql -h localhost -U myuser mydb

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

作为the documentation状态,local仅适用于UNIX套接字连接,而您正在建立与localhost的TCP连接。

使用这样的一行:

host    all    myuser    127.0.0.1/32    trust

使用IPv4信任来自localhost的所有连接(使用地址::1/128进行IPv6)。