0

I am using npm pg ("pg": "^7.18.2", "pg-native": "^3.0.0" in my package.json), at which I am new, to connect my newbie node app to a postgresql database. I use docker-compose to start up everything using this configuration:

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

As you can imagine fibserver is the backend app trying to connect to postgres service which is hosting the postgresql database. Sadly, fibserver app is always getting this error while connecting to the database:

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)

I double-checked my docker-compose file, if services are aware of each other (fibserver can ping postgres), npm pg configuration, docs and examples, and everything looks ok to me. Still missing something. This is where you can find the whole stuff.

UPDATE: I put a simple circuit-breaker in my fibserver: when he fails to connect to database it wait for a while and tries again. Well, fibserver did a lot of attempts to connect do postgres service, even after 10 minutes. Could be my usage of npm pg library to cause this issue? I also update the master if anyone wants to have a look. Thankyou.

UPDATE: I installed psql into fibserver container and I managed to connect to postgres service succesfully, I think it's a matter of how I am using the pg client. This was something I should have tried before.

Francesco
  • 1,742
  • 5
  • 44
  • 78
  • try to use compose v2 and use this https://docs.docker.com/compose/compose-file/compose-file-v2/#depends_on maybe it takes time for postgress to initialize. if the depends_on doesn't work try with a health check like here https://github.com/peter-evans/docker-compose-healthcheck – Eitank Sep 19 '20 at 12:12
  • @Eitank thanks for the tip, I'll try it a.s.a.p! But, shouldn't postgres service came up before fibserver because of depends-on attribute in docker-compose file? – Francesco Sep 19 '20 at 12:51
  • `depends_on:` causes the database container to start starting before the application container, but doesn't wait for it to be fully up. [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) discusses this in more detail. – David Maze Sep 19 '20 at 13:05
  • @Francesco try the health check – Eitank Sep 19 '20 at 16:50
  • @Eitank I tried the healthcheck in a 2.1 docker-compose but no luck. – Francesco Sep 22 '20 at 20:12

1 Answers1

0

Finally having some time to go further this issue. I uninstall the two pg modules in my fibserver application and install pg again provides me a new version of npm pg module (8.3.3) without having the native part which I probably installed by mistake. Single javascript pg library does the job.

Francesco
  • 1,742
  • 5
  • 44
  • 78