2

I pulled pgAdmin4 docker image into my linux debian machine and followed the process specified here to configure the container. I run docker run -p 8000:8000 --env-file ./pgadmin_docker_env.list -d dpage/pgadmin4. For clarity, the pgadmin_docker_env.list specified in the command contains the environmental variables:: PGADMIN_DEFAULT_EMAIL=my_email@example.com PGADMIN_DEFAULT_PASSWORD=my_password. With the container running in detached mode, I run localhost:8080 in my web browser to access pgAdmin 4 in server mode. However, I was unable to create a server connection to the localhost postgres database from inside the pgadmin. I got the following error after input of the connection parameters (shown in the screenshot attached below) enter image description here Unable to connect to server: 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?

UPDATE I used host.docker.internal in place of localhost but I still got an error

Unable to connect to server: could not translate host name "host.docker.internal" to address: Name does not resolve
Ercross
  • 519
  • 6
  • 17
  • Which `localhost` are you trying to connect to? (The host system outside a container; the pgadmin container; a separate PostgreSQL container; my laptop? All of these things individually think they are `localhost`) – David Maze Oct 16 '21 at 15:38
  • The host system localhost – Ercross Oct 16 '21 at 16:35

2 Answers2

2

localhost in this scenario refers to the PgAdmin container, where there is not a Postgres instance running.

You want to connect to Postgres running on the host machine from the container (from what I can tell anyway?) so use host.docker.internal instead of localhost.

clarj
  • 1,001
  • 4
  • 14
2

You can skip a step if you've already done it

  1. Using psql, alter the authentication credential of default postgres user, postgres with the following commands
sudo -u postgres psql
ALTER USER postgres PASSWORD 'newPassword';

Optionally, you can also create a user for your current account as a superuser with CREATE ROLE user_name WITH LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION;

  1. Modify /etc/postgresql/13/main/pg_hba.conf and add host all all 0.0.0.0/0 md5 to the end of the file

  2. Modify the pgadmin_docker_env.list file to include your choice port

PGADMIN_LISTEN_PORT=8000
  1. Stop the previously running container pgadmin docker stop pgadmin and remove the containerdocker rm pgadmin. Then run docker run --env-file ./pgadmin_docker_env.list --network="host" --name pgadmin dpage/pgadmin4 to run the container in host network mode. See more on host network mode

  2. Run localhost:8000 in your web browser and create a server connection using the same connection parameter as in the screenshot.

Ercross
  • 519
  • 6
  • 17
  • (Note that host networking does not work on MacOS or Windows systems; the option is accepted but networking in and out of the container silently fails.) – David Maze Oct 16 '21 at 17:42
  • Yeah, the solution was tested on a Linux Debian machine. – Ercross Nov 04 '21 at 07:16