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

asked Feb 12, 2014 at 21:53
6
  • 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... Commented Feb 12, 2014 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. Commented Feb 13, 2014 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. Commented Feb 13, 2014 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 Commented Feb 13, 2014 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 ;) Commented Feb 13, 2014 at 21:04

3 Answers 3

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.

answered Feb 13, 2014 at 20:41
5
  • 1
    Doh! Encode your password for urls of course ;) Nice to know it's been resolved. Commented Feb 13, 2014 at 21:02
  • 5
    Great thanks a lot. Just doing encodeURIComponent('passwordwith#') fixed the problem. Commented Sep 14, 2014 at 16:51
  • 1
    Thanks - can't imagine ever finding this by debugging, probably would have given up! Commented Jul 8, 2015 at 11:48
  • exactly my case. I use encodeURIComponent and the issue was resolved. thank you all for your help Commented Oct 24, 2016 at 17:31
  • character "#" in the password made it not connect.. Changed the password and node connected with db Commented Apr 29, 2017 at 19:48
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

answered Feb 13, 2014 at 1:26
1
  • I've tried that aswell, I changed the postgres:// to pg:// after postgres:// didn't work. Forgot to mention that Commented Feb 13, 2014 at 7: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
answered Jan 10, 2023 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.