问题
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
回答1:
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.
回答2:
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);
来源:https://stackoverflow.com/questions/35807792/how-do-you-connect-to-a-postgres-database-with-no-password-using-pg-promise