Skip to content

活动事件 Postgresql 存储

使用 Postgres 进行全新安装 全新安装的默认配置是 SQLite 存储。但是,从版本 . 开始,用户可以选择利用 Postgres 的优势来配置新实例。 要启用 Postgres,请将以下环境变量添加到您的管理服务中:

shell
NB_ACTIVITY_EVENT_STORE_ENGINE=postgres
NB_ACTIVITY_EVENT_POSTGRES_DSN=host=localhost port=5432 user=<user> password=<password> dbname=<db_name>

NB_ACTIVITY_EVENT_STORE_ENGINE您可以通过将变量设置为.来切换回 sqlite 存储sqlite。

TIP

切换存储选项需要采取迁移步骤以防止数据丢失。

  1. 备份您的数据存储(events.db默认)datadir/var/lib/netbird/
shell
mkdir backup
docker compose cp -a management:/var/lib/netbird/. backup/
  1. 将 SQLite 数据导入 Postgres 迁移 SQLite 数据时,我们使用pgloadersudo apt-get install pgloader工具。您可以在 Debian 或macOS 系统上运行命令来安装它 brew install pgloader。
shell
pgloader --type sqlite backup/events.db "postgresql://<PG_USER>:<PG_PASSWORD>@<PG_HOST>:<PG_PORT>/<PG_DB_NAME>"
  1. 创建.env包含以下内容的文件:
shell
NB_ACTIVITY_EVENT_STORE_ENGINE=postgres
NB_ACTIVITY_EVENT_POSTGRES_DSN="host=<PG_HOST> user=<PG_USER> password=<PG_PASSWORD> dbname=<PG_DB_NAME> port=<PG_PORT>"
  1. 更新docker-compose.yml文件,将 PostgreSQL 配置传递给management容器
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
  1. 重启管理服务
shell
docker compose restart management
  1. 检查日志
shell
docker compose logs management

您应该会看到类似这样的条目:

log
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

回滚到 SQLite 存储

要回滚到 SQLite 存储,请按照以下步骤操作: 1.恢复events.db备份

shell
docker compose cp backup/events.db management:/var/lib/netbird/events.db
  1. 启用 SQLite 的方法:设置NB_ACTIVITY_EVENT_STORE_ENGINE环境变量为sqlite:
shell
NB_ACTIVITY_EVENT_STORE_ENGINE=sqlite
  1. 重启管理服务
shell
docker compose restart management
  1. 检查日志
shell
docker compose logs management

您应该会看到类似这样的条目:

log
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