node.js - 如何使用 NodeJs 连接到 docker compose 中的 postgres?

标签 node.js postgresql docker express

我已经使用 Docker 在 Cloud Server 中安装了 Mainflux。 Postgres 数据库也是 在 Docker 容器中运行。我有一种情况可以使用 Node.Js(以编程方式)连接到 PostgresDB。我发现“pg”模块连接到云端 Postgres 数据库。但是,我无法连接到正在运行的 Postgres docker 容器。我在下面粘贴了我的代码,请让我知道连接“Docker Postgres”的场景。

  const pg = require('pg');
  const conStringPri = postgres://mainflux:mainflux@MYIP/mainflux-things-db;
  const Client = pg.Client;
  const client = new Client({connectionString: conStringPri});
  client.connect();
  client.query(CREATE DATABASE DB_Name)
  .then(() => client.end());

报错如下:

node:8084) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED MYIP:5432 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14) (node:8084) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch() (rejection id: 1) (node:8084) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:8084) UnhandledPromiseRejectionWarning: Error: Connection terminated unexpectedly at Connection.con.once (D:\postgres_Nodejs\node_modules\pg\lib\client.js:200:9) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:106:13) at Connection.emit (events.js:208:7) at Socket. (D:\postgres_Nodejs\node_modules\pg\lib\connection.js:76:10) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at TCP._handle.close [as _onclose] (net.js:561:12)

最佳答案

如果您使用此处提供的 docker-compose 配置启动 Mainflux https://github.com/mainflux/mainflux/blob/master/docker/docker-compose.yml ,那么您的 PostgreSQL 容器没有向主机公开的端口。为了能够连接到数据库,你需要暴露这个端口。

这是 docker-compose 部分的外观示例,things-db 容器的端口 5432(默认 PostgreSQL 端口)暴露

   things-db:                                                                                                                                                                
    image: postgres:10.2-alpine                                                                                                                                             
    container_name: mainflux-things-db                                                                                                                                      
    restart: on-failure                                                                                                                                                     
    environment:                                                                                                                                                            
      POSTGRES_USER: mainflux                                                                                                                                               
      POSTGRES_PASSWORD: mainflux                                                                                                                                           
      POSTGRES_DB: things                                                                                                                                                   
    networks:                                                                                                                                                               
      - mainflux-base-net                                                                                                                                                   
    ports:                                                                                                                                                                  
      - 5432:5432  

所以你需要修改你的docker-compose.yml。 请注意,Mainflux docker compose 在 2 个容器中有 2 个 PostgreSQL 数据库:things-db 和 users-db。

关于node.js - 如何使用 NodeJs 连接到 docker compose 中的 postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53389362/

相关文章:

来自多个身份提供者的 Node.js Passport SAML

postgresql - Postgres 执行计划图片

postgresql - 使用 Distinct : postgres ERROR: could not identify an ordering operator for type record 时出现 PostGres 错误

postgresql - 'Decimal' 和 'Numeric' 在 PostgreSQL 中不可用

node.js - karma 开始引发用户代理错误

node.js - Sails JS - session

node.js - NodeJs - Sequelize - 无法识别的属性数据类型

docker-compose 无法连接到外网

docker - 如何在远程Windows引擎上运行Docker命令

ubuntu - 如何禁用 Docker 内的所有网络连接?