1

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:
      - PGADMIN_DEFAULT_EMAIL=admin@gmail.com
      - 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       | > nestjs-app@0.0.1 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?

  • 2
    If the application and database are in the same docker compose stack (like in your example), you can use the alias `postgres` as the host. Try to set the host in the data source to `postgres`. Let me know if it works. – stonith404 Jan 04 '23 at 10:44
  • Thank you for your response. I set the host in datasource to `postgres` and I still have the same error. – Tanishq Daiya Jan 04 '23 at 12:07
  • @generxhd I think it might be a nestjs error, what do yo think? – Tanishq Daiya Jan 05 '23 at 03:14
  • I don't use typorm but it seems like `typeOrmConnectionDataSource` is ignored because as you can see in the logs, typeorm tries to connect to localhost and this is the default value. – stonith404 Jan 05 '23 at 08:46
  • I have also tried to put it into AppModule and it still gets ignored... – Tanishq Daiya Jan 05 '23 at 09:53
  • SOLUTION: Apparently the problem lies in NestJS not compiling properly because of my customization to its scripts. Thanks to @generxhd for your help, After fixing this issue, I followed your instructions to use "postgres" as host, and it worked. – Tanishq Daiya Jan 05 '23 at 10:59

2 Answers2

1

Set the host to 'postgres' in ORM config.

They now converse with hostnames.

Emmanuel
  • 11
  • 1
0

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).