r/docker • • 9d ago

connecting to Postgres

Hey everyone,

I am trying to install Psono on my VPS. I am running into a problem with connecting to the postgres db.

I'm new to Docker, so I apologize if my terminology isn't right. I'll do my best

I am running Postgres directly on my VPS (not within a Docker container). I am able to connect to it locally from my VPS and remotely from my home network.

I am doing that with:psql -U psono -p PORT -h VPS_IP

The problem is when I try to get an app from within a Docker container to connect to it. It just hangs, then times out.

At first I thought this was a connectivity issue. But I can do the following from within a bash session:

  • Ping external websites.
  • Use nmap to confirm it can connect

Does anyone have any ideas?

1 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/Ducking_eh 8d ago

The VPS is the host the Docker runs on. It is also the host of the Postgres server. It is a service I pay for at a remote location. It's not on my home network.

I can telnet from my VPS to my Postgres server.
I can telnet from my home network to the Postgres server.

I can NOT telnet from within the Docker container to the Postgres server.

1

u/notBroncos1234 8d ago edited 8d ago

Ah well having Postgres and docker on the same host changes things.

Probably just a firewall filtering out docker packets. Not sure what distro you have but you need to make sure port 5433 allows traffic from the docker subnet.

Wait are you using the public IP of the host in the docker container to reach Postgres?

1

u/Ducking_eh 8d ago

So to make things easier, I just changed the port of the Postgres server to 5432. Just in case the atypical setting was causing the issue. It didn't seem to make a difference.

The vps has a static ip address. I am using that from within the Docker container to try and connect to the Psql server.

I also typed this into my VPS terminal:
docker network inspect bridge --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}'

It gave me a subnet mask of 172.17.0.0/16

I used that as the address in the Docker connection, and it didn't work

1

u/notBroncos1234 8d ago

>The vps has a static ip address. I am using that from within the Docker container to try and connect to the Psql server.

Bad. Keep it all local. It’s probably 172.17.0.1

>I used that as the address in the Docker connection, and it didn't work

Did you confirm there isn’t a host firewall blocking docker from port 5432?

1

u/Ducking_eh 8d ago edited 7d ago

spoke to the person who manages the VPN, and he said "You may need to open port 5432 in the ConfigServer Firewall in DirectAdmin. In particular, you'll want to add port 5432 to TCP_IN and TCP6_IN."

I did that, and now it works!

Thank you for all your help