Published on

Local PostgreSQL docker container with PgAdmin4

Authors
  • avatar
    Name
    ZoruChan
    Twitter

Local PostgreSQL docker container with PgAdmin4

The other day, I had to do some quick data wrangling with some client’s data, decided to just dump their data and spin up a local PostgreSQL database.

In this tutorial, we will create a PostgreSQL database and access it from PgAdmin using docker-compose. In my opinion, docker compose provides the easiest way to have your Postres container and PgAdmin on the same network.

docker-compose.yml

version: '3'
services:
  postgresql:
    image: postgres:11.8
    container_name: postgresql
    ports:
      - 5432:5432
    volumes:
      - ./postgres/init:/docker-entrypoint-initdb.d
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_INITDB_ARGS: '--encoding=UTF-8'
    hostname: postgres
    restart: always
    user: root
  pgadmin4:
    image: dpage/pgadmin4:4.27
    container_name: pgadmin4
    ports:
      - 80:80
    environment:
      PGADMIN_DEFAULT_EMAIL: root@root.com
      PGADMIN_DEFAULT_PASSWORD: root
    hostname: pgadmin4
    restart: always
    tty: true

files

.
├── docker-compose.yml
└─── postgres
     └── init
         └── dumpfile.sql

The init folder can contain any backup files you have and want to load into the DB on start.

How to run

docker-compose up -d --force-recreate

Notice: Sometimes docker-compose might have some previous build cache that interferes. Hence, the — force-recreate argument. Sometimes, it’s good to run the good old docker system prune --all command as well if you encounter any errors.

Login to PgAdmin

Open your browser and access http://localhost (Container can be accessed on port 80 from outside)

You can login to PgAdmin with user root@root.com and password root.

The host name when you create a new server connection (your local database container) will be postgresql . You will be able to connect with user root and password root .