Skip to content

BenedictKing/migrate_sqlite_to_pg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

migrate-sqlite-to-pg

SQLite 到 PostgreSQL 的数据库迁移工具,专为 one-api / new-api 设计。

功能特性

  • 自动迁移表结构(包含类型映射)
  • 迁移数据、主键、索引和外键
  • 智能类型转换:
    • SQLite INTEGER → PostgreSQL BIGINT
    • SQLite TEXT/JSON → PostgreSQL JSONB
    • SQLite NUMERIC → PostgreSQL BOOLEAN
    • SQLite BLOB → PostgreSQL BYTEA
  • 自动重置序列值(SERIAL/BIGSERIAL)

安装

# 使用 uv(推荐)
uv sync

# 或使用 pip
pip install -e .

使用方法

1. 设置环境变量

export SQLITE_DB_PATH="/path/to/your/one-api.db"
export PG_DSN="postgresql://user:password@localhost:5432/dbname"

2. 运行迁移

# 使用 uv
uv run migrate

# 或直接运行脚本
python migrate_sqlite_to_pg.py

环境变量

变量名 说明 示例
SQLITE_DB_PATH SQLite 数据库文件路径 /data/one-api.db
PG_DSN PostgreSQL 连接字符串 postgresql://user:pass@host:5432/db

注意事项

  • 迁移前请备份原始数据库
  • 确保目标 PostgreSQL 数据库为空或不存在同名表
  • 迁移过程中会自动处理类型转换和序列重置

依赖

  • Python >= 3.10
  • psycopg2-binary >= 2.9.11

License

MIT

About

SQLite to PostgreSQL migration tool with new-api support

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages