4

I am trying to initialize a connection to my PG database doing this:

    pgp = require('pg-promise')({
    // Initialization Options
}),
cn = {
    host: 'activity.postgres.activity', // server name or IP address;
    port: 5432,
    database: 'activity',
    user: 'postgres',
    password: ''
},
db = pgp(cn),

But I keep getting:

Error: connect ECONNREFUSED 172.20.0.3:5432

Any idea why?

RESOLVED: set the listen_addresses = '*' in the postgresql.conf file

2 Answers2

3

The issue is not with the library you are using or the password.

The error tells you that there is no server available at that IP/port.

See also: node-postgres get error connect ECONNREFUSED

i.e. check first that you can connect to it via PSQL.

Community
  • 1
  • 1
vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • This issue was solved by changing the line "listen_addresses" in the postgresql.conf file to be '*' to accept all incoming connections. thanks for pointing me in the right direction. – Geoff Paul Bremner Mar 07 '16 at 18:53
0

You can find this via a Google search in here

Example for using "pg-promise":

var pgp = require('pg-promise')(/*options*/);

var cn = {
    host: 'my_host', // server name or IP address;
    port: 5401,
    database: 'myDatabase',
    user: 'myUser',
    password: 'myPassword'
};

var db = pgp(cn);
// SELECT all rows with id = 111 in my_table
db.one("SELECT * FROM my_table WHERE id=$1", 111)
    .then(function (result) {
        console.log(result); // print user result;
    })
    .catch(function (error) {
        console.log(error); // print why failed;
    });

And other an example connect with Postgres database that use "pg" module:

var Client                      = require('pg').Client;

var configServer                        = {
    user                : 'user', // if don't have user and pass then ''
    password            : 'my_pass',
    database            : 'my_database',
    host                : 'my_host',
    port                : 54103 // this is a example
};
var client      = new Client(configServer);
    client.connect();

// SELECT all rows in my_table
var sql = "SELECT * FROM my_table";
client.query(sql, callback);
DinhNguyen
  • 356
  • 3
  • 14
  • 1
    I don't think he needs an example of using libraries when his connection with the server is broken. – vitaly-t Mar 05 '16 at 18:53