I think my postgres container can't accept remote connections

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?

(Background on this error at: Error Messages — SQLAlchemy 1.4 Documentation)
(testvenv)

Can you tell us more about how you run the container, on which operating system and when and where you see the error message?

One thing that I noticed: 0.0.0.0 should be an IP address.

Check if any firewall service can block the connection. Ensure the firewall can allow connection to 5432 port.

I guess, In windows python or other will not connect to ‘0.0.0.0’ it will connect to ‘127.0.0.1’ or ‘localhost’ that is for all listners. Also you have make docker postgress to listen global not inside docker only.

It should have something like
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f99bcfb75de5 postgres:latest “docker-entrypoint.s…” 22 minutes ago Up 22 minutes 0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp postgress

Note: Notice PORT is 0.0.0.0:5432->5432/tcp and it is not just 5432, this means it will listen to any connection in window not only inside docker. [It write 0.0.0.0 but is it for localhost in WINDOWS]

My suggestion is to remove the postgress and re pull and re run with under process

If you see postgres container → check PORTS column

:cross_mark: Wrong (common mistake)

5432/tcp

:white_check_mark: Correct

0.0.0.0:5432->5432/tcp

If ports are NOT exposed → FIX

Stop container and re-run:

docker run -d \
  -p 5432:5432 \
  -e POSTGRES_DB=your_db \
  -e POSTGRES_USER=your_user \
  -e POSTGRES_PASSWORD=your_password \
  postgres:18