Reputation: 39
I am having trouble connecting to my database created from a Docker container. I'm getting this error Error: P1001: Can't reach database, and I assume it has something to do with the fact that the database is in the container while Prisma is on my machine.
My docker-compose file:
version: '3'
services:
db:
image: 'postgres:latest'
container_name: fynansus-db
env_file:
- .env
pgadmin:
image: 'dpage/pgadmin4'
container_name: fynansus-pgadmin
env_file:
- .env
ports:
- '8081:80'
depends_on:
- db
pdadmin works correctly and can connect with db.
and my .env
POSTGRES_PASSWORD=password
POSTGRES_USER=root
POSTGRES_DB=fynansus
DATABASE_URL="postgresql://root:password@db:5432/fynansus"
[email protected]
PGADMIN_DEFAULT_PASSWORD=123
I tried change the host of the url to ( localhost, db, fynansus-db, container-ip), but none of them work, i also tried to expose the port 5432 but dont work too.
Upvotes: 0
Views: 322
Reputation: 8490
If you want to use Prisma from your host machine (for development I suppose), you will have to set the "ports" section of the docker-compose service. Then you will have to use "localhost" in your DATABASE_URL.
services:
db:
image: 'postgres:latest'
container_name: fynansus-db
ports:
- ${POSTGRES_PORT-5432}:5432
env_file:
- .env
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Before you try to connect with Prisma, make sure everything is running (docker compose logs) and that you can connect with psql.
Upvotes: 0