0

This is the env file code that I am using to run the docker container:

POSTGRES_HOST="localhost"
POSTGRES_PORT=5432
POSTGRES_USERNAME="postgres"
POSTGRES_PASSWORD="123"
POSTGRES_DATABASE="netabe"

This is the docker.compose.yml file:

postgres:
    container_name: postgres_container
    image: 'postgres:latest'
    environment:
      POSTGRES_USER: ${POSTGRES_USERNAME}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DATABASE}
    ports:
      - 5432:5432
    networks:
      app_network:
        ipv4_address: 172.26.0.10
    restart: unless-stopped
    healthcheck:
      test: ['CMD', 'pg_isready -U postgres']
      interval: 5s
      timeout: 5s
      retries: 5
    volumes:
      - postgres:/data/postgres

It runs the postgres in docker container successfully but when I run the nest app then it says [Nest] 13620 - 05/01/2023, 6:56:41 pm ERROR [SequelizeModule] Unable to connect to the database. Retrying (1).... I don't understand why this is happening. My postgres is also up and running and the database is also created in postgres but in the console it is throwing an error. The start:dev command: NODE_ENV=development nest start --watch

The postgresqlProvider is a different module and then I imported this in my app.module.ts file. the postgresqlprovider module code:

@Module({
  imports: [
    SequelizeModule.forRootAsync({
      imports: [PostgresConfigModule],
      useFactory: async (postgresConfigService: PostgresConfigService) => ({
        dialect: 'postgres' as string,
        host: postgresConfigService.host as string,
        port: postgresConfigService.port,
        username: postgresConfigService.username,
        password: postgresConfigService.password,
        database: postgresConfigService.database,
        keepConnectionAlive: true,
        synchronize: true,
        autoLoadModels: true,
      }),
      inject: [PostgresConfigService],
    } as SequelizeModuleAsyncOptions),
  ],
})
export class PostgresDatabaseProviderModule {}

It has nothing to do with the docker however the problem I found is with the synchronize option. Whenever it is false it works just fine and does not throw any error but when it is set to true then it gives the error that I mentioned above. I don't understand why this is happening.

John Oliver
  • 125
  • 1
  • 11
  • 1
    You have set `POSTGRES_HOST` to `localhost`. If that's what you're using as the database hostname in your nest app, it's not going to work; you need to use the hostname `postgres`. – larsks Jan 05 '23 at 16:46
  • Also see [ECONNREFUSED for Postgres on nodeJS with dockers](https://stackoverflow.com/questions/33357567/econnrefused-for-postgres-on-nodejs-with-dockers) if @larsks is correct about the incorrect `POSTGRES_HOST` setting. – David Maze Jan 05 '23 at 17:02
  • If I set the `POSTGRES_HOST` to postgres then it throws this error in the console: `SequelizeHostNotFoundError: getaddrinfo ENOTFOUND postgres` – John Oliver Jan 06 '23 at 04:21

0 Answers0