0

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:
      - PGADMIN_DEFAULT_EMAIL=user@hotmail.com
      - 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

enter image description here

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.

Jagesh Maharjan
  • 866
  • 1
  • 11
  • 24

2 Answers2

1

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:

enter image description here

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
-1

Try creating a network and putting both containers on it:

version: "3"

networks:
  bridge_net:
    driver: bridge

services:
  postgres:
    image: postgres
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - bridge_net

  pgadmin:
    image: dpage/pgadmin4
    ports:
      - 5050:80
    environment:
      - PGADMIN_DEFAULT_EMAIL=user@hotmail.com
      - PGADMIN_DEFAULT_PASSWORD=password
    networks:
      - bridge_net

volumes:
  postgres-data:
    driver: local
Mike Organek
  • 11,647
  • 3
  • 11
  • 26
  • Still the exactly the same problem, after using the network. Earlier I created a network from docker, and ran pgadmin and postgres with docker without docker-compose, yet had the same issue. – Jagesh Maharjan Feb 02 '21 at 08:57
  • @JageshMaharjan When you add a network you need to address the database as `postgres` from PGAdmin instead of `localhost`. – Mike Organek Feb 02 '21 at 09:01
  • Yea, the default database based on Postgres docker-hub, is `postgres` and i named `postgres` for a database. i tried in small aws instance it worked. but in my local machine, it's not working. – Jagesh Maharjan Feb 02 '21 at 09:19
  • This is Inside the posgres container, `$ docker exec -it 062000290d78 bash` `root@062000290d78:/# su - postgres` `postgres@062000290d78:~$ psql` `postgres=# \t` `Tuples only is on.` `postgres=# ` – Jagesh Maharjan Feb 02 '21 at 09:22
  • Compose creates a `default` network for you, identical to the `bridge_net` you propose here but with a different name. [Networking in Compose](https://docs.docker.com/compose/networking/) has more details. – David Maze Feb 02 '21 at 11:55
  • I understand that, docker-compose creates a docker default network. As well we can create manually. But either doesn't solve the above problem. – Jagesh Maharjan Feb 02 '21 at 13:08