6

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

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Preslav Rachev
  • 3,983
  • 6
  • 39
  • 63
  • This seems more like a DNS issue, rather than a node issue. Are you using http:// when specifying the connection to the DB? See this question: http://stackoverflow.com/questions/9580226/nodejs-httprequest-with-data-getting-error-getaddrinfo-enoent – Slavo Dec 10 '12 at 09:04
  • This is my connection url: postgresql://user:pass@localhost:5432/db_name – Preslav Rachev Dec 10 '12 at 10:00

4 Answers4

8

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:pass@127.0.0.1:5432/db_name
Michael Theriot
  • 994
  • 7
  • 13
  • Faced the same problem while I was in the kitchen had unstable wifi. Changing to 127.0.0.1 fixed the problem for me! Thank you <3 – Frank Roth Mar 06 '19 at 14:27
1

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:password@172.28.6.250: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();
             });

            }
randomness
  • 1,377
  • 1
  • 14
  • 21
0

Check whether your postgre server has started. You can start it by giving the following command:

pg_ctl -D <db-name> -l logfile start
Zarko
  • 30
  • 1
  • 6
0

In my case I forgot to add Headers as required ex. Content-Type = application/json

If this is not the error causing case you can replace your db config as:

Old:

var config = {
        host: 'localhost',
        database: 'dbname',
        user: 'postgres',
        password: 'secret',
        port: 5432
    };

Replaced Code:

var config = {
    host: '127.0.0.1',
    database: 'dbname',
    user: 'postgres',
    password: 'secret',
    port: 5432
};

And use this config to connect DB:

const {
        Pool
    } = require('pg');
    const pool = new Pool(config);

    pool.connect((err, client, done) => {
// Your code logic
};
Pawan Pareek
  • 469
  • 4
  • 7