Docker - Postgres and pgAdmin 4 : Connection refused - postgresql

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

Docker-compose container postgresql not available at localhost:5432 [duplicate]

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.

Docker postgres connection from *inside* docker container to host machine's postgres localhost instance on a Mac

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

docker confusion about port

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.

Connecting Robo 3T to Docker MongoDB container

As part as an attempt to resolve a Hibernate OGM connection issue, I want to see if I connect from Robo 3T.
I build my MongoDB image and start it running.
docker ps:
MacBook-Pro:GoStopHandle NOTiFY$ docker ps CONTAINER ID IMAGE
COMMAND CREATED STATUS PORTS
NAMES 0375a68b9988 gostophandlemongodb:latest
"docker-entrypoint.s…" 5 seconds ago Up 4 seconds
0.0.0.0:32844->27017/tcp goStopHandleMongo
The IP address of the containers is:
docker inspect -f '{{range
.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0375a68b9988
172.17.0.2
I've enter the 'mongo' shell:
docker exec -it goStopHandleMongo mongo admin
I add a user & password:
> db.createUser(
... {
... user: "NOTiFY",
... pwd: "MyPassword",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... }
... )
Successfully added user: {
"user" : "NOTiFY",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
I then create a connection in Robo 3T:
I then set-up the 'Authentication':
When I try & connect I get:
Any suggestions?
It wasn't until I had to reboot my Mac that I realised that I'd still had a local instance of MongoDB running, therefore the I wasn't connecting to my Docker instance of MongoDB but my local (Brew installed) instance.
Once I'd seen #Artjom Zabelin answer here and "Don't forget to map port to host port"
docker run --name some-mongo -p 27017:27017 -d mongo
It connected to MongoDB running my container.
#Frank Yucheng Gu was correct that I needed 'localhost'.
To connect, you should simply connect with localhost:27017 on Robo3T.
The problem originates in the confusion between a container's internal IP and its external IP. The internal IP is your container's address in the docker network, which is isolated from you host network (unless explicitly bridged). When you do the docker inspect command, you grabbed the container's IP on the docker network.
Your container is listening on 0.0.0.0:32844 in the docker network, and exposed to the host with a port mapping to port 27017. So if you have another container in the docker network, you should access your service with 172.17.0.2:32844. But if you have anything outside of the docker network, you should access your mongodb with either localhost:27017 or [your host IP]:27017.
Hope this helps!
In my case, I was running docker on Mac OSX and docker was using my host IP address, and as a result docker inspect CONTAINER_ID --format '{{ .NetworkSettings.IPAddress }}' would give me an empty string. Docker host address on Mac OSX and Windows is host.docker.internal. So I was able to connect to the mongodb instance running in docker after setting the host address on robo 3t to host.docker.internal

Cannot connect to Postgres running in Docker from PGAdmin running on Windows Host

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)