【问题标题】:node.js postgres connection problemnode.js postgres连接问题
【发布时间】:2011-09-21 22:47:36
【问题描述】:
我正在尝试从 node.js 连接到 postgres 数据库,但我总是遇到一些奇怪的错误
ENOTFOUND, Domain name not found
我使用的 node.js 模块是 'pg'。
在几个例子中我看到了不同的连接字符串:
pg://, tcp:// and postgres://
你能告诉我哪个是正确的吗?什么会导致这个问题?
【问题讨论】:
标签:
postgresql
node.js
database-connection
【解决方案1】:
这是我用来尝试为我的 PG 数据库提供 Web 界面的一些代码。它能够连接和插入/删除/选择记录,具体取决于您通过 curl 或 Web 浏览器发送的命令。
var app = require('express').createServer();
var pg = require('pg');
var conString = "postgres://YOURUSER:YOURPASSWORD@localhost/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');
});