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
host hostnameget 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.localhostif 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.