Running Percona Distribution for PostgreSQL with PMM monitoring and query analytics in Docker Compose

Hi, I am developing an application that uses PostgreSQL. I’m using Docker Compose in development, and I thought I’d share my configuration as I’ve spent some time setting it up.

This is the configuration:

  1. Starts Percona Distribution for PostgreSQL with the desired parameters and enables pg_stat_monitor to monitor and analyze queries.
  2. Starts PMM server, which can be opened in browser at localhost address.
  3. Starts PMM Client with the required parameters, which sends data to PMM Server.

It is very convenient during application development to see how the database feels and find slow queries.

version: '3.7'
services:

  postgres:
    image: percona/percona-distribution-postgresql:16.2-multi
    environment:
      POSTGRES_PASSWORD: password
      POSTGRES_USER: postgres
      LANG: en_US.utf8
      PGDATA: /data/db
    volumes:
      - pgdata:/data/db
    ports: 
      - "5432:5432"
    command: >
      bash -c "
        postgres -c shared_preload_libraries=pg_stat_monitor \
                -c pg_stat_monitor.pgsm_query_max_len=10000 \
                -c pg_stat_monitor.pgsm_normalized_query=0 & \
        sleep 20 && \
        PGPASSWORD=password psql -U postgres --host=localhost -d postgres -c 'CREATE EXTENSION pg_stat_monitor;' --set=sslmode=disable && \
        PGPASSWORD=password psql -U postgres --host=localhost -d your_database -c 'CREATE EXTENSION pg_stat_monitor;' --set=sslmode=disable
      "

  pmm-server:
      image: percona/pmm-server:2
      platform: "linux/amd64"
      container_name: pmm-server
      restart: always
      ports:
        - 8081:80
        - 443:443
      volumes:
        - pmm-data:/srv
      environment:
        - DISABLE_TELEMETRY=0
      healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost"]
        interval: 30s
        timeout: 10s
        retries: 5

  pmm-client-postgres:
    image: percona/pmm-client:2
    platform: "linux/amd64"
    container_name: pmm-client-postgres
    depends_on:
      pmm-server:
        condition: service_healthy
    environment:
      PMM_AGENT_SERVER_ADDRESS: pmm-server
      PMM_AGENT_SERVER_USERNAME: admin
      PMM_AGENT_SERVER_PASSWORD: admin
      PMM_AGENT_SERVER_INSECURE_TLS: 1
      PMM_AGENT_CONFIG_FILE: config/pmm-agent.yaml
      PMM_AGENT_SETUP: 1
      PMM_AGENT_SETUP_FORCE: 1
      PMM_AGENT_PRERUN_SCRIPT: "pmm-admin status --wait=10s; pmm-admin add postgresql --username=postgres --password=password --host=postgres --port=5432 --query-source=pgstatmonitor"

volumes:
  pgdata:
  pmm-data:

I’m using macOS with an ARM processor, you may need to modify the images.

Dashboard example.