Setup PostgreSQL Sandbox with Docker

In this recipe we will use Docker and Docker compose to setup a PostgreSQL sandbox. The sandbox will run following services:

  • PostgreSQL database server

  • Pgadmin

Although we call it sandbox, the environment preserves the data and configuration between sessions. You can start and stop the environment without loosing configuration and data.

Define the environment

To define our environment, we need to describe it to Docker compose. Create a docker-compose.yml file with the following environment definition:

docker-compose.yml
 version: '3.8'

 services:
 pgdb:
     image: postgres
     restart: always
     environment:
         - POSTGRES_USER=postgres
         - POSTGRES_PASSWORD=postgres
     ports:
         - '5432:5432'
     volumes:
         - pgdb:/var/lib/postgresql/data

 pgadmin:
     container_name: pgadmin4_container
     image: dpage/pgadmin4
     restart: always
     environment:
     PGADMIN_DEFAULT_EMAIL: admin@admin.com
     PGADMIN_DEFAULT_PASSWORD: postgres
     ports:
         - "5050:80"
     volumes:
         - pgadmin:/var/lib/pgadmin

 volumes:
     pgdb:
         driver: local
     pgadmin:
         driver: local

Start the environment

$ docker compose up

To open pgadmin, navigate the web browser to http://localhost:5050. To login pagadmin:

  • Admin email: admin@admin.com

  • Admin password: postgres

If this is the first time you login to pgadmin, you need to create a database server. Use following settings:

  • General

    • Name: my-db

  • Connection

    • Host name/address: pgdb

    • Username: postgres

    • Password: postgres

Shutdown the environment

$ docker compose down

Further reading