Connection to server at "127.0.0.1", port 5432 failed: Connection refused

3 years ago by sunwoo1524 to c/docker

FastAPI container fails to connect to PostgreSQL.
However, the PostgreSQL container is running well and accessible.
I don't know why this problem is happening.
Here is gist link of Dockerfile, docker-compose.yaml and log when docker-compose is run. https://gist.github.com/...

load all comments
superpants 1 point 3 years ago

The python container should connect on postgres:5432 not localhost. To the web container, localhost is itself, not the physical host running docker

path: 0 1807505, hotness: undefined, score: 1, children: 3
sunwoo1524 1 point 3 years ago

I changed host to postgres:5432, but python made an error.

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "postgres" to address: Temporary failure in name resolution
path: 0 1807505 1812853, hotness: undefined, score: 1, children: 2
superpants 2 points 3 years ago

Sorry, I misread your compose file. It should be the name of the PG container, so db:5432

path: 0 1807505 1812853 1819783, hotness: undefined, score: 2, children: 1
sunwoo1524 1 point 3 years ago

Thanks!
I also added

depends_on:
      - db

to docker-compose.yaml and fixed the issue!

path: 0 1807505 1812853 1819783 1836266, hotness: undefined, score: 1, children: 0
docker
docker

@lemmy.world

login for more options
597
25
1

go to feed...