11

I am trying to connect my local postgres database using pgAdmin4 docker container. When I open http://localhost:5050/ and login after create new server connection I got Unable to connect to server: could not connect to server: Connection refused error message.

Here is my docker-compose.yml file

version: '3.5'

services:
  pgadmin:
    container_name: pgadmin4
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: db@db.com
      PGADMIN_DEFAULT_PASSWORD: odoo
    volumes:
       - pgadmin:/root/.pgadmin
    ports:
      - "5050:80"
    restart: unless-stopped

volumes:
    pgadmin:

enter image description here

I am finding solution to connect my local postgres databasw with pgadmin4 docker container. I am using Ubuntu 20.04 os system.

---- Updated base on @Veikko answer -----------

Here is my docker-compose file code https://pastebin.com/VmhZwtaL

and here is postgresql.conf file https://pastebin.com/R7ifFrGR

and pg_hba.conf file https://pastebin.com/yC2zCfBG

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76

6 Answers6

5

You can access your host machine from your docker container with dns name host.docker.internal. Replace localhost in your server connection with this name. The name localhost inside your pgAdmin refers to the docker container itself, not to your host machine.

Veikko
  • 3,372
  • 2
  • 21
  • 31
  • Yes I did but still I can not connect – Jaykumar Patel Jun 24 '20 at 10:58
  • Thanks for updating the questions with OS info. The host.docker.internal is not yet supported in Ubuntu/linux version of Docker. This works for Docker for Mac/Windows from version 18.03. I will post a linux-compatible answer later as a separate answer. – Veikko Jun 25 '20 at 08:37
  • name does not resolve - error for me... – Pfinnn Jul 01 '22 at 15:44
2

You can use image qoomon/docker-host to access services on your host computer. Add docker-host service to your docker-compose like this:

version: '3.5'

services:
  docker-host:
      image: qoomon/docker-host
      cap_add: [ 'NET_ADMIN', 'NET_RAW' ]
      restart: on-failure
  pgadmin:
    container_name: pgadmin4
    image: dpage/pgadmin4
    environment:
...

After this you should be able to access your host Postgres service with host name docker-host. Replace localhost with docker-host in your connection string and connection should work.

If problems with connection after this, please make sure you do not have any firewall blocking the traffic, you have proper Docker network setup (see docs) and your postgresql is listening to this address.

Ubuntu/linux version of Docker does not currently support host.docker.internal DNS name that would point containers to the host. That is the easiest way to link to host in Docker for Mac or Windows. I hope we get this also to Linux soon.

More information about docker-host can be found in Github repo: https://github.com/qoomon/docker-host

Veikko
  • 3,372
  • 2
  • 21
  • 31
  • Hi @Veikko Thank you for your answer. I have try as per you answer I got this error https://ibb.co/Kqq9X1T as well as Here is my docker-compose file code https://pastebin.com/VmhZwtaL and here I shared my postgresql.conf contains https://pastebin.com/R7ifFrGR and pg_hba.conf file contains https://pastebin.com/yC2zCfBG – Jaykumar Patel Jun 26 '20 at 12:13
  • The pgadmin host name in screen capture is the dns name that works only with win/mac (’host.docker.internal’). Please use name ’docker-host’ in pgadmin field ’Host name/address’. It refers to the service name for qoomon:docker-host container specified in docker-compose.yaml. – Veikko Jun 26 '20 at 13:57
  • can you please clerify what should I enter in Host name/address field? – Jaykumar Patel Jun 26 '20 at 16:44
  • Use ’docker-host’ in Host name/address. – Veikko Jun 26 '20 at 18:10
  • still not work Unable to connect to server: could not connect to server: Connection refused Is the server running on host "docker-host" (172.29.0.2) and accepting TCP/IP connections on port 5432? – Jaykumar Patel Jun 29 '20 at 09:07
  • Are you able to connect locally from your host to pg with address 172.29.0.2 without Docker? Your pg_hba.conf configuration looks like it allows only localhost (127.0.0.1). – Veikko Jul 04 '20 at 20:09
1

I have some problem before then now I have find the solution.

Just type this command

docker exec <container_name> cat /etc/hosts

Then it will show this

127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3      607b00c25f29

use 172.18.0.3 as you hostname

Hope this can help you.

0

First call this: sudo netstat -nplt to be sure that someone listening on port 5432.
Than call sudo ip addr to know IP of your host mashine.
Than try connecting using real IP instead of localhost.
I see in your ip addr output:

3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 30:10:b3:e4:39:e2 brd ff:ff:ff:ff:ff:ff inet 192.168.43.190/24 brd 192.168.43.255 scope global dynamic noprefixroute wlp3s0

So your real IP is 192.168.43.190

Alexandr Ivanov
  • 389
  • 1
  • 5
  • 7
  • Where is I find real IP. I have done give both command here is output of both command https://pastebin.com/v91294x0 waiting for your reply thanks – Jaykumar Patel Jun 26 '20 at 16:49
0

Create a server with host name/address 172.17.0.1 and make sure PostgreSQL is listening (*).

install_pgadmin4_using_docker

output

Thirumal
  • 8,280
  • 11
  • 53
  • 103
  • No it not work like that. I have to connect local postgres database using docker pgAdmin4. – Jaykumar Patel Jun 29 '20 at 09:10
  • It works for me, I am using it in Ubuntu 20.04 - PostgreSQL -12 and PgAdmin4.23(Docker). – Thirumal Jun 29 '20 at 09:13
  • thank you for your reply. can you please update here steps which you follow. Thanks – Jaykumar Patel Jun 29 '20 at 09:19
  • I have personal documentation in https://github.com/M-Thirumal/installation_guide/blob/master/pgadmin4/install_pgadmin4_using_docker.md && https://github.com/M-Thirumal/installation_guide/blob/master/PostgreSQL/Install_PostgresQL_in_Ubuntu.md && https://github.com/M-Thirumal/installation_guide/blob/master/pgadmin4/Reverse_Proxying_with_ngnix.md – Thirumal Jun 29 '20 at 09:50
0

you can try to add to your pg_hba.conf

host    all             all             0.0.0.0/0            trust

or

host    all             all             0.0.0.0/0            md5

to test.

Then if this works you should change the 0.0.0.0/0 netmask to your docker bright netmask. And check it again. Btw. to connect to you localhost (on host) you need to connect to docker bright ip for me it's 172.17.0.1/32.

EDIT: Second: postgresql.conf

uncomment and read what there is written:

listen_addresses = '*'     # what IP address(es) to listen on;
                    # comma-separated list of addresses;
                    # defaults to 'localhost'; use '*' for all
                    # (change requires restart)

you need to bind to your IP's that are connectable from docker so in my case it should be 172.17.0.1

Jomaar
  • 84
  • 9