How to import Postgres data into Grafana

I have been looking into ways to display data from the wife's Oura ring, as Oura ring offer an API. Shame on you Whoop for not offering one. In the past I have put data into tables and built dashboards in React, the frontend part can become labour-intensive. Therefore I looked info Grafana as a way to display the data.

Grafana ships with a built-in PostgreSQL data source plugin that allows you to query and visualize data from a PostgreSQL-compatible database.

Database user permissions (Important!)

Before setting up the data source we should create a new user that only has SELECT permissions on the specified database and tables you want to query. Grafana does not validate that the query is safe. The query could include any SQL statement. For example, statements like DELETE FROM user; and DROP TABLE user; would be executed. To protect your tables against this Grafana highly recommends you create a specific PostgreSQL user with restricted permissions.

CREATE USER grafanareader WITH PASSWORD 'password';
GRANT USAGE ON SCHEMA <schema> TO grafanareader;
GRANT SELECT ON <schema>.<table> TO grafanareader; -- you can use this as times as need to give permissions to as many tables as needed
ALTER ROLE grafanareader SET search_path = '<schema>';

Once the user has been created and given the right permissions, we can then add it into Grafana as a data source.

PostgreSQL settings

Now we know we can get the Postgres data inside Grafana we have to configure basic settings for the data source, by completing the following steps:

  • Click Connections in the left-side menu.
  • Under Your connections, click Data sources.
  • Enter PostgreSQL in the search bar.
  • Select PostgreSQL.

For example here is how I have added the information needed to connect:

grafana postgres settings

Setting up dashboards

Now we have a connection to our database, it's time to start making pretty-looking graphs. This part is pretty straightforward, to display the data you can select from the table and which columns you require. You can do this via the query UI or write your own SQL query in the code builder section.

query dashboard settings

And there you have it, pretty straightforward and no more React dashboards. You can add as many visualisations as you need to within your dashboard Grafana is your oyster.

More info can be found here