To allow a pgAdmin Docker container to connect to a PostgreSQL server using the Docker network IP range, you will need to perform the following steps:

1. Identify the Docker Network IP Range

  • First, identify the Docker network IP range. You can find it by inspecting the network your pgAdmin container is attached to:
  docker network inspect <network_name>

For example, if your pgAdmin container is attached to the default bridge network:

  docker network inspect bridge

The output will show the IP range (subnet) in the IPAM section, such as:

  "IPAM": {
      "Config": [
          {
              "Subnet": "172.17.0.0/16"
          }
      ]
  }

2. Configure PostgreSQL to Allow Connections from Docker IP Range

Now that you have the Docker subnet, modify PostgreSQL’s pg_hba.conf to allow connections from this IP range.

  • Open the pg_hba.conf file (the path may vary depending on your system):
  sudo nano /etc/postgresql/{version}/main/pg_hba.conf
  • Add a line for the Docker subnet, for example:
  host    all             all             172.17.0.0/16           md5

This allows any IP from the Docker subnet to connect using password authentication.

3. Restart PostgreSQL

After making changes to pg_hba.conf, restart PostgreSQL to apply the new settings:

sudo systemctl restart postgresql

4. Ensure listen_addresses is Set Correctly

Make sure that PostgreSQL is listening on the correct network interfaces, especially listen_addresses is set to either '*' or the Docker bridge IP.

  • Edit the postgresql.conf file:
  sudo nano /etc/postgresql/{version}/main/postgresql.conf
  • Set listen_addresses to allow connections from Docker:
  listen_addresses = '*'
  • Save the file and restart PostgreSQL again.

5. Set Up pgAdmin Docker Container

When running the pgAdmin container, ensure that it is attached to the same Docker network as the PostgreSQL container (or the host if Postgres is running on the host).

  • Run pgAdmin in Docker and attach it to the correct network:
  docker run -d \
    --name pgadmin \
    --network <network_name> \
    -e PGADMIN_DEFAULT_EMAIL="admin@domain.com" \
    -e PGADMIN_DEFAULT_PASSWORD="admin" \
    -p 8080:80 \
    dpage/pgadmin4

6. Connect pgAdmin to PostgreSQL

  1. Open pgAdmin in your browser by navigating to http://localhost:8080 (or http://<host-ip>:8080 if you’re running Docker remotely).
  2. Log in with the default credentials (email: admin@domain.com, password: admin).
  3. In pgAdmin, add a new server:
    • Host: Use the Docker bridge IP (usually 172.17.0.1 if PostgreSQL is running on the host) or the IP of the PostgreSQL container.
    • Port: 5432
    • Username: PostgreSQL user (e.g., postgres)
    • Password: PostgreSQL user’s password

7. Ensure Docker Container Communication

  • If both pgAdmin and PostgreSQL are running inside Docker containers, ensure they can communicate by running them on the same network, such as:
  docker network create pg-network
  docker run -d --name postgres --network pg-network -e POSTGRES_PASSWORD=mysecretpassword postgres
  docker run -d --name pgadmin --network pg-network -e PGADMIN_DEFAULT_EMAIL=admin@domain.com -e PGADMIN_DEFAULT_PASSWORD=admin -p 8080:80 dpage/pgadmin4

This setup will ensure that both containers can communicate through the Docker network.

Let me know if you need further details!