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 OverflowThe 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.
By default the postgres image is already exposing to 5432 so you can just remove that part in your yml.
Then if you would like to check if web service can connect to your postgres service you can run this docker-compose exec web curl postgres:5432 then it should return:
curl: (52) Empty reply from server
If it cannot connect it will return:
curl: (6) Could not resolve host: postgres or curl: (7) Failed to connect to postgres port 5432: Connection refused
UPDATE:
I know the problem now. It's because you are trying to connect on the localhost you should connect to the postgres service.
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 refusedIs the server running on that host and accepting TCP/IP connections?connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested addressIs 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 refusedIs the server running on that host and accepting TCP/IP connections?connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested addressIs 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.
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.
I was encountering this issue and I think I've found an answer. When I was encountering this, my database environment setup included whitespace between the declarations. Look at the spaces next tot he "=" in this image The fix for it, was eliminating the whitespace. So in this image the whitespace is removed. Try looking through your .yml file and see if there is any unnecessary whitespace and that might be your problem.
Django settings.py -> Database={host:db} . Other database option remain same. Like
DATABASE_ENGINE=django.db.backends.postgresql
DATABASE_USER=postgres
DATABASE_PASSWORD=1234
DATABASE_NAME=pos
DATABASE_HOST=db
DATABASE_PORT=5432
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?