node-postgres获取错误连接ECONNREFUSED [英] node-postgres get error connect ECONNREFUSED

查看:141
本文介绍了node-postgres获取错误连接ECONNREFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :

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!

推荐答案

@Madhavan Kumar非常感谢您的帮助

@Madhavan Kumar thank you very much for your help

解决此问题的步骤如下:

the steps to resolve this were as follows:

在远程服务器上:-

1- 查找\-名称 postgresql.conf 查找配置文件的位置

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

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

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

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

4- 查找\ -name pg_hba.conf 查找hba配置文件

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

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

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

6-添加

全部托管0.0.0.0/0 md5
全部托管所有::: 0 md5

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

at the end of the file, then save and exit

7-运行 /etc/init.d/postgresql restart 重新启动postgres

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

在代码中这样连接:-

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

这篇关于node-postgres获取错误连接ECONNREFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆