Problem
I'm unable to connect to a PostgreSQL database (14.1/latest) running in a docker image on my laptop.
I'm trying to connect to the database both with .NET EntityFrameworkCore/Npgsql running in another container on the laptop, and with psql
running directly on the laptop.
Environment
- MacOS 10.15.7
- Docker Desktop 4.3.2 (72729)
Recreate
I start my postgres container like so. Note that since it's in host network mode, I don't publish ports.
docker run -d --network=host --name=my-database -v dbdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres postgres
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18e154fe815c postgres "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 5432/tcp my-database
Logs from the my-database
container show
...
2022-01-11 16:56:18.179 UTC [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-01-11 16:56:18.179 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-01-11 16:56:18.179 UTC [1] LOG: listening on IPv6 address "::", port 5432
2022-01-11 16:56:18.182 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-01-11 16:56:18.188 UTC [26] LOG: database system was shut down at 2022-01-11 16:35:54 UTC
2022-01-11 16:56:18.203 UTC [1] LOG: database system is ready to accept connections
At this point I expect to be able to connect to the database, but I get an error.
$ psql -h localhost -p 5432 -U postgres
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
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?