简体   繁体   中英

docker pgAdmin4 connection refused while connecting local postgres database

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:

在此处输入图像描述

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

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.

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

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

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

install_pgadmin4_using_docker

输出

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM