Newbie with docker, I am trying to connect throught localhost my pgAdmin container to the postgres one.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b00555238ba dpage/pgadmin4 "/entrypoint.sh" 43 minutes ago Up 43 minutes 0.0.0.0:80->80/tcp, 443/tcp pedantic_turing
e79fb6440a95 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp pg-docker
I succeed connecting with psql command.
psql -h localhost -U postgres -d postgres
But when I create the server on pgAdmin with the same parameters as psql I got the following error.
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?
I succeed to connect throught the IPAddress given by docker inspect on the container.
By the way, I checked postgresql.conf and assert that listen_addresses = '*' and also that pg_hba.conf contain host all all all md5.
But I don't get it, why shouldn't I be able to use the localhost address ? And why does docker even give me an address that is not local ?
In this case:
Pgadmin fails to connect to localhost, but psql works from outside docker.
both pgadmin & Postgres are running as Containers
Although you haven't indicated if you are doing so, ideally both containers could be part of a custom bridge network for automatic DNS resolution.
If not added explicitly they will be part of the default bridge network.
To find out the networks created in your docker runtime, type:
$ docker network ls
Some networks will be listed in the console, maybe you'll find a [name]_default it should be your network.
Execute
docker network inspect [name]_default
it'll show up a bunch of information, for us the most important is IPv4Address, something like this:
"7c3cd7532ab8aacc70830afb74adad7296d9c8ddd725c498af2d7ee2d2c2aadd": {
"Name": "intime_postegres_1",
"EndpointID": "56a9cb574469f22259497b72719f9f4a3e555b09f95058fcf389ef5287381f28",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
Instead of using localhost for the server name/ip in the pgAdmin new server dialog, connect to the postgres instance's "IPv4Address".
In my case connecting at 172.18.0.2:5432, worked like a charm.
I too had the case when you're able to connect with psql command on terminal but not with pgAdmin4.
The following solution worked for me.
First -
docker ps
From there you'll get the container name/ID of the postgres container, then do -
docker inspect name_of_container_here
It'll give you something like this -
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f35dbe66b36cd38673aad6e1278ad33031ef9d5abd34582d4b91955e288f855e",
"EndpointID": "2e63ea59e9d0de7526bb02ba29bc0b16bcad51b8963127ad380416d15da994db",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
Note the IPAddress and provide that while creating a new server in pgAdmin4 -
I had the same issue and I solved it in a non-canonical way, but very satisfactory to my local workflow.
I always start my postgresql container with a docker-compose.yaml file. So I just added the pgAdmin to the same compose file:
version: "3.7"
services:
my_awesome_db:
image: postgres:latest
ports:
- "5432:5432"
container_name: postgresql-local
volumes:
- "/var/run/postgres.sock:/var/run/postgres/postgres.sock"
- "/home/myuser/docker-apps/volumes/postgres-data:/var/lib/postgresql/data"
pg_admin:
image: dpage/pgadmin4:latest
container_name: pgadmin4
ports:
- "15432:80"
environment:
- GUNICORN_THREADS=1
- PGADMIN_DEFAULT_EMAIL=my_awesome_email#email.com
- PGADMIN_DEFAULT_PASSWORD=does_not_matter
depends_on:
- my_awesome_db
So I access pgAdmin on my localhost:15432 (just because it's easy to remember), and I've configured pgAdmin with:
Host: my_awesome_db
Port: 5432
Username: postgres
For me worked like this:
Add Server -> Connection -> Hostname/address
Set field to 172.17.0.1
More info at Docker Network doc: https://docs.docker.com/network/network-tutorial-standalone
You just need to specify in your connection string the, server parameter, the name of your container name and not the localhost or 127.0.0.1
first you need to get container information
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b00555238ba dpage/pgadmin4 "/entrypoint.sh" 43 minutes ago Up 43 minutes 0.0.0.0:80->80/tcp, 443/tcp pedantic_turing
e79fb6440a95 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp pg-docker
then extract IP Address
$sudo docker inspect e79fb6440a95 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "192.168.64.2",
And when you create your server put your IP Address
Because I was connecting to a server on the hose, it was as simple as adding
--net host
to the docker run command, so that the container gets access to the host's ports.
Related
All the questions on SO about this seem to refer to an opposite case of creating a postgres container and connecting it from Mac host. But I am trying to do the opposite, without success. I have localhost running on my Mac host machine, and despite setting port flags, I cannot get code inside my container to talk to my localhost postgres (talks to remote host postgres just fine).
docker run -it -p 5000:5000 -p 5432:5432 yard-stats
Then inside docker:
telnet 0.0.0.0 5432
Trying 0.0.0.0...
telnet: Unable to connect to remote host: Connection refused
or telnet 127.0.0.1 or localhost. Connection is refused.
Edit: I also tried with flag --network="host", which did not change anything except break inbound connections to the container on localhost:5000 as well.
If you are using docker for mac, you can use use host.docker.internal special DNS name which resolves to the internal IP address used by the host.
You can also use --network="host" with your docker run command to run the container in host network. Then the localhost interface inside the container will be same as localhost interface of the host machine when run in host network. So you should be able to use localhost:5432 to connect to postgresql. You can remove -p option as it has no effect when running with --network="host".
docker run -it --network=host yard-stats
I created a docker postgres container by using a compose file:
postgres:
image: postgres:12-alpine
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"
init.sql:
CREATE USER test WITH PASSWORD '123456';
ALTER USER test WITH SUPERUSER;
CREATE DATABASE test;
I also created a pgAdmin4 container by running:
docker pull dpage/pgadmin4
docker run -p 5050:80 \
-e "PGADMIN_DEFAULT_EMAIL=user#domain.com" \
-e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" \
-d dpage/pgadmin4
when setting up the connection in pgAdmin. I need to set up the server connection to 172.17.0.2 by looking at the postgres container IPAddress: docker container inspect 02e83f5f39d6 | grep IPAddress, using localhost as connection string will fail in pgAdmin4.
However, when I use python sqlalchemy, I could connect to localhost:5432.
import pandas as pd
from sqlalchemy import create_engine, MetaData
eng = create_engine('postgresql://test:123456#localhost:5432/test', echo=True)
meta = MetaData()
meta.reflect(eng, schema='public')
This is confusing. Why pgAdmin didn't recognize the port mapping? Is it because it is running from another container? What should be the correct set up then?
localhost is relative to the container / host.
For the pgAdmin container, localhost is itself. So you need to specify the IP to make pgAdmin point to the correct host.
Similarly for the python script running on your machine, localhost is the host machine (your PC). But since you specified the port mapping 5432:5432 the port on localhost:5432 is forwarded by docker the postgres containers port 5432. So if the script connects to localhost:5432 - it is forwarded to the container by docker.
There is nothing wrong for with this setup. Better would be have the pgAdmin container as part of the same compose file. With that you can utilize the name of the service as a DNS mapping to the container IP for easier management.
Newbie with docker, I am trying to connect throught localhost my pgAdmin container to the postgres one.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b00555238ba dpage/pgadmin4 "/entrypoint.sh" 43 minutes ago Up 43 minutes 0.0.0.0:80->80/tcp, 443/tcp pedantic_turing
e79fb6440a95 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp pg-docker
I succeed connecting with psql command.
psql -h localhost -U postgres -d postgres
But when I create the server on pgAdmin with the same parameters as psql I got the following error.
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?
I succeed to connect throught the IPAddress given by docker inspect on the container.
By the way, I checked postgresql.conf and assert that listen_addresses = '*' and also that pg_hba.conf contain host all all all md5.
But I don't get it, why shouldn't I be able to use the localhost address ? And why does docker even give me an address that is not local ?
In this case:
Pgadmin fails to connect to localhost, but psql works from outside docker.
both pgadmin & Postgres are running as Containers
Although you haven't indicated if you are doing so, ideally both containers could be part of a custom bridge network for automatic DNS resolution.
If not added explicitly they will be part of the default bridge network.
To find out the networks created in your docker runtime, type:
$ docker network ls
Some networks will be listed in the console, maybe you'll find a [name]_default it should be your network.
Execute
docker network inspect [name]_default
it'll show up a bunch of information, for us the most important is IPv4Address, something like this:
"7c3cd7532ab8aacc70830afb74adad7296d9c8ddd725c498af2d7ee2d2c2aadd": {
"Name": "intime_postegres_1",
"EndpointID": "56a9cb574469f22259497b72719f9f4a3e555b09f95058fcf389ef5287381f28",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
Instead of using localhost for the server name/ip in the pgAdmin new server dialog, connect to the postgres instance's "IPv4Address".
In my case connecting at 172.18.0.2:5432, worked like a charm.
I too had the case when you're able to connect with psql command on terminal but not with pgAdmin4.
The following solution worked for me.
First -
docker ps
From there you'll get the container name/ID of the postgres container, then do -
docker inspect name_of_container_here
It'll give you something like this -
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f35dbe66b36cd38673aad6e1278ad33031ef9d5abd34582d4b91955e288f855e",
"EndpointID": "2e63ea59e9d0de7526bb02ba29bc0b16bcad51b8963127ad380416d15da994db",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
Note the IPAddress and provide that while creating a new server in pgAdmin4 -
I had the same issue and I solved it in a non-canonical way, but very satisfactory to my local workflow.
I always start my postgresql container with a docker-compose.yaml file. So I just added the pgAdmin to the same compose file:
version: "3.7"
services:
my_awesome_db:
image: postgres:latest
ports:
- "5432:5432"
container_name: postgresql-local
volumes:
- "/var/run/postgres.sock:/var/run/postgres/postgres.sock"
- "/home/myuser/docker-apps/volumes/postgres-data:/var/lib/postgresql/data"
pg_admin:
image: dpage/pgadmin4:latest
container_name: pgadmin4
ports:
- "15432:80"
environment:
- GUNICORN_THREADS=1
- PGADMIN_DEFAULT_EMAIL=my_awesome_email#email.com
- PGADMIN_DEFAULT_PASSWORD=does_not_matter
depends_on:
- my_awesome_db
So I access pgAdmin on my localhost:15432 (just because it's easy to remember), and I've configured pgAdmin with:
Host: my_awesome_db
Port: 5432
Username: postgres
For me worked like this:
Add Server -> Connection -> Hostname/address
Set field to 172.17.0.1
More info at Docker Network doc: https://docs.docker.com/network/network-tutorial-standalone
You just need to specify in your connection string the, server parameter, the name of your container name and not the localhost or 127.0.0.1
first you need to get container information
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b00555238ba dpage/pgadmin4 "/entrypoint.sh" 43 minutes ago Up 43 minutes 0.0.0.0:80->80/tcp, 443/tcp pedantic_turing
e79fb6440a95 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp pg-docker
then extract IP Address
$sudo docker inspect e79fb6440a95 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "192.168.64.2",
And when you create your server put your IP Address
Because I was connecting to a server on the hose, it was as simple as adding
--net host
to the docker run command, so that the container gets access to the host's ports.
This community is my last resort for this problem, as I have been fighting with this for several hours now.
I have a go app running in one container, in the other container I am running a postgres db. I am able to connect to the postgres db from my go application as long as only my postgres is within a container, and my go app is running locally as usual. However, when my go app is trying to access the postgres from within a docker container i am getting the following error:
dial tcp 127.0.0.1:8080: connect: connection refused
Below I try to provide enough information, but will gladly add more if requested.
I have 2 docker containers running with the following ports:
go application, port info: 8081/tcp -> 0.0.0.0:8081
postgres db, port info: 5432/tcp -> 0.0.0.0:8080
I am running the go app with:
docker run -it --rm --name gographqlserver --link postgresdb:postgres -d -p 8081:8081 gogogopher;
and the postgres db with:
docker run -it --rm --name postgresdb -e POSTGRES_PASSWORD=hello123 -d -p 8080:5432 postgresimage;
both containers can be started without any problems.
I have also tried connecting both containers within a docker network, which did not help.
help would be immensely appreciated!
You are using localhost address within the container which is not the same as your host's address. You should do one of the following instead:
Use your actual host's IP from app's container
Use postgresdb container's IP with the native port (5432). You can discover this IP using docker inspect postgresdb.
Use postgresdb as host name and the native port (5432) when connecting both containers to the same network
I have read all the questions on this, like this one, but it does not help.
Host: Windows 10, running VirtualBox and PgAdmin.
I have setup Docker correctly and run a few containers without issue. Now I tried to setup Postgres. I tried two:
1 https://hub.docker.com/r/paintedfox/postgresql/
2 https://hub.docker.com/_/postgres/
And both have the same issue. When I try to connect from PgAdmin, it says the server does not listen.
When I run docker inspect postgres I see
"NetworkSettings": {
"Bridge": "",
"SandboxID": "6ad76f4d61017c44f814c5ec7ab9081a650d925a46c2b69902c4f0e5209076ce",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5432/tcp": null
},
"SandboxKey": "/var/run/docker/netns/6ad76f4d6101",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "3f14630554c972ac875cbb384725c6970d1d4d5acfba7cbc05e416b5b22f0056",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
I have tried that IP address as the host in PGAdmin.
I have tried setting the container with -p 5432:5432 and connecting as 127.0.0.1:5432.
I have tried setting port forwarding in VirtualBox for the container.
I also tried the machine host, as shown in Kitematic: 192.168.99.100
I finally noticed that in Kitematc in the IP & Ports section on the Home tab that the ACCESS URL was not set.
Under Docker Port it shows '5432/tcp'
So I clicked the IP address next to that and set it and now it shows:
192.168.99.100:32768
I put that IP and port into PGAdmin and it can now connect to the container.
FYI that IP is the same IP returned by:
docker-machine ip default
If you run your container from command-line alter your run-command like this:
docker run -p 32768:5432 my-postgres
Now you can connect to your postgres via <mapped-ip>:32768 (in my case the mapped IP was 192.168.99.100)