skip to Main Content

I’m having a problem when running the npx prisma migrate dev command. Docker desktop tells me that the database is running correctly on port 5432 but I still can’t see the problem.
I tried to put connect_timeout=300 to the connection string, tried many versions of postgres and docker, but I can’t get it to work.
I leave you the link of the repo and photos so you can see the detail of the code.
I would greatly appreciate your help, since I have been lost for a long time with this.

Repo: https://github.com/gabrielmcreynolds/prisma-vs-typeorm/tree/master/prisma-project
Docker-compose.yml

version: "3.1"
services:
  postgres:
    image: postgres
    container_name: postgresprisma
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=santino2002
    ports:
      - "5432:5432"
    volumes:
      - postgres:/var/lib/postgresql/data

volumes:
  postgres:

Error:

Error: P1001: Can’t reach database server at localhost:5432
Please make sure your database server is running at localhost:5432.

Docker ps show this:
enter image description here

2

Answers


  1. Looks like the application and the database are running on two separate containers. So, in this case, connecting to localhost:5432 from the application container will try to connect to 5432 port within that container and not in the docker host’s localhost.
    To connect to database from the application container, use postgres:5432 (If they are on the same network) or <dockerhost>:5432.

    Login or Signup to reply.
  2. Your docker ps output is showing that your postgres container has no ports connected to your local network.

    It should look something similiar to this on ports column.

    0.0.0.0:5432->5432/tcp, :::5432->5432/tcp
    

    But yours is just 5432/tcp

    You need to open ports for your postgres container.

    Your docker-compose.yml file you posted in the question is correct. Probably you started postgres container with no ports first, then changed your docker-compose.yml file to have ports. So you just need to restart it now.

    Use docker compose down && docker compose up --build -d to do that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search