I'm using Docker on Windows to use in local a postgres database. I made two docker container: one for the database called pg and another called pgadmin with pgadmin4. After inspected pg, I connected pgadmin4 in 172.17.0.3:5432 and it works. Now time to code but...
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "172.17.0.3" and accepting
TCP/IP connections on port 5432?
I read a lot of questions like that, but i can't figure it out. Doesn't seem, to me, like a problem generated in pg_hba.conf for the denied connection, because in this case pgadmin should not work, right? (But if is a problem with pg_hba.conf where I can find it? In the docker installation folder I did't find it)
I put my python code here but don't seems the problem:
conn = psycopg2.connect(
database = 'example',
user = 'username',
password = 'secretpassword',
host = '172.17.0.3', # default port is 5432 so it's not necessary
)
Upgrade edit: I found the configurations In postgresql.conf the listen_addresses = '*', that is correct. In pg_hba.conf:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
You could use docker run
and the --expose
parameter to expose your ports, on each container.
Another command you can run is docker network connect
which allows you to connect a container to a network, so if two containers are on the same network, they should be able to communicate with each other.
The usage is docker network connect [OPTIONS] NETWORK CONTAINER
One advantage of using the docker network create
command is to be able to create an --ip-range
so to specify ip addresses to be used on the network.
Afterwards you can assign a container to have a specific ip address, docker network connect
. If the container was not on the network for example, the ip address would not be reassigned to another container on the network.
More information in the docker documentation, https://docs.docker.com/engine/reference/commandline/network_connect/
User contributions licensed under CC BY-SA 3.0