I am trying to connect to postgres from my node project. So far this is my code
var pg = require('pg');
var connectionString = 'postgress://username:mypssword@localhost:5432/dbname';
var client = new pg.Client(connectionString);
client.connect(function(err) {
if (err) {
console.log(err);
}
});
I get this error:
{
[Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect'
}
I really need help on this
-
2Don't forget to accept an answer, you get 2 rep for it, and it helps people in the future figure out what they need.SomeKittens– SomeKittens2014年04月21日 21:17:41 +00:00Commented Apr 21, 2014 at 21:17
3 Answers 3
it should be postgres://
not postgess:
also you could use an object instead of the string as documented here: pg.Client
-
Changing it from a string into an object actually made this one work for me. Cheers.Michael Thelin– Michael Thelin2014年12月17日 23:14:53 +00:00Commented Dec 17, 2014 at 23:14
Error: connect ECONNREFUSED
means that you're trying to connect to something that won't allow it (and thus is refusing the connection). Make sure your connectionString is correct. The one issue I can see from here is that it starts with:
postgress://
instead of
postgres://
(You have an extra s
).
I was also facing the same issue for a local Postgres connection. Here your application is not able to establish a connection with Postgres database. Try this solution for Windows OS, replace string "localhost"
with "host.docker.internal"
.
So your new URL will be
postgres://DbUser:[email protected]:5432/DbName