【问题标题】:AWS: nodejs app can't connect to pg databseAWS:nodejs 应用程序无法连接到 pg 数据库
【发布时间】:2018-07-25 02:57:00
【问题描述】:

我正在尝试在 aws 上在线上传节点应用程序。 当我在本地启动应用程序时,它工作得非常好,因为我的应用程序可以访问 postgres。
但是,当我上传我的服务器时,它就无法连接到数据库。 我的应用使用 loopback.io。
这是 server/config.json :

{
"restApiRoot": "/api",
"host": "0.0.0.0",
"port": 3000,
"remoting": {
  "context": false,
  "rest": {
     "handleErrors": false,
  "normalizeHttpPath": false,
  "xml": false
},
"json": {
  "strict": false,
  "limit": "100kb"
},
"urlencoded": {
  "extended": true,
  "limit": "100kb"
},
"cors": false
},
"legacyExplorer": false,
"logoutSessionsOnSensitiveChanges": true
}

这里是/server/datasources.json

{
"db": {
"name": "db",
"connector": "memory"
},
"postgres": {
"host": "localhost",
"port": 5432,
"url": "",
"database": "postgres",
"password": "postgresseason",
"name": "postgres",
"user": "postgres",
"connector": "postgresql"
}
}

我已经进行了研究,我认为我必须更改一个网址,以便它不会尝试寻找“本地”方式,但无法使其工作。
我尝试使用 url postgress://postgres:postgresseason@db:5432/postgres 没有成功。

我得到的错误是:

Web server listening at: http://0.0.0.0:8080
Browse your REST API at http://0.0.0.0:8080/explorer
Connection fails: Error: getaddrinfo ENOTFOUND db db:5432
It will be retried for the next request.

或者:

Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer
Connection fails: Error: connect ECONNREFUSED 127.0.0.1:5432
It will be retried for the next request.

任何帮助如何使它工作? 谢谢

【问题讨论】:

    标签: node.js postgresql amazon-web-services


    【解决方案1】:

    您需要确保 postgres 服务器已安装且可由 aws 访问。

    默认情况下,它无法访问您本地安装的 postgres(没有复杂的端口转发等...)

    如果您使用的是 ec2,您可以在本地安装 postgres 服务器并使用 localhost。 或者在另一个类似这样的 aws 服务中设置 postgres:https://aws.amazon.com/rds/postgresql/ 只需确保 nodejs 服务器/服务具有访问和查询 postgres 所需的权限。

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 2016-09-27
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2015-05-21
      • 2014-04-24
      相关资源
      最近更新 更多