samedi 18 avril 2015

node-postgres get error connect ECONNREFUSED

I want to connect my apps nodejs using node-posgres to PostgreSQL. My apps in localhost (ubuntu) and my postgresql in virtual machine in the cloud with operating system OpenSuse. This is my code :



var express = require('express');
var app = express();


var http = require('http');
var pg = require('pg');

var conString = "postgres://postgres:mypassword@myurl.com:5432/postgres";


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

// respond with "SERVER" on the homepage
app.get('/server', function (req, res) {

var client = new pg.Client(conString);
client.connect(function (err) {
if (err) {
return console.error('could not connect to postgres', err);
}
console.log('CONNECT PASSED');
client.query('SELECT * FROM visit', function (err, result) {
if (err) {
return console.error('error running query', err);
}
console.log('QUERY PASSED');
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});

});


var server = app.listen(3000, function () {

var host = server.address().address;
var port = server.address().port;

console.log('Example app listening at http://%s:%s', host, port);

});


But i got an error like this:



could not connect to postgres { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }


Please help me how to solve this. Thanks!


Aucun commentaire:

Enregistrer un commentaire