#nest app is unable to connect to postgresql

11 messages · Page 1 of 1 (latest)

fickle steppe
#

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

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`
slow belfry
#

Are you using TypeOrmModule.forRootAsync({...configOptions}) in app.module.ts file inside imports section?

fickle steppe
#

No, I am using a bit different configuration. It is a separate module: @Module({ imports: [ ConfigModule.forRoot({ load: [configuration], validationSchema: Joi.object({ POSTGRES_HOST: Joi.string(), POSTGRES_PORT: Joi.number(), POSTGRES_USERNAME: Joi.string(), POSTGRES_PASSWORD: Joi.string(), POSTGRES_DATABASE: Joi.string(), }), }), ], providers: [ConfigService, PostgresConfigService], exports: [ConfigService, PostgresConfigService], })

#

And then I imported it in app.module.ts file

slow belfry
#

TypeOrmModule.forRoot({...}) is required in app.module.ts file

fickle steppe
#

Without TypeOrmModule it works on my colleague's machine but it is not working on my machine

slow belfry
#

can you show your code of app.module.ts file

fickle steppe
#
  imports: [
    ConfigModule.forRoot({
      envFilePath: `${process.cwd()}/${process.env.NODE_ENV}.env`,
      isGlobal: true,
    }),
    AppConfigModule,
    PostgresConfigModule,
    PostgresDatabaseProviderModule,
    AuthModule.forRoot({
      connectionURI: process.env.SUPER_TOKEN_CONNECTION_STRING as string,
      appInfo: {
        appName: process.env.APP_NAME as string,
        apiDomain: process.env.APP_URL as string,
        websiteDomain: process.env.APP_WEBSITE_URL as string,
        apiBasePath: '/auth',
        websiteBasePath: '/auth',
      },
    }),
    ApplicationModule,
    ShareModule,
    FileModifyModule,
  ],
  controllers: [AppController],
  providers: [AppService, ChatGateway],
})```
slow belfry
#

I don't think without TypeOrmModule.forRootAsync() you will be able to connect with your database.

fickle steppe
#

Here it is : @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), ], })

slow belfry
#

I think the problem is like you are not able connect the postgres outside the docker, and I think looking around this would be helpful, just a guess BTW.