Dnyaneshwar Shinde
Dnyaneshwar Shinde

Reputation: 11

Encountering P1000 Error: Authentication Failure with Dockerized PostgreSQL Database in Prisma Migration

I've configured my Dockerfile with all the necessary credentials, and my Docker container is running smoothly. However, when I execute npx prisma migrate dev --name create_user_table, I encounter the following error: Error

Here is my docker compose.yml

version: '3.4'

services:
  postgres:
    container_name: threads-db
    image: postgres
    ports:
      - "5432:5432" #default port of postgres:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data #store the data from this add to volume postgres data such that upon shutting off, the data is still stored in our local machine
    environment:
       POSTGRES_USER: postgres #Use this in DATABASE_URL-user in .env
       POSTGRES_DB: threads #db name
       POSTGRES_PASSWORD: postgres #do not use @ -password


volumes:
  postgres_data:

Here is my schema.prisma

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model User{
  id String @id @default(uuid())
  firstName String @map("first_name")  //at code level-firstName, in db-first_name ->mapping is not compulsory.
  lastName String @map("last_name")
  proflieImgURL String @map("profile_img_url")
  email String @unique  //will take email as it is for db
  password String
  salt String


  @@map("users")  //will make the table as users
}

Here is my .env-

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

I'm attempting to migrate the Prisma schema into a PostgreSQL database running within a Docker container. Despite verifying all the fields, I'm still encountering an error.

I'm puzzled as to where I might have gone wrong. Could anyone lend a hand in pinpointing the issue? Any help would be greatly appreciated!

Upvotes: 0

Views: 312

Answers (3)

mansoor.khan
mansoor.khan

Reputation: 2616

I also ran into the same issue after following a tutorial to setup postgres and running migrations with prisma. This is how my docker-compose.yml file looks like.

version: '3.8'
services:
  dev-db:
    image: postgres:17
    ports:
      - 5434:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: posgtres
      POSTGRES_DB: nest
    networks:
      - my-network
networks:
  my-network:

and my I had a DATABASE_URL set to DATABASE_URL="postgresql://postgres:postgres@localhost:5434/nest?schema=public" in .env file. I was running this command: docker compose up dev-db -d but it threw the same authentication error, where dev-db is the service name.

I changed the command to docker compose up -d dev-db and ran npx prisma migrate dev it worked like a charm. The expectation is to add the -d flag immediately after the up command.

In Docker means detached mode (enabled by the -d flag) runs your containers in the background rather than attaching them to your terminal session.

Upvotes: 0

tushar shah
tushar shah

Reputation: 1

I was also facing the same issue but I fixed the issue by removing Postgres locally from my windows machine.

This also might help if you have installed postgresSQL locally

Upvotes: 0

molecule
molecule

Reputation: 140

  1. did you already run fist migration and initialised database:
npx prisma migrate dev --name init
  1. Your .env file should look something like this:
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/threads?schema=public"

so try passing ?schema=public

For the docker, you can check this repo. It runs pretty smoothly: https://github.com/tomanagle/awesome-docker-compose/blob/main/postgres/docker-compose.yml

Upvotes: 0

Related Questions