エラー内容
「フロントエンド開発のためのテスト入門」 10章で開発環境構築をしていたら以下のエラーが生じた
$ npm run prisma:migrate
> frontend-testing-book-nextjs@1.0.0 prisma:migrate
> prisma migrate dev
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "app-db", schema "public" at "localhost:5432"
Error: P1010: User `root` was denied access on the database `app-db.public`
rootでアクセスできないというその通りのエラーだが、本の通りにはやっていたので記録として残す
エラー解消方法
// 実際にDockerでたてているpostgresにpostgresユーザでログインする
$ psql -h localhost -p 5432 -U postgres
// postgres内でroleを確認すると、rootがない
postgres-# \du
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
osaosa | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser | {}
// rootユーザを以下のコマンドで作成
postgres=# create role root with superuser login password 'password';
// rootユーザが作成されていることを確認
postgres=# \du
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
osaosa | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser | {}
root | Superuser | {}
マイグレーションできるのを確認
$ npm run prisma:migrate
> frontend-testing-book-nextjs@1.0.0 prisma:migrate
> prisma migrate dev
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "app-db", schema "public" at "localhost:5432"
PostgreSQL database app-db created at localhost:5432
Applying migration `20221017012748_init`
The following migration(s) have been applied:
migrations/
└─ 20221017012748_init/
└─ migration.sql
Your database is now in sync with your schema.
✔ Generated Prisma Client (4.6.0 | library) to ./node_modules/@prisma/client in 124ms
Running seed command `ts-node --compiler-options {"module":"CommonJS"} prisma/seed/index.ts` ...
Start seeding ...
Seeding finished.
🌱 The seed command has been executed.
参考