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?

📘Frourio プロジェクトで SQLite から PostgreSQL に切り替える手順

Posted at

✅ 1. PostgreSQL の Docker コンテナを準備

プロジェクトルートに compose.yaml を作成:

services:
  db:
    image: postgres:15-alpine 
    container_name: sample-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: sample
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

起動:

docker-compose up -d


✅ 2. .env に接続情報を記述

server/.env またはルートの .env に以下を追記または修正:

DATABASE_URL="postgresql://postgres:postgres@localhost:5432/sample"


✅ 3. Prisma 設定ファイルを修正

server/prisma/schema.prismadatasource を修正:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Task {
  id    Int     @id @default(autoincrement())
  label String
  done  Boolean @default(false)
}

provider = "sqlite""postgresql" に変更すること!


✅ 4. SQLite 用の古いマイグレーションを削除

以下を削除:

rm -rf server/prisma/migrations


✅ 5. PostgreSQL 用マイグレーションを新規作成・適用

cd server
npx prisma migrate dev --name init

成功すると、次のようなログが表示されます:

The following migration(s) have been created and applied from new schema changes:
migrations/
  └─ 20250101000000_init/
    └─ migration.sql


✅ 6. Prisma Client を再生成(必要に応じて)

npx prisma generate


✅ 7. データベースの確認(任意)

docker exec -it sample-db psql -U postgres -d sample

\d "Task";         -- テーブル構造確認
SELECT * FROM "Task";  -- データ内容確認


✅ 8. サーバー起動

npm run dev

Prisma経由でPostgreSQLに接続できれば成功です 🎉


📌 補足

  • 本番運用では .env に書くパスワードの管理方法に注意(.env.gitignore に含めるなど)
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?