node.js - 无法连接到 EC2 上的 Postgres : ECONNREFUSED

标签 node.js postgresql amazon-web-services amazon-ec2 sequelize.js

我有一个托管在 EC2 实例上的 Node 服务器,该服务器正在尝试连接到在同一实例上运行的 Postgres。当我启动服务器时,我收到数据库的 ECONNREFUSED 错误:

Unable to connect to the database: SequelizeConnectionRefusedError: connect ECONNREFUSED x.x.x.x:5432

我正在使用 NPM 的 Sequelize ORM 包:

import Sequelize from 'sequelize';
const db = new Sequelize('postgres://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70001f0304170215033045425e495e4143465e4543" rel="noreferrer noopener nofollow">[email protected]</a>/unloadx' {dialect: 'postgres'});

Postgres 正在实例上运行,并且我在入站安全规则中打开了端口 5432。我可以确认 postgres 正在运行 ps aux | grep postgres其中显示了多个 postgres 相关进程,以及 netstat -anp --tcp确认 postgres 似乎正在监听该端口:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp6       0      0 ::1:5432                :::*                    LISTEN      -

但是当我用 nmap x.x.x.x -p 5432 扫描端口时显示端口状态为关闭。根据 nmap,这意味着该端口可访问,但没有应用程序监听该端口并能够响应数据包。

感谢任何帮助使连接正常工作的帮助。

最佳答案

一个问题是您正在使用公共(public) IP 地址。这意味着您的数据库连接基本上是与互联网连接并返回。这将阻止安全组规则发挥作用,并引入安全和延迟问题。

如果您尝试从 VPC 中的另一台服务器进行连接,您可能希望使用私有(private) IP 进行连接。但是,由于它位于同一实例上,您可以删除安全组规则并仅使用 localhost127.0.0.1

关于node.js - 无法连接到 EC2 上的 Postgres : ECONNREFUSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38837030/

相关文章:

javascript - 使用 Node 从 CL 创建存储库时出现 Github API 错误,JSON 解析错误

jquery - 使用 Node.js 和 Cheerio 来预处理 HTML

postgresql - 无法在 PostgreSQL 中创建名为 'user' 的数据库表

amazon-web-services - 无法访问AWS EC2上的8080端口

amazon-web-services - 使用 CloudFormation 模板中的现有角色

javascript - ChaiJS 应该 - 测试空字符串

node.js - 多次登录的 Web 测试

postgresql - Postgres 丢弃类型 XX000 "cache lookup failed for type"

python - 无法在 centos 7 上安装 psycopg2

python - 如何在 aws 中使用 boto3 创建不具有公共(public)访问权限的存储桶和对象?