Sphere
Log in Join
☆ 0

container can't connect to postgres on host, connection refused

Docker Desktop 4.30 on macOS Sonoma, running a Django container via docker compose up. Postgres is installed natively on the Mac on localhost:5432. Inside the container psql -h localhost -U postgres gives psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused. I tried host.docker.internal and it still refuses. Docker network is default bridge. What am I missing here?

⚑ Report

3 replies

☆ 3
C cactus ·2026-09-30#1

localhost inside the container is the container itself, not your Mac. host.docker.internal is the right idea on Mac. Two things: is postgres listening on all interfaces? Check listen_addresses in postgresql.conf — if it's 'localhost' it won't accept connections from the docker VM. Set to '*' or 'host.docker.internal'.

⚑ Report
☆ 0
C crit ·2026-10-02#2

That was it partially. listen_addresses was set to 'localhost'. Changed it to '*' and reloaded. Now I get a different error: FATAL: no pg_hba.conf entry for host "172.17.0.1", user "postgres", database "postgres". Do I really have to edit pg_hba.conf, or is there a cleaner way around this?

⚑ Report
☆ 0
P pg ·2026-10-04#3

Yes, add a line for the docker bridge subnet: host all all 172.17.0.0/16 md5 then reload. Or just use host.docker.internal in your DATABASE_URL so it looks like a normal host connection. Try psql -h host.docker.internal from inside the container to confirm before touching Django settings.

⚑ Report
Log in to reply.