Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting pgadmin to postgres in docker

I have a docker-compose file with services for python, nginx, postgres and pgadmin:

services:
  postgres:
    image: postgres:9.6
    env_file: .env
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5431:5431"

  pgadmin:
    image: dpage/pgadmin4
    links:
      - postgres
    depends_on:
      - postgres
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: pwdpwd
    volumes:
      - pgadmin:/root/.pgadmin
    ports:
      - "5050:80"

  backend:
    build:
      context: ./foobar  # This refs a Dockerfile with Python and Django requirements
    command: ["/wait-for-it.sh", "postgres:5431", "--", "/gunicorn.sh"]
    volumes:
      - staticfiles_root:/foobar/static
    depends_on:
      - postgres

  nginx:
    build:
      context: ./foobar/docker/nginx
    volumes:
      - staticfiles_root:/foobar/static
    depends_on:
      - backend
    ports:
      - "0.0.0.0:80:80"

volumes:
  postgres_data:
  staticfiles_root:
  pgadmin:

When I run docker-compose up and visit localhost:5050, I see the pgadmin interface. When I try to create a new server there, with localhost or 0.0.0.0 as host name and 5431 as port, I get an error "Could not connect to server". If I remove these and instead enter postgres in the "Service" field, I get the error "definition of service "postgres" not found". How can I connect to the database with pgadmin?

like image 467
GluePear Avatar asked Nov 12 '18 16:11

GluePear


2 Answers

the docker container name changes when you run docker-compose to prefix the folder name (to keep container names unique). You could force the name of the container with container_name property

version: "3"
services:

  # postgres database
  postgres:
    image: postgres:12.3
    container_name: postgres
    environment:
      - POSTGRES_DB=admin
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=admin
      - POSTGRES_HOST_AUTH_METHOD=trust # allow all connections without a password. This is *not* recommended for prod
    volumes:
      - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
    ports:
      - "5432:5432"

  # pgadmin for managing postgis db (runs at localhost:5050)
  # To add the above postgres server to pgadmin, use hostname as defined by docker: 'postgres'
  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin
      - PGADMIN_DEFAULT_PASSWORD=admin
      - PGADMIN_LISTEN_PORT=5050
    ports:
      - "5050:5050"
  
volumes:
  database-data:

Another option is to connect the postgres container to localhost with

network_mode: host

But you lose the nice network isolation from docker that way

like image 165
Peter F Avatar answered Oct 05 '22 07:10

Peter F


Be careful that the default postgres port is 5432 not 5431. You should update the port mapping for the postgres service in your compose file. The wrong port might be the reason for the issues you reported. Change the port mapping and then try to connect to postgres:5432. localhost:5432 will not work.

like image 28
Ciprian Stoica Avatar answered Oct 05 '22 07:10

Ciprian Stoica