I’m tynna connect to the database “postgres” but it doesn’t work.
Here is my configurations:
app.module.ts
<code>import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
port: parseInt(configService.get('DB_PORT')),
entities: [],
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
host: configService.get('DB_HOST'),
database: configService.get('DB_NAME'),
synchronize: true,
}),
}),
],
})
export class AppModule {}
</code>
<code>import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
port: parseInt(configService.get('DB_PORT')),
entities: [],
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
host: configService.get('DB_HOST'),
database: configService.get('DB_NAME'),
synchronize: true,
}),
}),
],
})
export class AppModule {}
</code>
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
port: parseInt(configService.get('DB_PORT')),
entities: [],
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
host: configService.get('DB_HOST'),
database: configService.get('DB_NAME'),
synchronize: true,
}),
}),
],
})
export class AppModule {}
.env file:
<code>DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=root
DB_PORT=5433
DB_NAME=postgres
</code>
<code>DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=root
DB_PORT=5433
DB_NAME=postgres
</code>
DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=root
DB_PORT=5433
DB_NAME=postgres
docker-compose file:
<code>version: '3.9'
services:
postgres:
image: postgres:13.3
environment:
POSTGRES_DB: 'postgres'
POSTGRES_USER: 'root'
POSTGRES_PASSWORD: 'root'
ports:
- '5432:5432'
</code>
<code>version: '3.9'
services:
postgres:
image: postgres:13.3
environment:
POSTGRES_DB: 'postgres'
POSTGRES_USER: 'root'
POSTGRES_PASSWORD: 'root'
ports:
- '5432:5432'
</code>
version: '3.9'
services:
postgres:
image: postgres:13.3
environment:
POSTGRES_DB: 'postgres'
POSTGRES_USER: 'root'
POSTGRES_PASSWORD: 'root'
ports:
- '5432:5432'
Start command for container: docker-compose up
Start command for Nestjs: npm run start:dev
There is only 1 main module (app.module.ts)
The whole error:
<code>[Nest] 24888 - 08.05.2024, 18:59:29 ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
AggregateError:
at internalConnectMultiple (node:net:1116:18)
at afterConnectMultiple (node:net:1683:7)
</code>
<code>[Nest] 24888 - 08.05.2024, 18:59:29 ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
AggregateError:
at internalConnectMultiple (node:net:1116:18)
at afterConnectMultiple (node:net:1683:7)
</code>
[Nest] 24888 - 08.05.2024, 18:59:29 ERROR [TypeOrmModule] Unable to connect to the database. Retrying (2)...
AggregateError:
at internalConnectMultiple (node:net:1116:18)
at afterConnectMultiple (node:net:1683:7)
Trying add database to nestJS with official documentation of nestjs.