#(beginner) prisma is unable to connect to my database and i dont know why

5 messages · Page 1 of 1 (latest)

covert coral
#

I got this error message when i ran: npx prisma studio:
Can't reach database server at localhost:5432

Please make sure your database server is running at localhost:5432.

what i did already:

  • made sure docker with psql is running with: docker ps
  • validate my database url: DATABASE_URL="postgres://postgres:postgres@localhost:5432/median-db"
  • logged docker:
    postgres-1 | postgres-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization postgres-1 | postgres-1 | 2024-01-18 12:02:48.306 UTC [1] LOG: starting PostgreSQL 13.5 (Debian 13.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit postgres-1 | 2024-01-18 12:02:48.307 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 postgres-1 | 2024-01-18 12:02:48.307 UTC [1] LOG: listening on IPv6 address "::", port 5432 postgres-1 | 2024-01-18 12:02:48.311 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" postgres-1 | 2024-01-18 12:02:48.317 UTC [26] LOG: database system was shut down at 2024-01-18 12:02:46 UTC postgres-1 | 2024-01-18 12:02:48.324 UTC [1] LOG: database system is ready to accept connections
  • restart docker:
    docker compose down and up again
hard pawn
#

Hi @covert coral 👋

Localhost inside docker is bound to the container, not the machine. So you can't access your postgres container via localhost on inside you application docker container. So please use postgres(the container name) instead of localhost as a host in order to connect to your database instance.

covert coral
#

if i do docker ps to get the docker name i get the following:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8320f90386d4 postgres:13.5 "docker-entrypoint.s…" 2 days ago Up 2 hours 0.0.0.0:5432->5432/tcp backend-postgres-1

then I change my db URL to ->
DATABASE_URL="postgres://postgres:postgres@backend-postgres-1:5432/median-db"

covert coral