Open4

prismaのP1010エラーの対処

ねぎぎねぎぎ

環境

Next.jsとprismaでdockerで建てたpostgresqlにnpx prisma migrate dev --name initしようとしたらP1010(user was denied access on the database)エラーが出た。

prisma/shcema.prisma
generator client {
  provider = "prisma-client-js"
  output   = "../app/generated/prisma"
}

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

model Todo {
  id         Int      @id @default(autoincrement())
  title      String
  content    String
  created_at DateTime @default(now())
  updated_at DateTime @default(now())
}
.env
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres"
docker-compose.yml
version: "3.8"

services:
  db:
    image: "postgres:12-alpine"
    restart: always
    environment:
      POSTGRES_DB: postgres
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
    - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - prisma_network

networks:
  prisma_network:

volumes:
  postgres_data:
ねぎぎねぎぎ

エラーの原因

2時間ぐらいの試行錯誤でやっと解決
自分はこの記事に助けられた↓
Prisma database name overwritten with localhost IP

Macbookのローカルで立ち上がってる他のpostgresql5432ポートを上書きしてたみたい。
てなわけで、他のpostgresqlがどこで立ちあがってるか調べる方法を色々模索

lsof -i :5432

ローカルの5432ポートで何が動いてるか調べてみた。

lsof -i :5432
COMMAND     PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres    632 tatsu    7u  IPv6 0x204bd8b8feccd4f2      0t0  TCP localhost:postgresql (LISTEN)
postgres    632 tatsu    8u  IPv4 0x7d2a0559ad762aac      0t0  TCP localhost:postgresql (LISTEN)
com.docke 26215 tatsu  183u  IPv6 0x3470b90ca70cdeec      0t0  TCP *:postgresql (LISTEN)

brewなのか普通にデスクトップアプリケーションだったかどれか分からないけどcom.docker以外に起動してるやつがいる。
おそらくこいつらが5432ポートを使ってるから、今回の認証情報だと通らない。
どのpostgresqlなのか調べようとも思ったけど、とっとと使いたいからPIDからプロセス殺しちゃう。

kill 632

npx prisma migrate dev --name init実行してみたら、なんの問題もなく動いた。

npx prisma migrate dev --name init
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:5432"

Applying migration `20250901032515_init`

The following migration(s) have been created and applied from new schema changes:

prisma/migrations/
  └─ 20250901032515_init/
    └─ migration.sql

Your database is now in sync with your schema.

✔ Generated Prisma Client (v6.15.0) to ./app/generated/prisma in 49ms
ねぎぎねぎぎ

解決までにやってみたこと

  • 新しく別のDBユーザーを作成
  • 新しく別のDBを作成
  • docker volumeの削除
  • npx prisma db pushの実行(普通にP1010エラーがでた)
  • docker container exec -it [コンテナ名] shで中入ってSQLを直で実行
  • 同じく中で/var/lib/postgresql/data/pg_hba.confファイルの編集

そりゃ接続先が上書きされてたら上に書いてあることやっても挙動が変わるわけないよね(泣)