2011-06-24 39 views
5

我試圖連接到從node.js的一個Postgres數據庫,但我總是得到一些奇怪的錯誤Node.js的Postgres的連接問題

ENOTFOUND, Domain name not found 

,我用的是「PG」 Node.js的模塊。

在幾個例子,我看到了不同的連接字符串:

pg://, tcp:// and postgres:// 

能否請你告訴我哪一個是正確的?什麼會導致這個問題?

+0

你可以發佈你的代碼嗎? – Kuberchaun

回答

9

這是我用來嘗試給我的PG數據庫一個Web界面的一些代碼。它能夠根據您通過curl或Web瀏覽器發送的命令來連接和插入/刪除/選擇記錄。

var app = require('express').createServer(); 
var pg = require('pg'); 
var conString = "postgres://YOURUSER:[email protected]/dev"; 

var client = new pg.Client(conString); 
client.connect(); 

app.get('/', function(req, res){ 
    res.send('hello world'); 
}); 

app.get('/select/:client_id', function(req, res){ 
    var query = client.query("select '{count:}' as c_count,client_id from test_input where client_id = $1 limit 1", [req.params.client_id]); 
    query.on('row', function(row) { 
    res.send(row); 
}); 
} 
); 

app.get('/insert/:client_id', 

function(req, res) 
{ 

    console.log('called'); 
    client.query("INSERT INTO test_input(client_id) VALUES($1)",[req.params.client_id]); 
    res.send('done'); 
}); 


process.on('uncaughtException', function (err) { 
    console.log(err); 
}); 


app.get('/delete/:client_id', 

function(req, res) 
{ 

    console.log('called'); 
    client.query("DELETE FROM test_input WHERE client_id = $1",[req.params.client_id]); 
    res.send('done'); 
});