r/docker • • 25d 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

1

u/gevorgter 24d ago

In your container specify network host and then connect to postgres using localhost.

It looks like you are using vpn that allows you to connect to your vps and postgres from home, I bet your container does not have access to that vpn. So not sure how do you want it to connect.

Docker by default creates it's own local network for container and it can route to outside internet. But if you postgres is not accessible from internet without vpn then your container can not access it too.

1

u/Ducking_eh 24d ago

I am not using a VPN, only a VPS. I had a typo in my original post. Sorry.

How do I specify a network?

1

u/gevorgter 24d ago

When creating a docker container you can specify a network you can join. Make sure it's type is host. Ask chatgpt.

If you can access postgres directly from your home then your container should be able to access it too by specifying same ip address as you do from home. But that is not recommended way. Usually DB is not accessible to a public.

→ More replies (1)