Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting PostgreSQL installed in docker inside Hyper-V Ubuntu from Windows 10 PgAdmin

I need help in connecting PostgreSQL which is installed in Docker inside HyperV ubuntu 18.4 from Windows 10 PgAdmin. So far I tried the following

Step 1: Install Postgres in Docker (Ubuntu running on Hyper-V)

sudo docker run -p 5432:5432 --name pg_test -e POSTGRES_PASSWORD=admin -d postgres

Step 2: Create a database

docker exec -it pg_test bash
psql -U postgres
create database mytestdb

Step 3: Get the ip address

sudo docker inspect pg_test | grep IPAddress
//returned with 172.17.0.2

Step 4: pg_hba.conf

host    all             all             0.0.0.0/0               md5

Step 5: When I try to connect from Windows PgAdmin 4, I get this below error - Note: I have also tried using UBUNTU VM IP address, but no luck enter image description here

like image 699
Krishna Varma Avatar asked Dec 31 '22 08:12

Krishna Varma


2 Answers

Your's is a case where you are trying to connect to postgres from another subnet, i.e windows subnet to hyper visor subnet if you are not using bridged protocol.

So case 1:

  1. If this is on NAT\HOST and not on bridge then you need to make sure you are able to ping the ubuntu server from windows server.
  2. next is make sure that port is open from ubuntu's end. How do you check that, do a telnet on the port number from windows cmd prompt.

    telnet 192.168.0.10 5432

if you are bridged and you can ping ping the server as well, checked that port is opened which is telnet works. You need to make sure that in the postgres.conf file "listen address" is to "*". which is all.

Again from OS level in ubuntu run the command systemctl stop firewalld to stop firewall and then try to connect. IF this works then you need to open the port in the firewall using this command:

firewall-cmd --permanent --add-port 5432/tcp

I can see from you docker image that 5432 is already opened. This is more of port mapping and firewalld stuff.

like image 185
Raj Verma Avatar answered Jan 02 '23 22:01

Raj Verma


You may want to check that pg_hba.conf is not restricted to local. It should not be the case for docker image but you never know.

See: https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html

Also, there is a typo: POSTGRES_PASSWOR=admin is missing D, it should be POSTGRES_PASSWORD=admin.

like image 40
johnymachine Avatar answered Jan 02 '23 22:01

johnymachine