Search code examples
postgresqldockerdocker-composenestjstypeorm

Docker compose with NestJS PostgreSQL not connecting


I have tried to dockerize NestJS app with PostgreSQL. Postgres is refusing connection and is also showing one log that says database system was shut down at <<some timestamp>>. This is docker-compose.yml and the logs.

version: '3'
services:
  postgres:
    image: postgres
    restart: always
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_DB=gm
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  pgadmin:
    image: dpage/pgadmin4
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=admin
      - PGADMIN_LISTEN_PORT=5050
    ports:
      - "5050:5050"
  api:
    image: gm-server
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/home/node
    ports:
      - '8081:4001'
    depends_on:
      - postgres
    env_file: .env
    command: npm run start:prod
volumes:
  pgdata:
server-postgres-1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
server-postgres-1  | 
server-postgres-1  | 2023-01-04 09:36:45.249 UTC [1] LOG:  starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
server-postgres-1  | 2023-01-04 09:36:45.250 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
server-postgres-1  | 2023-01-04 09:36:45.250 UTC [1] LOG:  listening on IPv6 address "::", port 5432
server-postgres-1  | 2023-01-04 09:36:45.255 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
server-postgres-1  | 2023-01-04 09:36:45.261 UTC [29] LOG:  database system was shut down at 2023-01-04 09:36:27 UTC
server-postgres-1  | 2023-01-04 09:36:45.274 UTC [1] LOG:  database system is ready to accept connections
server-api-1       | 
server-api-1       | > [email protected] start:prod
server-api-1       | > node dist/main
server-api-1       | 
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [NestFactory] Starting Nest application...
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] MulterModule dependencies initialized +61ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] MulterModule dependencies initialized +1ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] AppModule dependencies initialized +1ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM   ERROR [ExceptionHandler] connect ECONNREFUSED 127.0.0.1:5432
server-api-1       | Error: connect ECONNREFUSED 127.0.0.1:5432
server-api-1       |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)
server-api-1 exited with code 1

And I have also tried most of the relevant answers (before Stackoverlow stars marking me as duplicate) and they didn't work. Yes, I have tried changing the host to host.docker.internal as suggested by the previous

For more clarity, here is my typeorm-datasource config in NestJS

import { DataSource } from 'typeorm';

export const typeOrmConnectionDataSource = new DataSource({
  type: 'postgres',
  host: 'host.docker.internal',
  port: 5432,
  username: 'postgres',
  password: 'postgres',
  database: 'gm',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
  logging: true,
  synchronize: false,
  migrationsRun: false,
});

Why this problem is different than the other "duplicate" questions?

The reason this problem is different is due to the reason because

  1. the other threads also don't solve the issue.
  2. even if we consider they do, the solutions didn't work for me.

More evidence?


Solution

  • Apparently the problem lies in NestJS not compiling properly because of my customization to its scripts. Check if that's an issue.

    After fixing this issue, Just follow the instructions to use "postgres" as host, and it will work (if you are facing the same issue).