Home > database >  Error: P1001: Can't reach database server at `localhost`:`5432`
Error: P1001: Can't reach database server at `localhost`:`5432`

Time:11-16

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: enter image description here

CodePudding user response:

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.

CodePudding user response:

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.

  • Related