0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【NestJS】Prismaでマイグレーションが実行できないエラー

Posted at

前提

NestJS、PostgreSQL、ORMとしてPrismaを使用しています。 Dockerで環境構築しています。

Prismaでマイグレーションをしようとしたのですが、エラーが発生しました。

エラー

以下コマンドでエラーが発生しました。

npx prisma migrate dev --name init

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "mydb", schema "public" at "localhost:5432"

Error: P1001: Can't reach database server at `localhost`:`5432`

Please make sure your database server is running at `localhost`:`5432`.

原因

docker-compose.ymlとenvファイルの設定が問題でした。
私の場合、コンテナを削除して一から設定し直しました。

再現手順

①それぞれ以下のように記述する
docker-compose.yml
version: '3.1'

services:
  db:
    image: postgres:13-alpine
    container_name: blitz-blog-postgres
    ports:
      - '5432:5432'
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: testdb
    volumes:
      - dbdata:/var/lib/postgresql/data
volumes:
  dbdata:
.env
POSTGRES_PASSWORD="password"
POSTGRES_USER="postgres"

DATABASE_URL=postgresql://admin:password@localhost:5432/testdb

docker-compose up -dを入力

npx prisma migrate dev --name initを入力

マイグレーションが成功

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?