9

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

Dani
  • 2,448
  • 8
  • 27
  • 44
  • 2
    Hm, what does a `host hostname` get you from the command line on that server? Is that one of its IPs, and does it listen to it? The error would seem the host does not know that it should be the 'hostname' you think it should be called... On a side note: I see only examples with `postgres://`, not `pg://`, I don't know if that matters... – Wrikken Feb 12 '14 at 22:26
  • Thanks for your reply Wrikken. The output for the db server is: `Host hostname not found: 3(NXDOMAIN)`. The db server is a VPS. I supposed that shouldn't matter. – Dani Feb 13 '14 at 17:42
  • 1
    Well, so, I mean: the hostname you enter is... not the servers hostname? It tells you it cannot translate the name to an IP, so fails. So, enter the proper one perhaps (or go for `localhost` if you know it to be the same server the code is running on as mentioned in an answer below). If that also fails: try to give the proper _IP_ address. – Wrikken Feb 13 '14 at 19:24
  • I tested it on the db server aswell. It appeared to be the password. Thanks for your Dutch help, your insights helped me get there – Dani Feb 13 '14 at 20:44
  • "Dutch help"? That's the first time I heard that phrase. As a Dutchman, I don't know what to think of it ;) – Wrikken Feb 13 '14 at 21:04
  • it's made up by some fellow Dutchman – Dani Feb 13 '14 at 21:10

3 Answers3

31

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.

Dani
  • 2,448
  • 8
  • 27
  • 44
1

The interface in node.js that I used can be found here https://github.com/brianc/node-postgres

var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword@localhost:5432/YourDatabase";

try changing pg:// to postgres://

This was scraped from another stackoverflow article: How to make connection to Postgres via Node.js

Community
  • 1
  • 1
matthew.peters
  • 571
  • 4
  • 6
  • I've tried that aswell, I changed the postgres:// to pg:// after postgres:// didn't work. Forgot to mention that – Dani Feb 13 '14 at 07:37
0

I managed to solve this problem by removing the double quotes and the single quotes from the env_file in docker swarm, in this case, different from docker compose, docker swarm interprets everything that is written after the "=" in the envs, for example, replace this:

ENV='production'

for this:

ENV=production
Daniel Bentes
  • 508
  • 4
  • 10