환경 변수 파일 만들기

src/config/orm.config.ts

import { ConfigService } from '@nestjs/config';
import { TypeOrmOptionsFactory, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { Injectable } from '@nestjs/common';

@Injectable()
export class typeORMConfig implements TypeOrmOptionsFactory {
  constructor(private configService: ConfigService) {}
  createTypeOrmOptions(): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions> {
    return {
      type: 'postgres',
      host: this.configService.get('DB_HOST'),
      port: this.configService.get<number>('DB_PORT'),
      username: this.configService.get('DB_USER'),
      password: this.configService.get('DB_PASSWORD'),
      database: this.configService.get('DB_DATABASE'),
      entities: [__dirname + '../**/*.entity.{.ts,.js}'],
      ssl: {
        rejectUnauthorized: false,
      },
      synchronize: false, //entity가 변할 때 변한 값을 실제 DB에 반영하겠냐는 조건 (실제 프로덕션 환경에서는 끄는게 좋다.)
      logging: true,
    };
  }
}

app.module.ts

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    TypeOrmModule.forRootAsync({
      //env 파일을 불러오기 위한 import
      imports: [ConfigModule],
      //config 불러오기
      useClass: typeORMConfig,
    }),
    BoardModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})

참조

Documentation | NestJS - A progressive Node.js framework