2011-06-24 38 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'); 
});