Configure Grafana connection DB as Postgres using Dokku

Gideon MainaGideon Maina
2 min read

Dokku has this behaviour of deleting a container and creating a new one when using the restart and rebuild ps commands. The app will of course lose its previous data. Grafana uses SQLite to store its metadata which you would have preferred if it was persistent. You can use Dokku's persistent storage to save the SQLite data (this was kind of tricky for me) or create a database service/app and connect to it. We will use the latter using Postgres which is pretty straightforward.

Assumptions: You already use Dokku version 0.19 or higher.

PS: This connection DB is not necessarily the one you will use as a data source to create dashboards. A separate DB to store Grafana's metadata such as users, teams, organizations, dashboards etc makes things cleaner.

Creating a Grafana app with Dokku

Create a simple Dockerfile using a Grafana image of your choice.

Dockerfile

FROM grafana/grafana-enterprise

Initialize Git in the working directory and commit the Dockerfile.

git init
git add .
git commit -m "Basic setup"

Set create a Dokku app in your server: dokku apps:create my-grafana-app

Add the dokku app repository as a remote origin: git remote add dokku-origin dokku@<host>:my-grafana-app

Push your Dockerfile to Dokku repository: git push -u dokku-origin. If branch name is different from master, add <branch-name>:master to this command

You will be presented with a prompt to log in to your server using SSH. If everything goes well sit and relax as Dokku builds your app.

Create Postgres Database

  1. Install Postgres plugin

    sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
    
  2. Create database dokku postgres:create my-grafana-db.

    You can override the default user password using the -p option

  3. Link database to app dokku postgres:link my-grafana-db my-grafana-app

This will generate a DATABASE_URL environment variable attached to the app in the form DATABASE_URL=postgres://user:password@host:port/my_grafana_db.

You can confirm this by running dokku config:show my-grafana-app.

Grafana DB config

Configure the Grafana app to connect to the service.

dokku config:set my-grafana-app GF_DATABASE_TYPE=postgres GF_DATABASE_URL=<DATABASE_URL>

This will restart the app with the new settings.

Warning: Always change the default Grafana admin password, which is admin, to a strong password in production mode. You can achieve this by setting the environment variable GF_SECURITY_ADMIN_PASSWORD to the Grafana app.

0
Subscribe to my newsletter

Read articles from Gideon Maina directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gideon Maina
Gideon Maina

Gideon is a serial experimenter who loves dismantling stuff. He dreams of creating the next tech unicorn with his $100 home server.