node.jspostgresqlnestjstypeorm

Even if the user exists, I get error: role "postgress" does not exist


I am trying to run a postgres container and connect my NestJS app to it using TypeORM.

The following is my Dockerfile:

FROM postgres:16
ENV POSTGRES_DB=my-db-name
ENV POSTGRES_USER=postgress
ENV POSTGRES_PASSWORD=mypassword

And the following is my app.module.ts:

import { Module } from '@nestjs/common';
import { CalendarController } from './calendar.controller';
import { CalendarService } from './calendar.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Slot } from './entities/slot.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgress',
      password: 'mypassword',
      database: 'my-db-name',
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
    }),
    TypeOrmModule.forFeature([Slot]),
  ],
  controllers: [CalendarController],
  providers: [CalendarService],
})
export class AppModule {}

But when I run my NestJS app, I get this error:

[Nest] 51550  - 09/25/2024, 4:44:04 PM     LOG [NestFactory] Starting Nest application...
[Nest] 51550  - 09/25/2024, 4:44:04 PM     LOG [InstanceLoader] TypeOrmModule dependencies initialized +50ms
[Nest] 51550  - 09/25/2024, 4:44:04 PM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
error: role "postgress" does not exist
    at Parser.parseErrorMessage (/Users/mohsen/enpal/my-app/node_modules/pg-protocol/src/parser.ts:368:69)
    at Parser.handlePacket (/Users/mohsen/enpal/my-app/node_modules/pg-protocol/src/parser.ts:187:21)
    at Parser.parse (/Users/mohsen/enpal/my-app/node_modules/pg-protocol/src/parser.ts:102:30)
    at Socket.<anonymous> (/Users/mohsen/enpal/my-app/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:191:23)

I have also run \du command from terminal within my postresql container to list the list of users and its output is as below:

coding-challenge=# \du
                             List of roles
 Role name |                         Attributes                         
-----------+------------------------------------------------------------
 postgress | Superuser, Create role, Create DB, Replication, Bypass RLS

It seems to me everything is correct but my NestJS app says the above error.


Solution

  • Problem was that I had already a PostgreSQL running on my system. I stopped it.