TypeOrmModule Unable to connect to the database (Postgres)

42 Views Asked by At

I'm trying to create a NestJS project without using Docker. However, I'm unable to connect to the PostgreSQL database.

typeorm.config.ts

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

export const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: '123456',
  database: 'book_api',
  entities: [__dirname + '/../**/*.entity.{ts,js}'],
  synchronize: true,
};

app.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BooksModule } from './books/books.module';
import { typeOrmConfig } from './config/typeorm.config';

@Module({
  imports: [TypeOrmModule.forRoot(typeOrmConfig), BooksModule],
})
export class AppModule {}

entity.ts

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Book extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  title: string;

  @Column()
  author: string;

  @Column()
  category: string;

  @Column()
  year: number;
}

Error detail

! ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... error: database "book_api" does not exist at Parser.parseErrorMessage (C:\Users\OS\book-api\node_modules\pg-protocol\src\parser.ts:369:69) at Parser.handlePacket (C:\Users\OS\book-api\node_modules\pg-protocol\src\parser.ts:188:21) at Parser.parse (C:\Users\OS\book-api\node_modules\pg-protocol\src\parser.ts:103:30) at Socket. (C:\Users\OS\book-api\node_modules\pg-protocol\src\index.ts:7:48) at Socket.emit (node:events:514:28) at addChunk (node:internal/streams/readable:376:12) at readableAddChunk (node:internal/streams/readable:349:9) at Socket.Readable.push (node:internal/streams/readable:286:10) at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

0

There are 0 best solutions below