使用Node.js连接到Heroku上的postgres

时间:2016-06-27 15:10:09

标签: node.js postgresql heroku

我尝试通过以下方式连接到Heroku上的postgres数据库:

var pg = require('pg');
pg.defaults.ssl = true;
var pool = new pg.Pool(process.env.DATABASE_URL);

pool.connect(function(err, client, done) {
if(err) {
  return console.error('error fetching client from pool', err);
}

我已经确认了proccess.env.DATABASE_URL的值,它具有正确的格式,我可以通过psql postgres成功连接到它:// XXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXX.com-west-1.compute.amazonaws.com :5432 / XXXXXXXX

这是输出,如果输出的话,让我感到困惑的是127.0.0.1引用。

2016-06-27T14:58:52.714154+00:00 app[web.1]: Express server listening on port 18119
2016-06-27T14:59:29.175122+00:00 app[web.1]: error fetching client from pool { Error: connect ECONNREFUSED 127.0.0.1:5432
2016-06-27T14:59:29.175133+00:00 app[web.1]:     at Object.exports._errnoException (util.js:949:11)
2016-06-27T14:59:29.175134+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:972:20)
2016-06-27T14:59:29.175135+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
2016-06-27T14:59:29.175136+00:00 app[web.1]:   code: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]:   errno: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]:   syscall: 'connect',
2016-06-27T14:59:29.175138+00:00 app[web.1]:   address: '127.0.0.1',
2016-06-27T14:59:29.175139+00:00 app[web.1]:   port: 5432 }

1 个答案:

答案 0 :(得分:0)

试试这个:

var pg = require('pg').native
pg.connect(process.env.databaseURL, function (err, conn, done) {
    ...
}