Docker部署thajeztah/pgadmin4连接本地PostgreSQL的IP地址问题
Hi Thomas, great question! When you run pgAdmin4 inside a Docker container, using localhost won’t work—each container has its own isolated network stack, so localhost inside the container points to the container itself, not your Linux Mint host machine. Here’s how to fix this:
Reliable Connection Methods
1. Use Your Host’s Private IP Address
This is the most straightforward and consistent approach:
- Open your Linux Mint terminal and run this command to get your private IP (the one your machine uses on your local network):
You’ll get an output likeip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1192.168.1.42—enter this IP in the Host name/address field when adding your new server in pgAdmin4.
2. Use Docker’s Host Gateway IP (Linux-Specific)
Docker’s default bridge network has a gateway that points to your host machine. By default, this is usually 172.17.0.1, but you can confirm it with:
- First, get your pgAdmin4 container ID:
docker ps - Then check the gateway IP:
Use the resulting IP (e.g.,docker inspect <your-container-id> | grep Gateway172.17.0.1) as the host address in pgAdmin4.
Critical Pre-Checks
Before connecting, make sure PostgreSQL is configured to accept external connections:
- Update
pg_hba.conf(usually located at/etc/postgresql/9.6/main/pg_hba.conf):
Add a line to allow connections from your Docker network or private IP range. For example:# Allow connections from Docker's default bridge network host all all 172.17.0.0/16 md5 # Or allow connections from your local network host all all 192.168.1.0/24 md5 - Update
postgresql.conf:
Setlisten_addressesto*so PostgreSQL listens on all network interfaces:listen_addresses = '*' - Restart PostgreSQL to apply changes:
sudo systemctl restart postgresql
Either of the connection methods above should work for your setup. I’d recommend using your private IP since it’s more stable if your Docker network configuration changes.
内容的提问来源于stack exchange,提问作者ThomasDKim

