在docker-compose场景中无法使用npm pg连接到Postgresql容器

弗朗切斯科

我正在使用npm pg"pg": "^7.18.2", "pg-native": "^3.0.0"我的package.json中的)新手将新手node应用程序连接postgresql数据库。我使用docker-compose以下配置来启动所有内容:

version: '3'
services:
  postgres:
    image: 'postgres:latest'
    environment:
      - POSTGRES_USER=postgres_user
      - POSTGRES_PASSWORD=postgres_password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres_user"]
      interval: 10s
      timeout: 5s
      retries: 5
  redis:
    image: 'redis:latest'
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '8000:80'
    depends_on:
      - fibclient
      - fibserver
  fibserver:
    build:
      dockerfile: Dockerfile.dev
      context: ./server
    volumes:
      - './server:/app'
    environment:
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - PGUSER=postgres_user
      - PGPASSWORD=postgres_password
      - PGHOST=postgres
      - PGPORT=5432
      - PGDATABASE=postgres
    depends_on:
      - redis
      - postgres
      - fibworker
    ports:
      - '9229:9229'
  fibclient:
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      - './client:/app'
  fibworker:
    build:
      dockerfile: Dockerfile.dev
      context: ./fibworker
    volumes:
      - './fibworker:/app'
    environment:
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    ports:
      - '9230:9229'
    depends_on:
      - redis

可以想象fibserver,后端应用程序试图连接到postgres托管postgresql数据库的服务。可悲的是,fibserver应用程序在连接到数据库时总是出现此错误:

fibserver_1  | Error: Connection terminated due to connection timeout
fibserver_1  |     at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:255:9)
fibserver_1  |     at Object.onceWrapper (events.js:420:28)
fibserver_1  |     at Connection.emit (events.js:314:20)
fibserver_1  |     at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:78:10)
fibserver_1  |     at Socket.emit (events.js:314:20)
fibserver_1  |     at emitCloseNT (net.js:1657:8)
fibserver_1  |     at processTicksAndRejections (internal/process/task_queues.js:79:21)
fibserver_1  |     at runNextTicks (internal/process/task_queues.js:62:3)
fibserver_1  |     at listOnTimeout (internal/timers.js:520:9)
fibserver_1  |     at processTimers (internal/timers.js:494:7)

docker-compose如果服务可以相互识别(fibserver可以pingpostgres),npm pg配置,文档和示例,并且我觉得一切正常,那么我会仔细检查文件仍然缺少一些东西。是在那里你可以找到整个东西。

更新:我在我的断路器中放了一个简单的断路器fibserver:当他无法连接数据库时,它会等待一段时间,然后重试。fibserverpostgres,即使在10分钟之后也做了很多尝试连接服务的尝试可能是我使用npm pg图书馆导致此问题吗?如果有人想看看,我也会更新主人。谢谢。

更新:我将psql安装到fibserver容器中,并且成功地成功连接到postgres服务,我认为这与我使用pg客户端的方式有关。这是我应该尝试过的东西。

弗朗切斯科

最后有一些时间来解决这个问题。pg在我的fibserver应用程序中卸载了两个模块,然后再次安装pg,为我提供了npm pg模块的新版本(8.3.3),而没有可能错误安装的本机部分。单个javascriptpg库即可完成工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章