The issue is that you are trying to connect to localhost inside the container for DB. The port mapping that you do 5432:5432 for postgres map 5432 to localhost of your host machine.

Now your web container code is running inside the container. And there is nothing on its localhost:5432.

So you need to change your connection details in the config to connect to postgres:5432 and this is because you named the postgres DB service as postgres

Change that and it should work.

Answer from Tarun Lalwani on Stack Overflow
🌐
Reddit
reddit.com › r/docker › help! i have no idea what i'm doing. docker-compose up connection refused
r/docker on Reddit: Help! I have no idea what I'm doing. docker-compose up connection refused
May 28, 2023 -

Full disclosure: I have no experience dealing with ports, networks, containers, or pretty much anything to do with how Docker works way under the hood. I'm a student being brought onto a small startup team to get some hands-on experience. The team is developing a mobile app, which I have no experience in whatsoever. I asked the backend developer how I should get started, and he sent me the GitHub to the backend and with it came some instructions on how to set it up, and I am trying to follow them. In other words, I'm just following bullet points and don't have a true understanding of what I'm doing yet.

I have Docker running on an Ubuntu virtual machine, the .env is all set up, Pipfile.lock exists (not that I understand what those are for yet) and the next step is to run docker-compose up on the project. Here is where I'm getting an error:

psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
Is the server running on that host and accepting TCP/IP connections?

The next line down says: "The above exception was the direct cause of the following exception:" then after the traceback, the exception is:

django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
Is the server running on that host and accepting TCP/IP connections?

I've been determined to try to figure this out on my own as I've already asked the developer for a lot of help already, and I don't want to bother the dude with so much already on his plate. And I kinda want to prove to myself I'm capable of this because now I'm invested, lmao.

I've googled a lot and found this which mentioned changing something in settings.py. However the project doesn't have a settings.py for the whole project (a settings.py does exist however it is in a deeper file for a more specific part of the code so I doubt that's what I need).

I saw a lot of people post their docker-compose.yml files so I guess I'll do that too:

version: "3"

services:
    web:
        build:
            context: .
            target: dev    
            args:
                SECRET_KEY: ${SECRET_KEY}
        env_file: .env
        volumes:
            - ./:/opt/webapp
        ports:
            - "8000:${PORT}"
        depends_on:
            - postgres
            - redis
    postgres:
        image: postgres:12
        environment:
            POSTGRES_USER: postgres
            POSTGRES_PASS: *omitting to be safe bc idk*
        volumes:
            - ./postgres-data/postgres:/var/lib/postgresql/data
        ports:
            - "5432:5432"
    redis:
        image: redis:5
        ports:
            - "6379:6379"

Please help! I would be extremely grateful. Thank you for anyone who reads.

🌐
Stack Exchange
unix.stackexchange.com › questions › 589474 › is-the-server-running-on-host-localhost-1-and-accepting-tcp-ip-connections
docker - Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432 - Unix & Linux Stack Exchange
May 28, 2020 - Couldn't create database for {"host"=>"localhost", "port"=>5432, "adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"development", "pool"=>10, "username"=>"user", "password"=>"secret"} rails aborted! PG::ConnectionBad: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
🌐
Reddit
reddit.com › r/docker › communication between postgres running on host and a web service running inside a container
r/docker on Reddit: Communication between Postgres running on host and a web service running inside a container
May 18, 2020 -

There's a Postgres database service running on my host machine (localhost:5432). I'm running a java web service inside a docker container running on the host. Inside the service when I try to access the db service through the jdbc url (jdbc:postgresql://localhost:5432/postgres), it throws "Connection refused" error.

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.

Any help or direction is very much appreciated.

🌐
PTC Community
community.ptc.com › home › iot & connectivity › thingworx › thingworx developers › docker can't connect to postgresql
Docker can't connect to PostgreSQL | Community
June 9, 2022 - My PostgreSQL server is running and i can even connec to It using pgAdmin4 on those port. Does anyone have a clue? ... Article - "ThingWorx: Setting up a PostgreSQL database for external connections": https://www.ptc.com/en/support/article/CS238699 ... This does not work for me. I tested every solution, even the `host all all all md5' I think there could be a problem in my docker-compose-postgresql.yml
🌐
GitHub
github.com › rancher-sandbox › rancher-desktop › issues › 5535
Unable to connect to postgresql port using docker compose · Issue #5535 · rancher-sandbox/rancher-desktop
September 15, 2023 - connection to server at "localhost" (127.0.0.1), port 5431 failed: Operation timed out Is the server running on that host and accepting TCP/IP connections? I should be able to connect to the container with psql. I switched to docker desktop and that is working, but it only works if i use localhost as the hostname, 127.0.0.1 doesn't work
Author   rancher-sandbox
🌐
codestudy
codestudy.net › blog › docker-is-the-server-running-on-host-localhost-1-and-accepting-tcp-ip-connections-on-port-5432
Docker & Ruby on Rails: How to Fix 'Server Not Running on localhost (::1) Port 5432' PostgreSQL Connection Error — codestudy.net
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?”** This error occurs when your Rails application cannot establish a connection to the PostgreSQL database server.
Find elsewhere
🌐
Docker Community
forums.docker.com › general
I think my postgres container can't accept remote connections - General - Docker Community Forums
March 18, 2022 - Has anybody out there ever have a message like this and if so how was it resolved? sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at “0.0.0.0”, port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?
🌐
Google Groups
groups.google.com › g › sqitch-users › c › ZWBuLgyo-3Y
suddenly unable to connect to postgres/docker
On May 25, 2019, at 19:32, Kevin ... show the ip and port are active: > > $ docker ps > CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES > 567799de6e98 postgres:10-alpine "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp postgresqldb > > $ sqitch deploy --target db:pg://postgres:12...@0.0.0.0/phile_starter --plan-file sqitch.plan > could not connect to server: Connection refused > Is the server running on host "0.0.0.0" and accepting > TCP/IP ...
🌐
GitHub
github.com › saleor › saleor-platform › issues › 102
Is the server running on host "db" (172.21.0.4) and accepting TCP/IP connections on port 5432? · Issue #102 · saleor/saleor-platform
June 4, 2021 - The above exception was the direct ... host Is the server running on host "db" (172.21.0.4) and accepting TCP/IP connections on port 5432? m3@localhost:~/repos/saleor-platform> ... You can’t perform that action at this time....
Author   saleor
🌐
Reddit
reddit.com › r/docker › can docker on linux connect to local ports in host machine or not?
r/docker on Reddit: Can Docker on Linux connect to local ports in host machine or not?
February 4, 2025 -

I've been reading lots of conflicting information on this, most of it lacking context (not saying what software it applies to, or vaguely mentioning that solutions exist bug giving no details).

I have a Linux machine running Ubuntu 22.04 LTS on bare metal. I installed Docker CE following the instruction at Install using the apt repository and the Linux postinstall to allow non-privileged usage.

My project uses Docker Compose to orchestrate a few containers that communicate among themselves using a network with bridge driver. I have access to a remote PostgreSQL database though some AWS CLI command I've been given ("aws ssm ....") that forwards a port in localhost (loopback interface).

My question is whether having access to local ports in the host machine is something that Docker CE supports so, if it doesn't, I don't waste more working hours trying to make it work.

It can certainly connect to actual network interfaces in the host (I use that all the time to connect Xdebug to IDE) but, is loopback supported?

🌐
GitHub
github.com › pydanny › cookiecutter-django › issues › 2767
Local Docker: failed connection to the database · Issue #2767 · cookiecutter/cookiecutter-django
August 25, 2020 - django | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect django | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django | psycopg2.OperationalError: could not connect to server: Connection refused django | Is the server running on host "localhost" (127.0.0.1) and accepting django | TCP/IP connections on port 5432? django | could not connect to server: Cannot assign requested address django | Is the server running on host "localhost" (::1) and accepting django | TCP/IP connections on port 5432? django | django | django | The above
Author   cookiecutter
🌐
Docker Docs
docs.docker.com › manuals › docker desktop › networking on docker desktop
Networking on Docker Desktop | Docker Docs
1 week ago - On the host, Docker Desktop’s backend process receives the traffic and creates standard TCP/IP connections using the same networking APIs as other applications.
🌐
Docker Community
forums.docker.com › general
Access TCP Server on docker container from host or other machines/hardware devices - General - Docker Community Forums
July 18, 2024 - The container is assigned the IP address 10.0.0.100, and the TCP server is running on port 8084 (e.g., 10.0.0.100:8087). A test TCP client running in another Docker container successfully connects to this server.
🌐
ChirpStack Community
forum.chirpstack.io › chirpstack (v4) › setup and configuration
Connecting docker container to a remote database - Setup and configuration - ChirpStack Community Forum
March 12, 2023 - Hello, can you help me please? I have a Chirpstack 4 container. I wish to use the Postgresql database I installed using “apt install…” locally on the machine in a container of a Chirpstack. IP of my machine is 10.0.2.45 and Postgresql is running on ports 5432 and 5433.
🌐
Docker
docs.docker.com › manuals › docker engine › networking › network drivers › host network driver
Host network driver | Docker Docs
November 17, 2025 - This means you can access a server that is running in a container from your host and you can access servers running on your host from any container that is started with host networking enabled.
🌐
Docker
docker.com › blog › how docker desktop networking works under the hood | docker
How Docker Desktop Networking Works Under the Hood | Docker
February 26, 2022 - When containers want to connect to the outside world, they will use TCP/IP. Since Linux containers require a Linux kernel, Docker Desktop includes a helper Linux VM. Traffic from containers therefore originates from the Linux VM rather than ...