Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node server can't connect to postgres db

I recently switched from MySQL to postgres as my database for an node.js project. While I'm able to reach my remote postgres database from my local pgAdmin III (OSX) client, so far I've been unable to connect to my database through node.js. I'm sure that the credentials I entered for pgAdmin and my node.js were exactly the same. The other thing I've tried was setting my local ipadress to trust in stead of md5 in the pg_hba.conf at my database server. Is there anything I did wrong? My favourite search engine came up with some worrying hits about resetting my local os. I just used the example from the github repo doc of node-postgres:

var pg = require('pg');

var conString = "postgres://myusername:mypassword@hostname:5432/dbname";

var client = new pg.Client(conString);
client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  client.query('SELECT NOW() AS "theTime"', function(err, result) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].theTime);
    client.end();
  });
});

And these are the errors I get every time I try to start my server:

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

Help would be greatly appreciated

like image 995
Dani Avatar asked Feb 12 '14 21:02

Dani


People also ask

Can't connect to PostgreSQL database?

To be sure that PostgreSQL is running, you can also restart it with systemctl restart postgresql. If this does not fix the problem, the most likely cause of this error is that PostgreSQL is not configured to allow TCP/IP connections.

Is node JS compatible with PostgreSQL?

Using node-postgres , you will be able to write Node. js programs that can access and store data in a PostgreSQL database. In this tutorial, you'll use node-postgres to connect and query the PostgreSQL (Postgres in short) database. First, you'll create a database user and the database in Postgres.


1 Answers

It appears that node-postgres doesn't accept a hashtag in your password. After removing the hashtag I was able to connect without a problem. Wouldn't have thought of that and it didn't strike me as a problem since pgAdmin accepted it.

The best solution would be to use encodeURIComponent to encode your password string. This would allow for hashtags to be used in the URL.

like image 51
Dani Avatar answered Oct 13 '22 06:10

Dani