Running PostgreSQL and pgAdmin using Docker

Introduction

Docker allows you to run an application in an isolated environment called a container.

Containers contain everything needed to run the application.
They are easily shared, so everyone can work in consistent, standardized environment.

In this article, I will show you how to use Docker to create a PostgreSQL database and access it using the pgAdmin portal. Using Docker is faster and saves you from the hassle of manually installing development software on your local machine.

Create Docker Compose file

Let's create a docker-compose.yml file with the following content:

version: "3.8"

services:
  db:
    container_name: postgres_container
    image: postgres:14.1
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"
    volumes:
      - ./postgresql_data:/var/lib/postgresql/data
  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4:6.3
    environment:
      PGADMIN_DEFAULT_EMAIL: root@root.com
      PGADMIN_DEFAULT_PASSWORD: root
    ports:
      - "8080:80"
    volumes:
      - ./pgadmin_data:/var/lib/pgadmin

When you run docker compose up, two containers are created: PostgreSQL container along with a pgAdmin container.

Docker will only download the images the first time you run this command. Image is a blueprint for the container and the container is an instance of that image.

Each container joins the default network set up by Compose automatically and is reachable by other containers on that network.

You can use the specified container's name when referencing the container within a Docker network. For example, within the pgadmin_container, your connection string to database would look like postgres_container:5432.

The volumes tag creates a binding between the host's and the container's file system. It can be used to share data between the host and the container.

It also ensures that any data you've created in volumes isn't lost.
In other words, these volumes allow the data to persist even after the container is destroyed.

If you update the docker-compose.yml file, run docker compose up to recreate the containers (mounted volumes are preserved). If there are no changes, the command just starts the containers.

Connect to PostgreSQL database using pgAdmin

Enter http://localhost:8080 in a browser to see the pgAdmin application running.

Use the root@root.com for the email address and root as the password to log in.

Once you have logged into pgAdmin, you must define a connection to the server.
Select Object > Create > Server to create a new server.

Enter any name as the server name and select the Connection tab.

Use the PostgreSQL database's container name postgres_container for the Host address field.
Use postgres for both Username and Password fields.

pgAdmin Create Server Connection Tab

Common Docker commands

# List all containers (with their IDs)
docker ps --all

# Create and start / destroy containers
docker compose up
docker compose down 

# Start / stop existing containers
docker compose start 
docker compose stop 

# Get container's IP
docker inspect CONTAINER_ID | grep -i "ipaddress"

# View logs (follow logs live)
docker compose logs --follow

# List images
docker images