I ran a docker-compose to run Postgres and pgadmin, with the following docker-compose file.
I can login to pgadmin but pgadmin cannot connect to the Postgres. However, Postgres is running smoothly and accepting requests.
docker-compose.yaml
version: "3"
services:
postgres:
image: postgres
ports:
- 5432:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
volumes:
- postgres-data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
ports:
- 5050:80
environment:
- [email protected]
- PGADMIN_DEFAULT_PASSWORD=password
volumes:
postgres-data:
driver: local
This is the error while connecting the Postgres from pgadmin view page.
Unable to connect to server:
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? could not connect to server: Address not available Is the
server running on host "localhost" (::1) and accepting TCP/IP
connections on port 5432?
or
This is the docker-compose log.
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2021-02-02 04:57:59.825 UTC [1] LOG: starting PostgreSQL 12.5 (Debian 12.5-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2021-02-02 04:57:59.825 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2021-02-02 04:57:59.825 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2021-02-02 04:57:59.842 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-02-02 04:57:59.867 UTC [76] LOG: database system was shut down at 2021-02-02 04:57:59 UTC
db_1 | 2021-02-02 04:57:59.877 UTC [1] LOG: database system is ready to accept connections
pgadmin_1 | NOTE: Configuring authentication for SERVER mode.
pgadmin_1 |
pgadmin_1 | [2021-02-02 04:58:10 +0000] [1] [INFO] Starting gunicorn 19.9.0
pgadmin_1 | [2021-02-02 04:58:10 +0000] [1] [INFO] Listening at: http://[::]:5050 (1)
pgadmin_1 | [2021-02-02 04:58:10 +0000] [1] [INFO] Using worker: threads
pgadmin_1 | /usr/local/lib/python3.9/os.py:1023: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
pgadmin_1 | return io.open(fd, *args, **kwargs)
pgadmin_1 | [2021-02-02 04:58:10 +0000] [87] [INFO] Booting worker with pid: 87
pgadmin_1 | ::ffff:192.168.80.1 - - [02/Feb/2021:05:34:52 +0000] "GET /browser/ HTTP/1.1" 302 257 "http://localhost:5050/login?next=%2Fbrowser%2F" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
pgadmin_1 | ::ffff:192.168.80.1 - - [02/Feb/2021:05:34:52 +0000] "GET /login?next=%2Fbrowser%2F HTTP/1.1" 200 1707 "http://localhost:5050/login?next=%2Fbrowser%2F" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
pgadmin_1 | 2021-02-02 05:34:52,568: ERROR flask.app: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
pgadmin_1 | Traceback (most recent call last):
pgadmin_1 | File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1813, in full_dispatch_request
pgadmin_1 | rv = self.dispatch_request()
pgadmin_1 | File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1791, in dispatch_request
pgadmin_1 | self.raise_routing_exception(req)
pgadmin_1 | File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1774, in raise_routing_exception
pgadmin_1 | raise request.routing_exception
pgadmin_1 | File "/usr/local/lib/python3.9/site-packages/flask/ctx.py", line 336, in match_request
pgadmin_1 | self.url_adapter.match(return_rule=True)
pgadmin_1 | File "/usr/local/lib/python3.9/site-packages/werkzeug/routing.py", line 1945, in match
pgadmin_1 | raise NotFound()
pgadmin_1 | werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
I don’t know what could have been gone wrong.
2
Answers
Try creating a network and putting both containers on it:
When inside the cluster, your containers intercommunicate using service name. In your case your service name is
postgres
.The error is saying that you’re trying to set server name as
localhost
. You need to set up the following settings: