Santino Fajardo
Santino Fajardo

Reputation: 51

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

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

Upvotes: 4

Views: 41168

Answers (9)

Shaima Hamila
Shaima Hamila

Reputation: 1

This is my .env

# POSTGRES_HOST=localhost // to run without docker
POSTGRES_HOST=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=tracking-system

DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB}?schema=public"

If you are using docker use : POSTGRES_HOST=postgres if not use POSTGRES_HOST=localhost

I changed it this way and it works

Upvotes: 0

I just solved the problem for my specific circumstance. I was setting up some tooling on my WSL Ubuntu Distro and did some changes that most likely caused the issue. I did a change in the .wslconfig to fix my slow network connection. The culprit seems to have been the networking mode which I had set to mirrored. Unsetting it immediately fixed it.

https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking

Upvotes: 0

Yoann Buzenet
Yoann Buzenet

Reputation: 879

For me it was switching from a previous python project, which had a virtual env (.venv) and prisma was accessing theses cached env var.

I just had to remove the virtual env and access the normal env var.

Upvotes: -1

JejeDev
JejeDev

Reputation: 528

You have to use the container name as HOST in the db url :

postgresql://postgres:postgres@postgres:5432/mydb?schema=public

Then, the connection should work from your app, but not the migrations commands. For this, you have to execute the commands from inside the container like this (considering that your app container is called myApp) :

docker exec myApp npx prisma migrate dev

To make it simpler, you can create a script in your package.json with this command :

// package.json
{
   ...
   "scripts": {
      "prisma-migrate-dev": "docker exec myApp npx prisma migrate dev",
      "prisma-studio": "docker exec myApp npx prisma studio" 
   }
   ...
}

(If you want that the prisma studio work, you have to expose the port 5555 in the docker-compose.)

Then execute the script migration:

yarn prisma-migrate-dev
# OR
npm run prisma-migrate-dev

Upvotes: 8

Mo Iisa
Mo Iisa

Reputation: 504

It appears that you could have updated the DATABASE_URL string in the .env file.

Make sure to:

  1. Save the changes made in .env file
  2. Restart Docker
  3. Run npx prisma db push command.

Upvotes: 0

Tùng Đào
Tùng Đào

Reputation: 1

Plese check file .env DATABASE_URL="postgresql://postgres:postgres@postgres:5432/mydb?schema=public" you fix postgresql://:@:/?schema=

Upvotes: 0

Ashin Thankachan
Ashin Thankachan

Reputation: 158

this is work for me Edit: postgresql.conf

 sudo nano /etc/postgresql/15/main/postgresql.conf

enter image description here

remove # from listen_addresses or add like this

listen_addresses = '*'

then restart the server

sudo service postgresql restart

Upvotes: 0

Mansur Ali Koroglu
Mansur Ali Koroglu

Reputation: 1918

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.

Upvotes: 1

Abhishek S
Abhishek S

Reputation: 637

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.

Upvotes: 4

Related Questions