2011-06-24 6 views
5
私はNode.jsのからのpostgresデータベースに接続しようとしている

のNode.jsが、私は常にいくつかの奇妙なエラーはPostgresの接続の問題

ENOTFOUND, Domain name not found 

私はPG「」の使用Node.jsのモジュールを取得します。私は別の接続文字列を見て、いくつかの例で

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

あなたは正しいですどちらを教えていただけますか?そして、この問題の原因は何ですか?

+0

コードを投稿できますか? – Kuberchaun

答えて

9

ここに私のPGデータベースにウェブインタフェースを与えるために使用したコードがあります。カールやウェブブラウザ経由で送信するコマンドに応じて、レコードを接続し、挿入/削除/選択することができます。

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'); 
});