簡體   English   中英

無法連接到數據庫 - NEST.JS 和 TypeORM

[英]Unable to connect to the database - NEST.JS and TypeORM

所以我在使用 typeorm 和 postgres 連接到 nest.js 中的數據庫時遇到問題。 我的本地 postgres 實例在 docker 上運行。 但是當我通過 yarn start 運行應用程序時,它會拋出該錯誤:

[Nest] 12736   - 2020-08-05 10:05:22   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +86ms 
QueryFailedError: no schematic representation of the creation of the object was indicated

我的主要應用程序模塊

@Module({
  imports: [TypeOrmModule.forRoot(configService.getTypeOrmConfig()), UsersModule, LoginModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

我的配置

  public getTypeOrmConfig(): TypeOrmModuleOptions {
    return {
      type: 'postgres',
      host: this.getValue('POSTGRES_HOST'),
      port: parseInt(this.getValue('POSTGRES_PORT')),
      username: this.getValue('POSTGRES_USER'),
      password: this.getValue('POSTGRES_PASSWORD'),
      database: this.getValue('POSTGRES_DATABASE'),
      entities: [__dirname + "/../**/*.entity{.ts,.js}"],
      synchronize: true,
    };
  }

我的環境文件

POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=kacluk123
POSTGRES_DATABASE=my_database
PORT=3000
MODE=DEV

運行 docker/postgres 的腳本

#!/bin/bash
set -e

SERVER="my_database_server";
PW="kacluk123";
DB="my_database";

echo "echo stop & remove old docker [$SERVER] and starting new fresh instance of [$SERVER]"
(docker kill $SERVER || :) && \
  (docker rm $SERVER || :) && \
  docker run --name $SERVER -e POSTGRES_PASSWORD=$PW \
  -e PGPASSWORD=$PW \
  -p 5432:5432 \
  -d postgres

# wait for pg to start
echo "sleep wait for pg-server [$SERVER] to start";
SLEEP 3;

# create the db 
echo "CREATE DATABASE $DB ENCODING 'UTF-8';" | docker exec -i $SERVER psql -U postgres
echo "\l" | docker exec -i $SERVER psql -U postgres

當我在我的 Nestjs 和 Typeorm 項目中遇到這個問題時,我的問題是我的數據庫實際上並沒有在我的 postgres 中創建,所以我需要創建它。 請注意,我的操作系統是 Linux,為此我遵循了這些步驟。

  • sudo su - postgres
  • psql進入 postgres cli 命令
  • CREATE DATABASE databaseName;

注意 postgres 命令末尾的分號,這些在語法中是強制性的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM