I am trying to make a small nodejs app run under osx (10.6.8), and connect to my localhost Postgres database. I am using node-orm (https://github.com/dresende/node-orm2) as well as the Postgres bindings (https://github.com/brianc/node-postgres).
For some reason, when I use orm.connect
and give it the connection url as a string, it fails with:
Error: getaddrinfo ENOENT
at errnoException (dns.js:31:11)
at Object.onanswer [as oncomplete] (dns.js:123:16)
If I change localhost with 127.0.0.1. it also dies with a timeout exception.
Using the vanilla postgress bindings to connect to the DB, using the same connection string is successful, and I even managed to get a few test queries running, got results, etc.
I tried to set up the DB client manually using the vanilla postgress api, an pass it on to orm using orm.use
. This manages to connect successfully to the DB, but every time I try to execute a query, nothing happens. It does not generate an error, but sinply gets stuck in the QueryQueue and doe not call the success callback. I checked the DB logs, and it seems that it has not detected the query either.
What do I do? I am a bit confused
I came across this issue while on a plane. I only receive Error: getaddrinfo ENOENT
when I'm not connected to a network, even though I'm running the database server locally. Here is what I did to fix it:
Instead of using localhost
, use 127.0.0.1
.
So your connection string will change from
postgresql://user:pass@localhost:5432/db_name
to
postgresql://user:[email protected]:5432/db_name
Not able to make out real problem from the question.Looks like your connection string is wrong. But here is a code block which works with https://github.com/brianc/node-postgres
`
var application_root = __dirname,
pg = require('pg');
// database
var conString = "postgres://username:[email protected]:5432/db_name";
function processReqResSql(req, res, sql) {
pg.connect(conString, function (err, client) {
if (err) {
res.writeHead(404, {
'Content-Type': 'application/json'
});
res.write('{couldnt connect to db}');
} else {
var query = client.query(sql);
/
var dbResponse ="";
query.on('row', function(row) {
console.log(row);
dbResponse = dbResponse+JSON.stringify(row);
});
query.on('end', function() {
client.end();
res.writeHead(200, {"Content-Type": "application/json"});
res.write("{ results:{"+dbResponse+"}");
res.end();
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With