Migrating Grafana in Docker from SQLite to Postgres
Because the documentation that people refer to no longer mentions external databases at all, and the old documentation which tells you to use GF_DATABASE_URL
doesn’t work, and migrations are painful…
Configure postgres and Grafana
compose.yml
services:
graphdb:
image: postgres:17
ports:
- 5435:5432
volumes:
- /data/grafana/postgres:/var/lib/postgresql
environment:
- POSTGRES_PASSWORD=dummy
graph:
image: grafana/grafana:10.4.2
volumes:
- /data/grafana:/var/lib/grafana
environment:
- GF_PATHS_CONFIG=/var/lib/grafana/grafana.ini
/data/grafana/grafana.ini
[database]
url = postgresql://postgres:dummy@graphdb/grafana
Directly translating sqlite tables into postgres tables doesn’t work, so we need grafana to create its own tables:
docker compose up -d graphdb
docker compose exec graphdb psql -h 127.0.0.1 -U postgres -c 'create database grafana;'
docker compose up -d graph
Once we have a postgres database with empty tables, we can migrate the data:
graph.load
load database
from sqlite:///data/grafana/grafana.db
into postgresql://postgres:dummy@localhost:5435/grafana
with data only, reset sequences
set work_mem to '16MB', maintenance_work_mem to '512 MB';
pgloader graph.load
2025-04-17 00:00:00 -0500