如何使用pg-promise连接到没有密码的Postgres数据库?

时间:2022-01-05 15:48:03

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

我正在尝试初始化到我的PG数据库的连接:

    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

错误:连接ECONNREFUSED 172.20.0.3:5432

Any idea why?

知道为什么吗?

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

已解决:在postgresql.conf文件中设置listen_addresses ='*'

2 个解决方案

#1


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.

该错误告诉您该IP /端口没有可用的服务器。

See also: node-postgres get error connect ECONNREFUSED

另请参阅:node-postgres获取错误连接ECONNREFUSED

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

即首先检查您是否可以通过PSQL连接到它。

#2


0  

You can find this via a Google search in here

您可以在此处通过Google搜索找到此信息

Example for using "pg-promise":

使用“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:

和其他一个例子连接使用“pg”模块的Postgres数据库:

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);

#1


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.

该错误告诉您该IP /端口没有可用的服务器。

See also: node-postgres get error connect ECONNREFUSED

另请参阅:node-postgres获取错误连接ECONNREFUSED

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

即首先检查您是否可以通过PSQL连接到它。

#2


0  

You can find this via a Google search in here

您可以在此处通过Google搜索找到此信息

Example for using "pg-promise":

使用“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:

和其他一个例子连接使用“pg”模块的Postgres数据库:

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);