Home > OS >  Unable to connect to postgres DB running in container on wsl 2 from one of the service running on ws
Unable to connect to postgres DB running in container on wsl 2 from one of the service running on ws

Time:02-26

I am unable to connect to postgres DB running in container on wsl 2 from one of the service running on wsl 2.

I can connect to this DB from one another service running on wsl 2 as well as I can connect to this DB from pgAdmin running on windows and another service running on windows.

2022-02-25 20:25:52.975  INFO 2829 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-02-25 20:25:53.207  INFO 2829 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.

But when I try to connect to this DB from another service running on wsl 2 I get

org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I tried using WSL IP, ipv4 of system in place of localhost nothing helps. The difference between 2 services is first is uber jar and second I am trying to run using docker-compose.

CodePudding user response:

In case you are trying to access the DB from localhost, you need to publish the ports of the DB.

In case you are trying t connect from another container, you need to join both container on the same docker network and use the DB container's name as hostname instead of localhost. Or one of its aliases. In case of compose, the container are usually already on the same network, and you can use the compose service name as hostname since all replicas of that service are added to the network(s) under an alias that is the name of the service.

  • Related