DEV Community

Cover image for How to monitor PostgreSQL with Prometheus and Grafana | Docker
Nelson Hernández
Nelson Hernández

Posted on • Edited on

11 2

How to monitor PostgreSQL with Prometheus and Grafana | Docker

For this example we will use Docker compose to be able to run all the services.

1.Docker Compose



version: "3.9"
services:
  grafana:
    image: grafana/grafana
    ports:
      - 3000:3000

  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro

  postgres:
    image: postgres:12
    ports:
      - 5432:5432
    volumes:
      - ./backup:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword
      POSTGRES_DB: shop

  postgres-exporter:
    image: prometheuscommunity/postgres-exporter
    ports:
      - 9187:9187
    environment:
      DATA_SOURCE_NAME: "postgresql://postgres:postgrespassword@postgres:5432/shop?sslmode=disable"
    links:
      - postgres
      - prometheus



Enter fullscreen mode Exit fullscreen mode

2.Prometheus File Settings (prometheus.yml)



global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: postgres-exporter
    static_configs:
      - targets: ["postgres-exporter:9187"]


Enter fullscreen mode Exit fullscreen mode

3.Run Docker Compose



docker-compose up


Enter fullscreen mode Exit fullscreen mode

4.Check status of Prometheus and PostgreSQL Exporter



http://localhost:9090/targets


Enter fullscreen mode Exit fullscreen mode

5.Visit Grafana Dashboard

Default

User: admin
Password: admin



http://localhost:3000


Enter fullscreen mode Exit fullscreen mode

Image description

6.Add data source

Image description

Save and test

Image description

7.Import PostgreSQL Dashboard for Prometheus

For this we will use a Dashboard created by the community

Image description

https://grafana.com/grafana/dashboards/9628

8.Import JSON or code
Image description

Image description

Code: 9628

9.Dashboard

Image description

Code of example (GitHub Gist)

https://gist.github.com/nelsoncode019/1c0f7930aab755223de7f971f3aca94b

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
_devpjmb_ profile image
Out-Box-Dev

Get "postgres-exporter:9187/metrics": dial tcp: lookup postgres-exporter on 127.0.0.11:53: no such host

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay