不同规模的最佳数据库选择
为什么需要数据库?
OpenClaw 使用数据库存储:
- 对话历史
- 用户配置
- 任务状态
- 知识库索引
数据库选项
1. SQLite(默认)
适合:单机、个人使用
database:
type: sqlite
path: ~/.openclaw/data/openclaw.db
优点:
- 零配置
- 轻量级
- 够用
缺点:
- 不支持高并发
- 无远程访问
2. PostgreSQL
适合:团队、企业
database:
type: postgresql
host: localhost
port: 5432
name: openclaw
user: ${DB_USER}
password: ${DB_PASSWORD}
优点:
- 高性能
- 支持并发
- 功能丰富
缺点:
- 需要安装
- 配置复杂
3. MySQL
适合:已有 MySQL 环境
database:
type: mysql
host: localhost
port: 3306
name: openclaw
user: ${DB_USER}
password: ${DB_PASSWORD}
PostgreSQL 安装
macOS
brew install postgresql@16
brew services start postgresql@16
Ubuntu
sudo apt install postgresql
sudo systemctl start postgresql
Docker
docker run -d \
--name openclaw-postgres \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 \
postgres:16
数据库初始化
# 创建数据库
createdb openclaw
# 初始化表结构
openclaw db init
# 检查
openclaw db check
性能优化
索引
-- 常用查询加索引
CREATE INDEX idx_messages_session ON messages(session_id);
CREATE INDEX idx_messages_created ON messages(created_at);
连接池
database:
pool_size: 10
max_overflow: 20
定期清理
heartbeat:
tasks:
- name: cleanup-old-data
schedule: "0 0 * * 0" # 每周日
action: |
DELETE FROM messages WHERE created_at < NOW() - INTERVAL '30 days';
备份策略
自动备份
heartbeat:
tasks:
- name: backup-database
schedule: "0 2 * * *" # 每天 2:00
action: |
pg_dump openclaw > ~/backups/openclaw_$(date +%Y%m%d).sql
恢复
psql openclaw < backup.sql
迁移指南
SQLite → PostgreSQL
# 导出
sqlite3 openclaw.db .dump > dump.sql
# 转换(需要工具)
# 使用 pgloader 或手动转换
# 导入
psql openclaw < converted.sql
常见问题
Q: SQLite 够用吗? A: 单机使用完全够用,10000+ 对话没问题。
Q: 什么时候升级 PostgreSQL? A: 多用户、高并发、需要远程访问时。
Q: 数据会丢吗? A: 定期备份,不会丢。
💬 你用什么数据库?评论区分享经验!
🎯 需要数据库配置服务?微信:yang1002378395