I am running two postgres servers as containers from docker-compose on different ports. In pgAdmin, only standard port mapping 5432:5432 is possible to connect. The second server mapped as 5433:5432 to avoid conflict is impossible to connect. Both server containers are running in separate internal network (intentionally), but both servers + pgAdmin are in the common external network. Other than that, all settings are identical.
docker-compose #1:
version: '3.7'
services:
postgres:
container_name: 'postgres'
image: postgres
volumes:
- ../var/pgdata:/var/lib/postgresql/data
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_PASSWORD=password
- POSTGRES_DB=firstdb
networks:
- netone
ports:
- '5432:5432'
networks:
netone:
external:
name: workspace
volumes:
postgres:
docker-compose #2
version: '3.7'
services:
test-postgres:
container_name: 'test-postgres'
image: postgres
volumes:
- /var/pgdata:/var/lib/postgresql/data
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_PASSWORD=password
- POSTGRES_DB=testdb
networks:
- test-network
ports:
- '5433:5432'
networks:
test-network:
external:
name: workspace
volumes:
postgres:
Both containers starts with success and running.
When I am trying to connect to the latter server, I am getting this in pgAdmin:
I have tried to change host to localhost, other ports, change db name, register under different servers groups, start both servers before pgadmin, etc. – no success. I am able to run interactive shell inside "problematic" server container and access postgres without any problem.
Running on Ubuntu. Appreciate any help.
2
Answers
After another several hours of trying to resolve this problem, I would close it as a definitive pgAdmin problem.
The thing is, that there is no problem in compose definition as well as there is no problem with neither of those two postgres servers running properly on different ports.
As I am able to work with both servers via interactive shell/psql and additionally, I am able to connect to the 5433 server via another client, like Pycharm:
the problem seems to exist only in pgAdmin when we want to connect two servers on different ports. Even change of the compose setup and connection of both servers + pgAdmin on the same internal network did not help:
(host/user etc. settings are slightly changed in above pictures against the original description as I was playing a lot with that. Additionally, "Maintenance database" in pgAdmin settings is not the same thing as "Database" in PyCharm settings of course, if somebody would want to bring up the db name difference. But I can assure you I have tried all variations just t be sure).
Conclusion: Do not use two postgres server with single pgAdmin client.
Both databases are running on port 5432, just in different containers.
Your images says you are trying to connect to port 5433 of host ‘postgres’. But that is not where your test database is running–neither the host nor the port. You would need to specify the correct port 5432 on the correct host ‘test-postgres’.
On the docker host computer, you could also specify 5433 of ‘localhost’ which would get redirected to 5432 of ‘test-postgres’. Or on a container in the network you could specify 5433 of host 172.17.0.1, which would go out to the host then get redirected back into the correct host of the network on the correct port.