Criei o banco de dados no pgAdmin. Ao digitar npm run start aparece o seguinte erro:
[Nest] 920 - 24/01/2024, 13:38:54 ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
error: autentica��o do tipo password falhou para usu�rio "root"
at Parser.parseErrorMessage (C:\Users\anton\angular\loja\node_modules\pg-protocol\src\parser.ts:369:69)
at Parser.handlePacket (C:\Users\anton\angular\loja\node_modules\pg-protocol\src\parser.ts:188:21)
at Parser.parse (C:\Users\anton\angular\loja\node_modules\pg-protocol\src\parser.ts:103:30)
at Socket.<anonymous> (C:\Users\anton\angular\loja\node_modules\pg-protocol\src\index.ts:7:48)
at Socket.emit (node:events:511:28)
at addChunk (node:internal/streams/readable:332:12)
at readableAddChunk (node:internal/streams/readable:305:9)
at Socket.Readable.push (node:internal/streams/readable:242:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
Eis o meu código
docker-compose.yaml
version: '3.5'
services:
postgres:
image: postgres:latest
environment:
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${DB_ADMIN_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${DB_PASSWORD}
ports:
- "8081:80"
depends_on:
- postgres
networks:
- postgres
networks:
postgres:
driver: bridge
volumes:
postgres:
app.module.ts
@Module({
imports: [
UsuarioModule,
ProdutoModule,
ConfigModule.forRoot({
isGlobal: true
}),
TypeOrmModule.forRootAsync({
useClass: DbConfigService,
inject: [DbConfigService]
})
]
})
export class AppModule {}
db.config.services.ts
@Injectable()
export class DbConfigService implements TypeOrmOptionsFactory {
constructor(private configService: ConfigService) {}
createTypeOrmOptions(): TypeOrmModuleOptions | Promise<TypeOrmModuleOptions> {
return {
type: "postgres",
host: this.configService.get<string>('DB_HOST'),
port: this.configService.get<number>('DB_PORT'),
username: this.configService.get<string>('DB_USERNAME'),
password: this.configService.get<string>('DB_PASSWORD'),
database: this.configService.get<string>('DB_NAME'),
entities: [],
synchronize: true
}
}
}
.env
DB_HOST=127.0.0.1
DB_PORT=5432
DB_USERNAME=root
DB_PASSWORD=root
DB_NAME=db_loja
DB_ADMIN_EMAIL=admin@root.com