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
3 Answers 3
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.
-
1Doh! Encode your password for urls of course ;) Nice to know it's been resolved.Wrikken– Wrikken2014年02月13日 21:02:43 +00:00Commented Feb 13, 2014 at 21:02
-
5Great thanks a lot. Just doing encodeURIComponent('passwordwith#') fixed the problem.Zaptree– Zaptree2014年09月14日 16:51:38 +00:00Commented Sep 14, 2014 at 16:51
-
1Thanks - can't imagine ever finding this by debugging, probably would have given up!Tobias Fünke– Tobias Fünke2015年07月08日 11:48:28 +00:00Commented Jul 8, 2015 at 11:48
-
exactly my case. I use encodeURIComponent and the issue was resolved. thank you all for your helpLucas Do Amaral– Lucas Do Amaral2016年10月24日 17:31:19 +00:00Commented Oct 24, 2016 at 17:31
-
character "#" in the password made it not connect.. Changed the password and node connected with dbmythicalcoder– mythicalcoder2017年04月29日 19:48:26 +00:00Commented Apr 29, 2017 at 19:48
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
-
I've tried that aswell, I changed the postgres:// to pg:// after postgres:// didn't work. Forgot to mention thatDani– Dani2014年02月13日 07:37:07 +00:00Commented Feb 13, 2014 at 7:37
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
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 withpostgres://
, notpg://
, I don't know if that matters...Host hostname not found: 3(NXDOMAIN)
. The db server is a VPS. I supposed that shouldn't matter.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.