Grafana High Availability: Using PostgreSQL Backend
You can use PostgreSQL as Grafana’s backend database.
This tutorial explains the Pigsty deployment system through these tasks:
- How to create a new database cluster
- How to create new business users in an existing cluster
- How to create new business databases in an existing cluster
- How to access databases created by Pigsty
- How to manage Grafana dashboards
- How to manage PostgreSQL datasources in Grafana
- How to upgrade Grafana database in one step
TL;DR
Create Database Cluster
You can define a grafana database on pg-meta. You can also create a dedicated pg-grafana cluster on new nodes.
Define Cluster
To create a new dedicated cluster pg-grafana on machines 10.10.10.11 and 10.10.10.12, use this config:
Create Cluster
Use this command to create the pg-grafana cluster: pgsql.yml.
This command is the Ansible Playbook pgsql.yml for creating database clusters.
Cluster initialization automatically creates the users and databases in pg_users and pg_databases. After cluster creation, any of these connection strings provides database access without DNS:
Pigsty defaults to a single meta node. The following steps use its existing pg-meta cluster for the Grafana user and database. They do not use the pg-grafana cluster created above.
Create Grafana Business User
Create the user before the database. A database with an owner depends on that user.
Define User
To create user dbuser_grafana on the pg-meta cluster, first add this user definition to pg-meta’s cluster definition:
Location: all.children.pg-meta.vars.pg_users
If you define a different password here, replace the corresponding parameter in subsequent steps
Create User
Use this command to create the dbuser_grafana user (either works):
This actually calls the Ansible Playbook pgsql-user.yml to create the user:
The dbrole_admin role has permission to execute DDL changes in the database, which is exactly what Grafana needs.
Create Grafana Business Database
Define Database
Creating a business database follows the same pattern as users. First add the new database grafana definition to pg-meta’s cluster definition.
Location: all.children.pg-meta.vars.pg_databases
Create Database
Use this command to create the grafana database (either works):
This actually calls the Ansible Playbook pgsql-db.yml to create the database:
Use Grafana Business Database
Verify Connection String Reachability
You can access the database using different services or access methods, for example:
Here, we’ll use the Default service that directly accesses the primary through load balancer.
First verify the connection string is reachable and has DDL execution permissions:
Directly Modify Grafana Config
To make Grafana use a Postgres datasource, edit /etc/grafana/grafana.ini and modify the config:
Change the default config to:
Then restart Grafana:
Activity in the new grafana database indicates that Grafana now uses PostgreSQL as its main backend.
The original Grafana dashboards and datasources are absent from this new backend. Import the dashboards and Postgres datasources again.
Manage Grafana Dashboards
As admin user, navigate to the files/grafana directory under the Pigsty directory and run grafana.py init to reload Pigsty dashboards.
Execution result:
The script detects the environment defined in ~/pigsty during installation. It gets Grafana access information and replaces dashboard URL placeholders (*.pigsty) with actual domains.
Use grafana.py clean to delete target dashboards. Use grafana.py load to load all dashboards from the current directory. When Pigsty dashboards change, use these two commands to upgrade all dashboards.
Manage Postgres Datasources
Pigsty registers PostgreSQL datasources in Grafana when pgsql.yml creates a cluster or pgsql-db.yml creates a business database. You can directly access target database instances through Grafana using the default monitoring user. Most pgcat application features depend on this.
To register Postgres databases, use the register_grafana task in pgsql.yml:
One-Step Grafana Upgrade
Change the Pigsty configuration to switch the Grafana backend database in one step. Edit the grafana_pgurl parameter in pigsty.yml:
Then re-run the grafana task from infra.yml to complete the Grafana upgrade:
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)