この記事はプログラミング学習者がアプリ開発中に躓いた内容を備忘録として記事におこしたものです。内容に不備などあればご指摘頂けると助かります。
0.前提条件
今回の記事は下記の動画を使って学習中に遭遇したエラーの解決方法です。
【DB操作の革命】Prisma完全攻略
1.エラー内容
Next.jsのprismaを使ってアプリを実装しようとしている時に下記のエラーが発生した。
prismaをインストールして、prismaのデータベースとdockerのデータベースをpostgresqlとして繋いだ後にマイグレーションコマンドを実行した時に生じたエラーです。
$ npx prisma migrate dev --name init
(node:30034) ExperimentalWarning: CommonJS module /Users/name/.nodebrew/node/v23.3.0/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /Users/name/.nodebrew/node/v23.3.0/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Loaded Prisma config from prisma.config.ts.
Prisma config detected, skipping environment variable loading.
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "db", schema "public" at "localhost:5432"
Error: P1010: User was denied access on the database `(not available)`
Dockerでpostgresqlを起動しているが、他にlocalでもpostgresqlが起動しており、どちらも5432ポートを指定しているので競合することで上記のエラーが発生していたもよう。
以下は5432ポートを使っているアプリケーションのリスト
$ lsof -i :5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 823 name 154u IPv6 0x76fa1429e60e691 0t0 TCP *:postgresql (LISTEN)
postgres 33483 name 7u IPv6 0x9d2a1ab069ee46fd 0t0 TCP localhost:postgresql (LISTEN)
postgres 33483 name 8u IPv4 0xa07df2703b65aae8 0t0 TCP localhost:postgresql (LISTEN)
Web上の記事を参考にして、ローカルで起動しているpostgresqlを停止させれば良いと分かったので、PIDを使ったkillコマンドで停止しようと試みたが、消しても直ぐに復活してくることが判明。
どうやらpostgresqlが自動継続するようになっているみたいだった。
2. 解決策
視点を変えて、homebrew版のpostgresqlの起動状態を確認したところ、下記のリストが出力された。
$ brew services list
Name Status User File
postgresql@14 started name ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist
redis none
停止コマンドを実行して、完全に停止したことを確認。
$ brew services stop postgresql
Stopping `postgresql@14`... (might take a while)
==> Successfully stopped `postgresql@14` (label: homebrew.mxcl.postgresql@14)
$ brew services list
Name Status User File
postgresql@14 none
redis none
再度、npx prisma migrate dev --name initコマンドを実行したところ、今度はマイグレーションが成功しました。
今回はエラー解決のページへ早々に辿り着いたことで、直ぐに解決するかなぁと思ったらもう一工夫必要だったので、記事として残すことにしました。
最後まで読んで頂き、ありがとうございます。
3. 参考にしたページ
prismaのP1010エラーの対処
PostgreSQLでprisma migrateがError: P1010: User username was denied access on the databaseで失敗する