Monitor Your PostgreSQL Grafana Database Using Grafana

Introduction
Grafana stores its users, dashboards, organizations, and settings in a database. If you are using PostgreSQL as the Grafana database, you can create a dashboard to monitor important information such as:
• Total users
• Admin users
• Active users
• Organizations
• Datasources
• Dashboard changes
This guide explains how to create a PostgreSQL monitoring dashboard in Grafana step by step.

Prerequisites

Before starting, make sure you have:

:white_check_mark: Grafana installed

:white_check_mark: PostgreSQL configured as Grafana’s database

:white_check_mark: Access to the PostgreSQL database

:white_check_mark: Grafana Admin privileges

Step 1: Verify PostgreSQL Connection

Open the Grafana configuration file:


and then restart the grafana
Step 2: Add PostgreSQL as a Data Source

  1. Login to Grafana.
  2. Click ConnectionsData Sources.
  3. Click Add Data Source.
  4. Select PostgreSQL.

Step 3: Create a New Dashboard

  1. Click Dashboards.
  2. Click New Dashboard.
  3. Click Add Visualization.
  4. Select your PostgreSQL datasource.

No of users with Admin Previleges

SELECT 
COUNT(*) AS admin_user_count
FROM public.org_user
WHERE role = 'Admin';

Active users information in last 24 hours

SELECT 
  o.name AS org_name,
  u.id AS user_id,
  u.login AS user_login,
  u.last_seen_at
FROM 
  public.user u
JOIN 
  public.org o ON u.org_id = o.id
WHERE 
  u.last_seen_at >= NOW() - INTERVAL '1 day'
ORDER BY 
  u.last_seen_at DESC;

Datasources by Org

select count(a.*), b."name" from public.data_source as a join public.org as b on a.org_id = b.id
group by b."name"

Note: Create more panels and KPIs based on Grafana database tables to monitor platform activity, such as active users, dashboards modified in the last week, total dashboards, total datasources, total users, organizations, admin users, login activity, and dashboard usage statistics. This will help administrators gain better visibility into Grafana usage and system health.