How To Setup PostgreSQL and PGAdmin4 on Docker

Bill - 14 January, 2020
Reading time 2 mins

I use docker almost every day (for my development environment) but i don't know why i installed PostgreSQL on my own laptop and not in docker like on my office computer :D. Just for your information, i use Archlinux on my laptop, and i usually got some problems with the regular system update for my PostgreSQL, it tooks me a few hours to fix it that's why i decide to use PostgreSQL database inside docker.

But, i found that by using it on docker is bit different, especially for accessing the PostgreSQL commands since it's being use inside a docker container, so we can't directly use the command through terminal.

I assume you guys already know how to work with docker, and in this article i'll share the basic setup for PostgreSQL database and its tools like PgAdmin through our favorite tool docker compose and terminal. I won't cover any security tips or best configurations since it's only for development, but if you still want to know how to hardening your docker environment for PostgreSQL, please read the documentation.

First step, make sure you have docker and docker compose installed on your machine, then you can pull the PostgreSQL image from docker hub (feel free to choose your own PostgreSQL version), i'm gonna use PostgreSQL 11 and the latest pgadmin4, run docker pull postgres:11-alpine then docker pull dpage/pgadmin4.

Quick setup

You can run the following commands in you terminal:

  $ mkdir postgres-docker && cd postgres-docker

  # Create docker volume for docker
  $ docker volume create --driver local --name=postgrevolume
  $ docker volume create --driver local --name=pgadminvolume

  # Create docker network for docker
  $ docker network create --driver bridge postgrenetwork

  # Create postgre environment files
  $ nano postgre-env.list
  POSTGRES_USER=your_pg_username
  POSTGRES_PASSWORD=your_pg_password

  $ nano pgadmin-env.list
  PGADMIN_DEFAULT_EMAIL=your_pgadmin_email@yourdomain.com
  PGADMIN_DEFAULT_PASSWORD=your_pgadmin_password

  docker run --publish 5432:5432 \
    --volume=postgrevolume:/pgdata \
    --env-file=postgre-env.list \
    --name="postgres" \
    --hostname="postgres" \
    --network="postgrenetwork" \
    --detach \
  postgres:11-alpine

  docker run --publish 8088:80 \
    --volume=pgadminvolume:/var/lib/pgadmin \
    --env-file=pgadmin-env.list \
    --name="pgadmin4" \
    --hostname="pgadmin4" \
    --network="postgrenetwork" \
    --detach \
  dpage/pgadmin4:latest

Open your browser to localhost:8088 and input your pgadmin email and password. Add new server then set the postgres container IP address as host, you can check the IP address through this command docker container inspect postgres or you can just set postgres as host since we've run our docker with --hostname="postgres" option.

Docker Compose

We can also use docker compose to wrapped up our services in one file. Create docker-compose.yml file inside postgres-docker directory with the following configurations:

version: '3.7'
services:
  database:
    image: postgres:11-alpine
    container_name: postgres
    hostname: postgres
    env_file:
      - ./postgre-env.list
    volumes:
      - postgresvolume:/pgdata
    ports:
      - '5432:5432'
    networks:
      - postgrenetwork

  pgadmin4:
    image: dpage/pgadmin4:latest
    container_name: pgadmin4
    hostname: pgadmin4
    env_file:
      - ./pgadmin-env.list
    volumes:
      - pgadminvolume:/var/lib/pgadmin
    ports:
      - '8088:80'
    networks:
      - postgrenetwork

networks:
  postgrenetwork:

volumes:
  postgresvolume:
  pgadminvolume:

Save it and run docker-compose up -d, it'll work as well as you run directly through docker run command on terminal.

With this, there will be no problems anymore if i update/upgrade my linux system, i can completely remove PostgreSQL package from my laptop and just using it through docker container.

# postgresql # docker - Blog posts