From b19f46df3e6069b7d8ad355a323a1487ccc7e378 Mon Sep 17 00:00:00 2001 From: Bethuel Mmbaga Date: Wed, 11 Jun 2025 12:46:04 +0300 Subject: [PATCH] add Activity Events Postgres store doc (#364) Signed-off-by: bcmmbaga --- src/components/NavigationDocs.jsx | 1 + .../selfhosted/activity-postgres-store.mdx | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/pages/selfhosted/activity-postgres-store.mdx diff --git a/src/components/NavigationDocs.jsx b/src/components/NavigationDocs.jsx index 2b8cc164..1dcf2d30 100644 --- a/src/components/NavigationDocs.jsx +++ b/src/components/NavigationDocs.jsx @@ -252,6 +252,7 @@ export const docsNavigation = [ { title: 'Advanced guide', href: '/selfhosted/selfhosted-guide' }, { title: 'Management SQLite Store', href: '/selfhosted/sqlite-store'}, { title: 'Management Postgres Store', href: '/selfhosted/postgres-store'}, + { title: 'Activity Events Postgres Store', href: '/selfhosted/activity-postgres-store'}, { title: 'Supported IdPs', href: '/selfhosted/identity-providers' }, { title: 'Management geolocation', href: '/selfhosted/geo-support' }, { title: 'Troubleshooting', href: '/selfhosted/troubleshooting' }, diff --git a/src/pages/selfhosted/activity-postgres-store.mdx b/src/pages/selfhosted/activity-postgres-store.mdx new file mode 100644 index 00000000..f53a0bf9 --- /dev/null +++ b/src/pages/selfhosted/activity-postgres-store.mdx @@ -0,0 +1,100 @@ +# Activity Events Postgresql store + +## Using Postgres for fresh installations + +The default configuration for fresh installations is SQLite storage. However users have the option to leverage the benefits +of Postgres for new instances beginning from version `0.46.0`. + +To enable Postgres add to your management service the following environmental variable: +```bash +NB_ACTIVITY_EVENT_STORE_ENGINE=postgres +NB_ACTIVITY_EVENT_POSTGRES_DSN=host=localhost port=5432 user= password= dbname= +``` + +You can switch back to sqlite storage by setting the `NB_ACTIVITY_EVENT_STORE_ENGINE` variable to `sqlite`. + + Switching between storage options requires migration steps to prevent data loss. + + + +## Migrating from SQLite store to Postgres store +This migration process allows users to seamlessly transition between storage options while maintaining data integrity. + + + The following commands assume you use the latest docker version with the compose plugin. If you have docker-compose installed as a standalone, please use docker-compose as a command. + + +1. Backup your data store (`events.db` in `datadir` - default `/var/lib/netbird/`) +```bash +mkdir backup +docker compose cp -a management:/var/lib/netbird/. backup/ +``` + +2. Import Sqlite data to Postgres + +For migrating the Sqlite data we rely on the [pgloader](https://github.com/dimitri/pgloader) tool. You can install it by running +`sudo apt-get install pgloader` on debian or `brew install pgloader` on MacOS. + +```bash +pgloader --type sqlite backup/events.db "postgresql://:@:/" +``` + +3. Create `.env` file with the following content: +```bash +NB_ACTIVITY_EVENT_STORE_ENGINE=postgres +NB_ACTIVITY_EVENT_POSTGRES_DSN="host= user= password= dbname= port=" +``` + +4. Update `docker-compose.yml` file to pass the postgres configuration to the `management` container +```yaml +environment: + - NB_ACTIVITY_EVENT_STORE_ENGINE=${NB_ACTIVITY_EVENT_STORE_ENGINE} + - NB_ACTIVITY_EVENT_POSTGRES_DSN=${NB_ACTIVITY_EVENT_POSTGRES_DSN} +env_file: + - .env +``` + +5. Restart the management service +```bash +docker compose restart management +``` + +6. Check logs to confirm the store switch: +```bash +docker compose logs management +``` +You should see an entry similar to: +``` +2025-06-06T18:23:17+03:00 INFO [context: SYSTEM] management/server/activity/store/sql_store.go:260: using postgres as activity event store engine +``` + +## Rollback to Sqlite store +To rollback to the Sqlite store, follow these steps: + + The following commands assume you use the latest docker version with the compose plugin. If you have docker-compose installed as a standalone, please use docker-compose as a command. + + +1. Restore `events.db` backup +```bash +docker compose cp backup/events.db management:/var/lib/netbird/events.db +``` + +2. Enable SQLite by setting the `NB_ACTIVITY_EVENT_STORE_ENGINE` environment variable to `sqlite`: +```bash +NB_ACTIVITY_EVENT_STORE_ENGINE=sqlite +``` + +3. Restart the Management service. +```bash +docker compose restart management +``` + +4. Check logs to confirm the store switch: +```bash +docker compose logs management +``` + +You should see an entry similar to: +``` +2025-06-06T18:24:19+03:00 INFO [context: SYSTEM] management/server/activity/store/sql_store.go:260: using sqlite as activity event store engine +``` \ No newline at end of file