Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: [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

like image 440
Jaykumar Patel Avatar asked Jun 18 '20 07:06

Jaykumar Patel


People also ask

Can't connect to server connection refused pgadmin4?

If pgAdmin displays this message, there are two possible reasons for this: the database server isn't running - simply start it. the server isn't configured to accept TCP/IP requests on the address shown.


1 Answers

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

like image 177
Veikko Avatar answered Oct 20 '22 09:10

Veikko