postgresql 节点 postgres 得到错误连接 ECONNREFUSED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29712228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node-postgres get error connect ECONNREFUSED
提问by zaldysyah
I want to connect my apps nodejs using node-posgresto 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) 中的应用程序和我在云中虚拟机中的 postgresql 使用操作系统 OpenSuse。这是我的代码:
var express = require('express');
var app = express();
var http = require('http');
var pg = require('pg');
var conString = "postgres://postgres:[email protected]: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!
请帮助我如何解决这个问题。谢谢!
回答by Ahmad Mayo
@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-find \ -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-find \ -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-添加
host all all 0.0.0.0/0 md5
host all all ::/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 restart
以重新启动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'
}
)
回答by Ahmad Mayo
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 应用程序,您必须在最后进行测试。