Home  >  Article  >  Backend Development  >  Unable to connect to database Postgres Docker container from other container

Unable to connect to database Postgres Docker container from other container

WBOY
WBOYforward
2024-02-09 10:30:081033browse

无法从其他容器连接到数据库 Postgres Docker 容器

php Editor Youzi encountered a problem when using a Docker container to connect to the Postgres database: unable to connect to the Postgres Docker container from other containers. This issue can cause blockages during development, so a workaround needs to be found. In this article, we will explore the cause of this problem and provide some possible solutions to help solve the problem of not being able to connect to the Postgres Docker container.

Question content

I have a service in a Docker container that syncs data from another database to my database, but the sync container cannot connect to the Postgres DB container.

container

My error message

Cannot connect to the database, exiting now: dial tcp 127.0.0.1:5432: connect: connection refused"

I can connect to the database via TablePlus on 127.0.0.1:5432, but via Docker Inspect is

The sync service is written in Go - the database name, user and password are the same as the one I'm connecting to via TablePlus. I also tried IP 0.0.0.0 and 172.19.0.2.

Does anyone know why it won't connect?

Solution

Try to connect to postgres:5432

You need to use the container name when connecting from another docker container

The above is the detailed content of Unable to connect to database Postgres Docker container from other container. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete