postgresql - ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)... error: role "7am-club" d

The database is running in Docker, but I keep getting this error when start the app with the command&

The database is running in Docker, but I keep getting this error when start the app with the command 'npm run start'

Here is my database configuration :

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule,
    TypeOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get<string>('DB_HOST', 'localhost'),
        port: configService.get<number>('DB_PORT', 5432),
        username: configService.get<string>('DB_USER'),
        password: configService.get<string>('DB_PASS'),
        database: configService.get<string>('DB_NAME'),
        synchronize: configService.get<string>('NODE_ENV') !== 'production',
        logging: configService.get<string>('NODE_ENV') === 'development',
        autoLoadEntities: true,
      }),
      inject: [ConfigService],
    }),
  ],
  exports: [TypeOrmModule],
})
export class DatabaseModule {}

And my docker-compose file:

services:
  postgres:
    env_file:
      - .env
    image: postgres
    restart: on-failure
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
      POSTGRES_DB: ${DB_NAME}
    ports:
      - '5432:5432'
    expose:
      - 5432
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U ${DB_USER} -d ${DB_NAME}']
      interval: 10s
      timeout: 5s
      retries: 5
    volumes:
      - db-data:/var/lib/postgresql/data

  7am-club:
    build:
      context: .
    command: npm run start:dev
    environment:
      NODE_ENV: ${NODE_ENV:-development}
    ports:
      - 3000:3000
    depends_on:
      postgres:
        condition: service_healthy

volumes:
  db-data:

And .env example:

DB_HOST=postgres
DB_PORT=5432
DB_USER=test
DB_PASS=test123
DB_NAME=test

Everything is pretty basic as I've been following the Nest documentation, but for some reason it doesn't work and I don't understand why

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744199546a4562814.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信