Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems making a node.js app connect to PostgreSQL, using node-orm (under osx 10.6.8)

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

like image 707
Preslav Rachev Avatar asked Dec 09 '12 15:12

Preslav Rachev


2 Answers

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
like image 193
Michael Theriot Avatar answered Sep 28 '22 07:09

Michael Theriot


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

            }
like image 21
randomness Avatar answered Sep 28 '22 05:09

randomness