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: [email protected]
      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

4
  • Where is your DB running and what's the address? pgadmin doesn't contain a db process... Commented Jun 18, 2020 at 7:25
  • database running in local 5432 port. I want to access my database in pgadmin4. Commented Jun 18, 2020 at 7:45
  • Where do you run the DB? on the machine hosting the container or in another container? Commented Jun 18, 2020 at 7:48
  • DB running on local system Commented Jun 24, 2020 at 11:02

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I did but still I can not connect
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.
name does not resolve - error for me...
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

6 Comments

Hi @Veikko Thank you for your answer. I have try as per you answer I got this error ibb.co/Kqq9X1T as well as Here is my docker-compose file code pastebin.com/VmhZwtaL and here I shared my postgresql.conf contains pastebin.com/R7ifFrGR and pg_hba.conf file contains pastebin.com/yC2zCfBG
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.
can you please clerify what should I enter in Host name/address field?
Use ’docker-host’ in Host name/address.
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?
|
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.

1 Comment

happy hear it, you're welcome
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

1 Comment

Where is I find real IP. I have done give both command here is output of both command pastebin.com/v91294x0 waiting for your reply thanks
0

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

install_pgadmin4_using_docker

output

4 Comments

No it not work like that. I have to connect local postgres database using docker pgAdmin4.
It works for me, I am using it in Ubuntu 20.04 - PostgreSQL -12 and PgAdmin4.23(Docker).
thank you for your reply. can you please update here steps which you follow. Thanks
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.