Peter Boomsma
Peter Boomsma

Reputation: 9808

Prisma cannot authenticate database server

I'm using docker to initiate a postgres db:

version: '3.8'
services:
  postgres:
    image: postgres:13
    restart: always
    environment:
      POSTGRES_USER: db_user
      POSTGRES_PASSWORD: db_password
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - '5432:5432'
volumes:
    postgres:

and in my /.env file I have:

DATABASE_URL="postgresql://db_user:db_password@localhost:5432/college_db?schema=public"

I start docker:

PS C:\Users\alucardu\Documents\projects\**-react> docker-compose up -d
Starting **-react_postgres_1 ... done

Check if the server is running:

PS C:\Users\alucardu\Documents\projects\**-react> docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS          PORTS                                       NAMES
e0f9233ce34b   postgres:13   "docker-entrypoint.s…"   2 minutes ago   Up 33 seconds   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   **-react_postgres_1

But when I run a Prisma migrate I get an authentication error:

PS C:\Users\alucardu\Documents\projects\movieseat-react> npx prisma migrate dev --name "init"
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db": PostgreSQL database "college_db", schema "public" at "localhost:5432"

Error: P1000: Authentication failed against database server at `localhost`, the provided database credentials for `db_user` are not valid.

Please make sure to provide valid database credentials for the database server at `localhost`.

Why is Prisma not matching the set db_user and db_password to the environment variables created in the docker yml?

//edit.

I've added a college_db database and a superuser called db_user and made it owner of the college_db:

enter image description here

But I'm still getting the same error.

Upvotes: 4

Views: 22458

Answers (5)

JejeDev
JejeDev

Reputation: 528

I had the same issue and doing a cleanup on my docker solve the problem for me (Be carreful, this is the hard way and it will erase all docker containers / images / volumes):

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images -q)
docker volume rm $(docker volume ls -q)
docker builder prune

You can also target the containers / volumes to erase instead of removing all of it:

docker stop <container-id>
docker rm <container-id>
docker volume rm <volume-id>
docker builder prune

Upvotes: 0

Marcos Vinicius
Marcos Vinicius

Reputation: 19

If you are still having this problem, check if you have postgres installed on the machine without docker and check if postgres is started on Windows and stop this process and try again

enter image description here

Upvotes: 1

Clement Olaniyan
Clement Olaniyan

Reputation: 423

Try to remove the quote from the port and edit to match below


version: '3.8'
services:
  postgres:
    image: postgres:13
    restart: always
    environment:
      POSTGRES_USER: db_user
      POSTGRES_PASSWORD: db_password
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - 5432:5432
volumes:
    postgres:

OR


ersion: '3.8'
services:
  postgres:
    image: postgres:13
    restart: always
    environment:
      POSTGRES_USER: db_user
      POSTGRES_PASSWORD: db_password
    volumes:
      - postgres:/var/lib/postgresql/data
    expose:
      - 5432
    ports:
      - 5432
volumes:
    postgres:

Upvotes: -1

Peter Boomsma
Peter Boomsma

Reputation: 9808

What-a-mistaka-to-make. I had postgres installed locally on my Windows machine. So it was using that instance of postgres instead of the one on my docker environment. I removed the windows postgres installation and everything is working as expected. https://github.com/prisma/prisma/issues/8927

Upvotes: 13

pbacterio
pbacterio

Reputation: 1152

I think the issue is a database name mismatch.

Try adding POSTGRES_DB: college_db environment variable to your postgres container.

By default the init container uses POSTGRES_USER value for the database name.

Upvotes: 0

Related Questions