[英]How to connect to postgresql database inside AWS EC2 instance using Node.js and Knex
I rented an EC2 instance of Ubuntu 16.xx on AWS and installed PostgreSQL on it.我在 AWS 上租用了 Ubuntu 16.xx 的 EC2 实例,并在其上安装了 PostgreSQL。 I created a database and table inside the PostgreSQL on EC2.我在 EC2 上的 PostgreSQL 内创建了一个数据库和表。 Right now I am trying to connect to and get data from the database via a local Node.js project using knex
.现在我正在尝试使用knex
通过本地 Node.js 项目连接到数据库并从数据库中获取数据。
I already enabled the inbound rule for port 5432 to IP from anywhere.我已经从任何地方启用了端口 5432 到 IP 的入站规则。
However, it returns error message as below:但是,它返回如下错误消息:
Error: connect ECONNREFUSED 13.229.xxx.xxx:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1142:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '13.229.xxx.xxx',
port: 5432
}
How am I gonna fix it?我要怎么修? Do I need to install and implement a reversed proxy?我需要安装和实施反向代理吗? If so, how do I set it up?如果是这样,我该如何设置? I know there is RDS on AWS, but I need to use EC2 to implement it.我知道 AWS 上有 RDS,但我需要使用 EC2 来实现它。
Here are some of my codes:以下是我的一些代码:
This is the knex
setting, I have pg installed.这是knex
设置,我安装了 pg。 The connection to a local database is successful.与本地数据库的连接成功。 But when I switch the host to whether a public IP/ private IP/ ec2-13-229-xxx-xxx.ap-southeast-1.compute.amazonaws.com.但是当我将主机切换到公共 IP/私有 IP/ec2-13-229-xxx-xxx.ap-southeast-1.compute.amazonaws.com 时。 They all return the above error message.它们都返回上述错误消息。
development: {
client: 'postgresql',
connection: {
host: '13.229.xxx.xxx',
database: 'project2',
user: 'postgres',
password: 'postgres',
port: 5432,
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
This is the Node.js code that I used to connect to the server.这是我用来连接服务器的 Node.js 代码。 A very simple one, just to test the connection.一个很简单的,只是为了测试连接。
const express = require('express');
const hbs = require('hbs');
const app = express();
const knexConfig = require('./knexfile')['development'];
const knex = require('knex')(knexConfig);
let query = knex.select('*').from('users');
query
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
This is my firewall setting which is turned off这是我的防火墙设置已关闭
Also, I paused my Kaspersky.另外,我暂停了我的卡巴斯基。
This is my pg_hba.conf file这是我的 pg_hba.conf 文件
And I am not sure where to add the permission of my personal IP.而且我不确定在哪里添加我个人 IP 的权限。
This issue was related to the pg_hba.conf
being restricted to localhost only.此问题与pg_hba.conf
仅限于本地主机有关。
Additionally the postgres.conf needed to have listen_addresses = '*'
.此外, postgres.conf 需要具有listen_addresses = '*'
。
By whitelisting outside access, it was possible to access the database.通过将外部访问列入白名单,可以访问数据库。
Additional support from this article . 本文的额外支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.