2016-03-15 100 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

相關問題