Skip to main content

Migrate NetBird SQLite Databases to PostgreSQL

This is how I moved the three SQLite stores in my combined NetBird deployment to PostgreSQL 18. I used pgloader because I needed to carry over existing management data, embedded identity data, and activity history:

  • store.db -> netbird_store
  • idp.db -> netbird_auth
  • events.db -> netbird_events

Prerequisites​

  • A combined NetBird Docker Compose deployment with a netbird-server service. The example uses ./data for the bind mount at /var/lib/netbird. For a named volume, use NetBird's backup procedure to export the stopped service's data instead of copying ./data.
  • Keep the same NetBird image version for the migration and verify which stores your deployment uses. The auth migration applies to the embedded IdP. Skip an optional store if your setup doesn't use it.
  • Install pgloader, sqlite3, and a PostgreSQL client where you will run the import. The CAST lists below are the ones I needed. Compare them with your copied SQLite schema and the upstream migration guide for your release.
  • Schedule downtime. Keep the backup, .load files, and database credentials private. The pgloader statements include include drop and belong on newly created, dedicated target databases.
Back up first

Stop NetBird and preserve the entire data directory, configuration, and encryption key before modifying either database.

1. Stop NetBird and create a backup​

(
set -eu
umask 077
docker compose stop netbird-server
mkdir backup
mkdir backup/data backup/migration
cp -a data/. backup/data/
cp -a backup/data/. backup/migration/
cp -a config.yaml docker-compose.yml backup/
)

Use a fresh backup directory. Adapt the Compose filename and copy any environment and proxy files needed for rollback. Keep backup/data unchanged and use backup/migration as the working copy. Retain the complete backup until you've verified the PostgreSQL deployment and its backups.

2. PostgreSQL Compose configuration​

PostgreSQL 18 uses /var/lib/postgresql as the persistent volume mount. Add the following to your existing Netbird docker-compose.yaml.

services:
netbird-db:
image: postgres:18
container_name: netbird-db
restart: unless-stopped
networks: [netbird]
ports:
- "127.0.0.1:5432:5432"
env_file:
- ./postgres.env
volumes:
- ./postgres_data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
start_period: 20s
interval: 10s
retries: 5
timeout: 5s

Under the existing services.netbird-server settings, add the dependency to wait for PostgreSQL to become healthy. Keep any other services already listed in depends_on.

depends_on:
netbird-db:
condition: service_healthy

Example postgres.env:

POSTGRES_USER=admin
POSTGRES_PASSWORD=REPLACE_WITH_A_STRONG_PASSWORD
POSTGRES_DB=postgres

The loopback mapping is for pgloader running on the Docker host. Remove it after the import if no host-side client needs it. NetBird connects to netbird-db:5432 over the shared Compose network. If PostgreSQL is on another host, use a private reachable address, restrict access to the necessary clients, and configure PostgreSQL TLS rather than carrying over the example's sslmode=disable.

3. Create the PostgreSQL databases​

Bring up the database without restarting NetBird. Wait for it to become healthy. Create the role using psql's password prompt so you don't put the password in the shell command.

docker compose up -d netbird-db
docker compose ps netbird-db
docker compose exec netbird-db psql -U admin -d postgres

At the psql prompt:

CREATE ROLE netbird LOGIN;
\password netbird
CREATE DATABASE netbird_store OWNER netbird;
CREATE DATABASE netbird_auth OWNER netbird;
CREATE DATABASE netbird_events OWNER netbird;
\q

4. Create the custom pgloader JSON transform​

Some NetBird JSON fields are SQLite BLOBs. Without a transform, pgloader emits a Common Lisp byte-vector representation such as #(123 34 ...), which PostgreSQL rejects as JSON.

Create transforms.lisp:

(in-package #:pgloader.transforms)

(defun byte-vector-to-json (value)
(when value
(sb-ext:octets-to-string value :external-format :utf-8)))

Load this file with pgloader's -l / --load-lisp-file option.

5. Migrate store.db​

Create sqlite.load with mode 0600. Replace the example source path in each load file with your working copy and percent-encode special characters in the password part of the PostgreSQL URI.

LOAD DATABASE
FROM sqlite:///root/compose/netbird/backup/migration/store.db
INTO postgresql://netbird:REPLACE_PASSWORD@127.0.0.1:5432/netbird_store

WITH include drop, create tables, create indexes, reset sequences

CAST
column accounts.is_domain_primary_account to boolean,
column accounts.settings_peer_login_expiration_enabled to boolean,
column accounts.settings_peer_inactivity_expiration_enabled to boolean,
column accounts.settings_regular_users_view_blocked to boolean,
column accounts.settings_groups_propagation_enabled to boolean,
column accounts.settings_jwt_groups_enabled to boolean,
column accounts.settings_routing_peer_dns_resolution_enabled to boolean,
column accounts.settings_extra_peer_approval_enabled to boolean,
column accounts.settings_extra_user_approval_required to boolean,
column accounts.settings_lazy_connection_enabled to boolean,
column accounts.settings_peer_expose_enabled to boolean,
column accounts.settings_auto_update_always to boolean,
column accounts.settings_metrics_push_enabled to boolean,
column accounts.settings_agent_network_only to boolean,
column accounts.settings_local_mfa_enabled to boolean,
column jobs.workload_parameters to jsonb using byte-vector-to-json,
column jobs.workload_result to jsonb using byte-vector-to-json
;

Run the migration:

pgloader -l ./transforms.lisp ./sqlite.load
CAST syntax

Separate CAST rules with commas and leave the final rule without one. Recent upstream instructions include accounts.settings_peer_hostname_collision_rejected. If your copy has that column, add a boolean cast for it too. Check your actual schema before adding or removing casts.

6. Resolve orphaned target records​

If PostgreSQL refuses to create targets_service_id_fkey, locate orphaned rows:

sqlite3 /root/compose/netbird/backup/migration/store.db "
SELECT t.id, t.service_id
FROM targets t
LEFT JOIN services s ON s.id = t.service_id
WHERE t.service_id IS NOT NULL
AND s.id IS NULL;
"

If the returned targets are confirmed obsolete, remove them from the migration copy. The IDs below came from my setup. Use only the rows you confirmed are obsolete in your own migration copy.

sqlite3 /root/compose/netbird/backup/migration/store.db "
DELETE FROM targets
WHERE id IN (15,16,26,27);
"

Run the orphan query again. It should return no rows before rerunning pgloader.

warning

Do not delete orphaned records blindly. Restore missing services rows or update the references if the targets are still required.

7. Migrate idp.db​

For an embedded IdP, create auth-sqlite.load with the same file permissions as sqlite.load.

LOAD DATABASE
FROM sqlite:///root/compose/netbird/backup/migration/idp.db
INTO postgresql://netbird:REPLACE_PASSWORD@127.0.0.1:5432/netbird_auth

WITH include drop, create tables, create indexes, reset sequences

CAST
type blob to bytea drop default using byte-vector-to-bytea,
column client.public to boolean,
column auth_request.force_approval_prompt to boolean,
column auth_request.logged_in to boolean,
column auth_request.claims_email_verified to boolean,
column auth_code.claims_email_verified to boolean,
column refresh_token.claims_email_verified to boolean,
column auth_request.mfa_validated to boolean,
column password.email_verified to boolean,
column user_identity.claims_email_verified to boolean
;

Run:

pgloader ./auth-sqlite.load

8. Migrate events.db​

Migrate the activity database into the PostgreSQL database selected for activity events:

Create activity-sqlite.load with the same file permissions.

LOAD DATABASE
FROM sqlite:///root/compose/netbird/backup/migration/events.db
INTO postgresql://netbird:REPLACE_PASSWORD@127.0.0.1:5432/netbird_events

WITH include drop, create tables, create indexes, reset sequences
;
pgloader ./activity-sqlite.load

9. Preserve the NetBird encryption key​

Reuse the exact encryption key from the previous NetBird configuration. pgloader copies encrypted values but does not decrypt or re-encrypt them.

For the combined setup:

server:
store:
encryptionKey: "REPLACE_WITH_EXISTING_KEY"

If you can't recover the old key, stop the migration and investigate. A new key can't decrypt the fields already stored in the database.

10. Configure NetBird for PostgreSQL​

Merge the store settings into the existing server block of config.yaml. Keep store.encryptionKey and other settings already in that block. The examples show the management, auth, and activity sections separately.

server:
store:
engine: postgres
dsn: "host=netbird-db port=5432 user=netbird password=REPLACE_PASSWORD dbname=netbird_store sslmode=disable"

Example auth-store configuration:

server:
authStore:
engine: postgres
dsn: "host=netbird-db port=5432 user=netbird password=REPLACE_PASSWORD dbname=netbird_auth sslmode=disable"

Example activity-store configuration:

server:
activityStore:
engine: postgres
dsn: "host=netbird-db port=5432 user=netbird password=REPLACE_PASSWORD dbname=netbird_events sslmode=disable"

11. Verify the migration​

Review the pgloader summaries, check rejected rows, and compare table counts across each source and destination before starting NetBird. A successful import should show:

  • Zero table-copy errors
  • Create Foreign Keys with zero errors
  • Matching counts in jobs, including rows using the JSON transform, if that table exists
  • The expected number of valid targets

Start NetBird:

docker compose up -d netbird-server
docker compose logs --tail=200 netbird-server

Confirm the store engines:

docker compose logs netbird-server | grep store

Expected messages include:

using Postgres store engine
using postgres as activity event store engine

Also verify:

  1. Dashboard login works.
  2. Existing users, peers, groups, and policies appear. Connect from a known existing peer.
  3. Audit events load correctly.
  4. No encryption-key warning appears.
  5. docker compose ps reports netbird-db as healthy.

12. Archive the legacy SQLite files​

Keep the untouched backup until tested PostgreSQL backups are available. If the checks fail, stop NetBird, restore the old settings and the untouched SQLite files, then restart with the original image version and verify login and peer access. Writes made after the PostgreSQL cutover will need to be accounted for before rollback.

Keep the GeoLite and Geonames data files in place in the ./data/ directory:

GeoLite2-City_*.mmdb
geonames_*.db

References​