node-postgres获取错误连接ECONNREFUSED

时间:2022-11-02 01:02:37

I want to connect my apps nodejs using node-posgres to PostgreSQL. My apps in localhost (ubuntu) and my postgresql in virtual machine in the cloud with operating system OpenSuse. This is my code :

我想使用node-posgres将我的应用程序nodejs连接到PostgreSQL。我在localhost(ubuntu)中的应用程序和我在操作系统OpenSuse的云中的虚拟机中的postgresql。这是我的代码:

var express = require('express');
var app = express();


var http = require('http');
var pg = require('pg');

var conString = "postgres://postgres:mypassword@myurl.com:5432/postgres";


app.get('/', function (req, res) {
    res.send('HOLAAAA');
});

// respond with "SERVER" on the homepage
app.get('/server', function (req, res) {

    var client = new pg.Client(conString);
    client.connect(function (err) {
        if (err) {
            return console.error('could not connect to postgres', err);
        }
        console.log('CONNECT PASSED');
        client.query('SELECT * FROM visit', function (err, result) {
            if (err) {
                return console.error('error running query', err);
            }
            console.log('QUERY PASSED');
            console.log(result.rows[0].theTime);
            //output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
            client.end();
        });
    });

});


var server = app.listen(3000, function () {

    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);

});

But i got an error like this:

但是我得到了这样的错误:

could not connect to postgres { [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }

Please help me how to solve this. Thanks!

请帮我解决这个问题。谢谢!

2 个解决方案

#1


1  

First try a simple psql command from the local end,

首先从本端尝试一个简单的psql命令,

psql -d DBNAME -h YOUR_IP -U USERNAME

it mayn't work for you. This can be because of two reasons, the VM's ip is not resolved by your local station. (or) is the VM in public cloud like amazon (or) your desktop. If it is in public group, the port 5432 on the VM is not open to the public world. You need to write a security group to do it.

它可能不适合你。这可能是由于两个原因,VM的IP无法由您的本地站解决。 (或)是公共云中的虚拟机,如亚马逊(或)您的桌面。如果它在公共组中,则VM上的端口5432不对公共世界开放。您需要编写一个安全组来执行此操作。

If you are sure, it is not any one of the above two issues, better visit postgres /etc/postgresql/9.3/main/pg_hba.conf and see if remote connections are enabled. NodeJs app, you must test at the end.

如果你确定,它不是上述两个问题中的任何一个,最好访问postgres /etc/postgresql/9.3/main/pg_hba.conf并查看是否启用了远程连接。 NodeJs app,你必须在最后测试。

#2


1  

@Madhavan Kumar thank you very much for your help

@Madhavan Kumar非常感谢你的帮助

the steps to resolve this were as follows:

解决这个问题的步骤如下:

On the remote server:-

在远程服务器上: -

1- find \ -name "postgresql.conf" to find place of config file

1-找到\ -name“postgresql.conf”找到配置文件的位置

2- sudo nano /path/to/config/postgresql.conf to edit config file

2- sudo nano /path/to/config/postgresql.conf编辑配置文件

3- change this #listen_addresses = 'localhost' to this listen_addresses = '*' then save and exit

3-将此#listen_addresses ='localhost'更改为此listen_addresses ='*'然后保存并退出

4- find \ -name "pg_hba.conf" to find hba config file

4-找到\ -name“pg_hba.conf”找到hba配置文件

5- sudo nano /path/to/config/pg_hba.conf to edit hba config file

5- sudo nano /path/to/config/pg_hba.conf来编辑hba配置文件

6- add host all all 0.0.0.0/0 md5 host all all ::/0 md5

6-添加主机全部0.0.0.0/0 md5主机全部:: / 0 md5

at the end of the file, then save and exit

在文件的末尾,然后保存并退出

7- run /etc/init.d/postgresql restart to restart postgres

7-运行/etc/init.d/postgresql重启以重启postgres

In the code connect like this:-

在代码中连接如下: -

let sequelize = new Sequelize(
  config.db.name,
  config.db.username,
  config.db.password,
  {
    host: config.ip,
    port: config.port,
    dialect : 'postgres'
  }
)

#1


1  

First try a simple psql command from the local end,

首先从本端尝试一个简单的psql命令,

psql -d DBNAME -h YOUR_IP -U USERNAME

it mayn't work for you. This can be because of two reasons, the VM's ip is not resolved by your local station. (or) is the VM in public cloud like amazon (or) your desktop. If it is in public group, the port 5432 on the VM is not open to the public world. You need to write a security group to do it.

它可能不适合你。这可能是由于两个原因,VM的IP无法由您的本地站解决。 (或)是公共云中的虚拟机,如亚马逊(或)您的桌面。如果它在公共组中,则VM上的端口5432不对公共世界开放。您需要编写一个安全组来执行此操作。

If you are sure, it is not any one of the above two issues, better visit postgres /etc/postgresql/9.3/main/pg_hba.conf and see if remote connections are enabled. NodeJs app, you must test at the end.

如果你确定,它不是上述两个问题中的任何一个,最好访问postgres /etc/postgresql/9.3/main/pg_hba.conf并查看是否启用了远程连接。 NodeJs app,你必须在最后测试。

#2


1  

@Madhavan Kumar thank you very much for your help

@Madhavan Kumar非常感谢你的帮助

the steps to resolve this were as follows:

解决这个问题的步骤如下:

On the remote server:-

在远程服务器上: -

1- find \ -name "postgresql.conf" to find place of config file

1-找到\ -name“postgresql.conf”找到配置文件的位置

2- sudo nano /path/to/config/postgresql.conf to edit config file

2- sudo nano /path/to/config/postgresql.conf编辑配置文件

3- change this #listen_addresses = 'localhost' to this listen_addresses = '*' then save and exit

3-将此#listen_addresses ='localhost'更改为此listen_addresses ='*'然后保存并退出

4- find \ -name "pg_hba.conf" to find hba config file

4-找到\ -name“pg_hba.conf”找到hba配置文件

5- sudo nano /path/to/config/pg_hba.conf to edit hba config file

5- sudo nano /path/to/config/pg_hba.conf来编辑hba配置文件

6- add host all all 0.0.0.0/0 md5 host all all ::/0 md5

6-添加主机全部0.0.0.0/0 md5主机全部:: / 0 md5

at the end of the file, then save and exit

在文件的末尾,然后保存并退出

7- run /etc/init.d/postgresql restart to restart postgres

7-运行/etc/init.d/postgresql重启以重启postgres

In the code connect like this:-

在代码中连接如下: -

let sequelize = new Sequelize(
  config.db.name,
  config.db.username,
  config.db.password,
  {
    host: config.ip,
    port: config.port,
    dialect : 'postgres'
  }
)