2016-03-15 69 views
0

当我使用CLI连接到我的数据库时,一切正常。Postgresql:我可以连接命令行,但不能连接node-postgres

psql dbname postgres 

psql问我密码我之前设置(与ALTER)和我连接。

在我的pg_hba.conf文件,我得到了下面一行:

local all    postgres        md5 

但是,当我使用节点Postgres的尝试同样的事情,我总是得到一个错误:

could not connect to postgres { [error: Ident authentication failed for user "postgres"] 

代码我使用的是基本的(假设我的密码是mypwd)

var pg = require('pg'); 
var conString = "postgres://postgres:[email protected]/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(); 
    }); 
}); 

你有什么想法吗?

[编辑:我的完整的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 
+0

这很奇怪,但您的nodejs代码尝试使用ident aut-method连接到数据库,而不是md5。你可以添加完整的pg_hba.conf到你的文章? –

回答

1

有 “本地” 和 “本地主机” 之间的差异。第一个将通过一个unix域套接字第二个通过TCP/IP连接到(通常)127.0.0.1

你应该在它们看到分开的行pg_hba.conf

通常您可以通过将路径(例如/var/run/postgresql/)替换为服务器名称来连接到unix域套接字。

1

实际上,问题出现在alexander.polomodov注意到的pg_hba.conf文件中。我只是用“md5”改变了“身份”这个词,它起作用。我不明白为什么我需要把它放在两条线上:

# IPv4 local connections: 
host all    all    127.0.0.1/32   md5 
# IPv6 local connections: 
host all    all    ::1/128     md5 

所以,如果你可以解释,随时发表评论。

+0

查看Richard Huxton的解答。 – MarAja

相关问题