https://github.com/wbh1/grafana-sqlite-to-postgres
Grafana SQLite to Postgres Database Migrator
https://github.com/wbh1/grafana-sqlite-to-postgres
grafana postgres sqlite
Last synced: 3 months ago
JSON representation
Grafana SQLite to Postgres Database Migrator
- Host: GitHub
- URL: https://github.com/wbh1/grafana-sqlite-to-postgres
- Owner: wbh1
- License: mit
- Created: 2019-05-16T21:48:59.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-22T21:14:56.000Z (10 months ago)
- Last Synced: 2025-03-24T13:51:20.487Z (3 months ago)
- Topics: grafana, postgres, sqlite
- Language: Go
- Size: 58.6 KB
- Stars: 100
- Watchers: 2
- Forks: 26
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Grafana SQLite to Postgres Database Migrator
[](https://goreportcard.com/report/github.com/wbh1/grafana-sqlite-to-postgres)
# ⚠️ Unmaintained Warning ⚠️
WARNING: This project is currently maintained at a "best-effort" level. I no longer have the free time to keep up with changes to Grafana's database schema and handle/test edge cases.One day, perhaps this can be rewritten to be more flexible, but it is not now. I'll continue to try to accept PRs as needed. This project started out of necessity and a desire to make a difficult move simpler for others. However, it's not something that I've personally had to use for years, so it's difficult to justify continuing to put in effort to this.
Use the project at your own risk (always back up your database [and test the backup!!]).
## Background
[My blog post](https://wbhegedus.me/migrating-grafanas-database-from-sqlite-to-postgres/)I ran into the issue of Grafana logging users out because the SQLite database was locked and tokens couldn't be looked up. This is discussed in [#10727 on grafana/grafana](https://github.com/grafana/grafana/issues/10727#issuecomment-479378941). The solution is to migrate to a database that isn't the default one of SQLite.
## Prerequisites
You **must** already have an existing database in Postgres for Grafana.Run `CREATE DATABASE grafana` in `psql` to make the database. Then, start up an instance of Grafana pointed to the new database. Grafana will automagically create all the tables that it will need. You can shut Grafana down once those tables are made. We **need** those tables to exist for the migration to work.
## Compatability
Tested on:| OS | SQLite Version | Postgres Version | Grafana Version |
| -------------- | -------------- | ---------------- | --------------- |
| MacOS | 3.24.0 | 11.3 | 6.1.0+ |
| CentOS 7/RHEL7 | 3.7.17 | 11.3 | 6.1.0+ |
| Fedora 36 | 3.36.0 | 15.0 | 9.2.0 |## Usage
```
usage: Grafana SQLite to Postgres Migrator []A command-line application to migrate Grafana data from SQLite to Postgres.
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--dump=/tmp Directory path where the sqlite dump should be stored.Args:
Path to SQLite file being imported.
URL-format database connection string to use in the URL format (postgres://USERNAME:PASSWORD@HOST/DATABASE).
```
### Use as Docker image
1. Build docker image: `docker build -t grafana-sqlite-to-postgres .`
2. Run migration: `docker run --rm -ti -v :/grafana.db grafana-sqlite-to-postgres /grafana.db "postgres://:@:5432/?sslmode=disable"`## Example Command
This is the command I used to transfer my Grafana database:
```
./grafana-migrate grafana.db "postgres://postgres:PASSWORDHERE@localhost:5432/grafana?sslmode=disable"
```
Notice the `?sslmode=disable` parameter. The [pq](https://github.com/lib/pq) driver has sslmode turned on by default, so you may need to add a parameter to adjust it. You can see all the support connection string parameters [here](https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters).## How it works
1. Dumps SQLite database to /tmp
2. Sanitize the dump so it can be imported to Postgres
3. Import the dump to the Grafana database## Acknowledgments
Inspiration for this program was taken from
- [haron/grafana-migrator](https://github.com/haron/grafana-migrator)
- [This blog post](https://0x63.me/migrating-grafana-from-sqlite-to-postgresql/)