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.