DEV Community

Cover image for How to setup Grafana with PostgreSQL database using docker-compose: A step-by-step guide
Dmitry Romanoff
Dmitry Romanoff

Posted on

How to setup Grafana with PostgreSQL database using docker-compose: A step-by-step guide

In this blog I will demonstrate how to setup Grafana with PostgreSQL database using docker-compose.

Also I will create data source in Grafana, and access some sample data from PostgreSQL database.

Docker-compose is a tool for defining and running multi-container Docker applications. With the docker-compose, it can be used a YAML file to configure our application's services.

Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources.

PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley.

Step #1 - Create docker-compose.yaml file

It has definition of the 3 services:

  • pg_data_wh - PostgreSQL database to have some sample data. It can be metrics, different kind of indicators, measurements distribution etc.
  • grafana - is a multi-platform open source analytics and interactive visualization web application.
  • pg_grafana - PostgreSQL database that will serve as a database for Grafana to keep its metadata.


docker-compose.yaml
-------------------

version: '3.8'
services:

  pg_data_wh:
    container_name: pg_data_wh
    image: postgres:15
    restart: always
    environment:
      POSTGRES_DB: my_data_wh_db
      POSTGRES_USER: my_data_wh_user
      POSTGRES_PASSWORD: my_data_wh_pwd
    ports:
      - "5488:5432"
    volumes:
      - pg_data_wh:/var/lib/postgresql/data

  pg_grafana:
    container_name: pg_grafana
    image: postgres:15
    restart: always
    environment:
      POSTGRES_DB: my_grafana_db
      POSTGRES_USER: my_grafana_user
      POSTGRES_PASSWORD: my_grafana_pwd
    ports:
      - "5499:5432"
    volumes:
      - pg_grafana:/var/lib/postgresql/data

  grafana:
    container_name: grafana
    image: grafana/grafana:latest
    user: "0:0"
    environment:
      GF_DATABASE_TYPE: postgres
      GF_DATABASE_HOST: pg_grafana:5432
      GF_DATABASE_NAME: my_grafana_db
      GF_DATABASE_USER: my_grafana_user
      GF_DATABASE_PASSWORD: my_grafana_pwd
      GF_DATABASE_SSL_MODE: disable
    restart: unless-stopped
    depends_on:
        - pg_grafana
    ports:
      - 3111:3000
    volumes:
      - grafana:/var/lib/grafana

volumes:
  pg_grafana:
    driver: local
  grafana:
    driver: local
  pg_data_wh:
    driver: local


Enter fullscreen mode Exit fullscreen mode

Step #2 - Build and run the corresponding containers



 docker-compose up -d
[+] Running 3/3
 ⠿ Container pg_grafana  Started                                                                                          0.9s
 ⠿ Container grafana     Started                                                                                          1.3s
 ⠿ Container pg_data_wh  Started                                                                                          0.9s


Enter fullscreen mode Exit fullscreen mode

Step #3 - Connect to the pg_data_wh PostgreSQL database and populate some sample data



psql -h localhost -p 5488 -U my_data_wh_user -d my_data_wh_db -W
Password:
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))
Type "help" for help.

my_data_wh_db=# create table my_table(person varchar(100), apples smallint);
CREATE TABLE
my_data_wh_db=# insert into my_table values('Anne', 10);
INSERT 0 1
my_data_wh_db=# insert into my_table values('Jane', 15);
INSERT 0 1
my_data_wh_db=# insert into my_table values('Jack', 25);
INSERT 0 1
my_data_wh_db=# insert into my_table values('Linda', 35);
INSERT 0 1


Enter fullscreen mode Exit fullscreen mode

Step #4 - Open browser and access the Grafana



http://<ip_of_the_host_machine>:3111


Enter fullscreen mode Exit fullscreen mode

Grafana

Step #5 - Define PostgreSQL data source in Grafana; it will access data from the pg_data_wh PostgreSQL database

Define PostgreSQL data source in Grafana

Step #6 - Explore the pg_data_wh PostgreSQL data from the Grafana

Explore the PostgreSQL data from the Grafana

Conclusion

In this blog I've demonstrated how to setup Grafana with PostgeSQL database using docker-compose.

Top comments (1)

Collapse
 
robotdazero_f2701a4abd026 profile image
robotdazero

greate post, it worked in no time