How do you connect to a Postgres Database with no password using pg-promise?

て烟熏妆下的殇ゞ 提交于 2019-12-10 16:19:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!