当我使用CLI连接到我的数据库时,一切工作正常。
psql dbname postgrespsql询问我之前设置的密码(使用ALTER),然后我就连接上了。
在我的pg_hba.conf文件中,我得到了以下行:
local all postgres md5但是当我尝试使用node-postgres做同样的事情时,我总是得到一个错误:
could not connect to postgres { [error: Ident authentication failed for user "postgres"]我使用的代码是basic (假设我的密码是mypwd)
var pg = require('pg');
var conString = "postgres://postgres:mypwd@localhost/database";
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});你有什么想法吗?
编辑: My full pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all postgres md5
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident发布于 2016-03-15 21:33:36
"local“和"localhost”是有区别的。第一个将通过unix域套接字连接,第二个将通过tcp/ip连接到(通常) 127.0.0.1
您应该会在pg_hba.conf中看到它们的单独行。
您通常可以通过将路径(例如/var/run/postgresql/)放在服务器名称的位置来连接到unix域套接字。
发布于 2016-03-15 21:33:44
实际上,正如alexander.polomodov注意到的那样,问题出在pg_hba.conf文件中。我只是把"ident“这个词改成了"md5”,这样就行了。我不明白为什么我需要把它放在这两行中:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5因此,如果您可以解释,请随时发表评论。
https://stackoverflow.com/questions/36011812
复制相似问题