---
title: "在浏览器中将 SQLite 迁移到 PostgreSQL"
description: "将 SQLite 数据库迁移到 PostgreSQL：把 .db 文件或其转储转换为表、键和数据相同的脚本，并报告每项更改。"
canonical: https://intosql.com/zh-cn/sqlite-to-postgresql
last_updated: 2026-10-15
---

# 在浏览器中将 SQLite 迁移到 PostgreSQL

拖入 SQLite 数据库文件（.db、.sqlite、.sqlite3）或 sqlite3 的 .dump 写出的 SQL，即可下载包含相同表、键、索引和数据的 PostgreSQL 脚本。每列获得其值都符合的类型，PostgreSQL 无法原样存储的每个值都会在报告中列出。

[SQLite 到 PostgreSQL 迁移指南](https://intosql.com/docs/migrations/sqlite-to-postgresql)

[SQLite 到 PostgreSQL 的数据类型](https://intosql.com/docs/types/sqlite-to-postgresql)

## 从 SQLite 到 PostgreSQL 的变化

每一行是转换器应用的一条规则，以及该规则更改你的数据时在报告中写入的记录。

| 在 SQLite 中 | 在 PostgreSQL 中 | 报告记录 |
| --- | --- | --- |
| declared types (any text: VARCHAR(255), DATETIME, BOOLEAN, JSON or none) | 列中每个值都符合的类型 |  |
| BOOLEAN, BOOL | boolean |  |
| TEXT, VARCHAR(n), CHAR(n), CLOB | 每个值都符合声明的长度时为 varchar(n)，否则为 text |  |
| DECIMAL(p,s), NUMERIC | 每个值都符合时为精确类型，否则为 double |  |
| BLOB, and columns holding bytes | bytea |  |
| DATE, DATETIME, TIMESTAMP holding ISO 8601 text | date、timestamp |  |
| JSON holding JSON text | jsonb |  |
| Infinity in REAL columns | Infinity，PostgreSQL 的 double 会保留它 |  |
| INTEGER PRIMARY KEY, AUTOINCREMENT | GENERATED BY DEFAULT AS IDENTITY，从最大的 id 或 sqlite_sequence 的值之后继续 |  |
| COLLATE NOCASE | 精确比较 | `case_insensitive_collation` |
| generated columns | STORED | `virtual_as_stored` |
| generated columns with functions IntoSQL doesn't write, and the || operator | 普通列 | `generated_not_translated` |
| defaults with functions IntoSQL doesn't write (strftime, randomblob) | 无默认值 | `default_not_translated` |
| CHECK constraints | 已转换 |  |
| CHECK constraints with functions IntoSQL doesn't write | 不创建 | `check_skipped` |
| partial and expression indexes | 不创建 | `index_skipped` |
| foreign keys (unenforced unless PRAGMA foreign_keys=ON) | 外键，先以 NOT VALID 添加，然后验证 |  |
| foreign keys to tables the dump doesn't create | 不创建 | `foreign_key_skipped` |
| primary keys holding NULL, and NOT NULL columns with values that become NULL | 去除 NOT NULL | `not_null_relaxed` |
| views, triggers and virtual tables (FTS, R*Tree) | 在报告中列出 | `not_converted_objects` |
| dumps without CREATE TABLE (--data-only) | 根据值推断类型 | `no_schema` |
| expressions in INSERT values other than the text functions dumps write | NULL | `expression_value` |
| NUL characters in text | 去除 | `nul_removed` |
| text that isn't valid UTF-8 | 错误的字节变为替换字符 | `invalid_utf8_replaced` |
| text decoded twice (Ã±) | 按原样复制 | `mojibake_suspected` |

## 将脚本导入 PostgreSQL

脚本先创建表、复制行，再添加键、索引和外键，因此导入速度快，并会在遇到第一个错误时停止。

```shell
psql -v ON_ERROR_STOP=1 -d mydb -f sqlite.postgresql.sql
```

外键先以 NOT VALID 添加，然后再验证。如果某行指向不存在的行，脚本会告诉你是哪个约束，而不是中途失败。

对于较大的转储，请先创建数据库，然后在服务器所在的机器上用 psql 运行脚本。

IntoSQL Pro 可以通过桌面应用或命令行将文件直接导入 PostgreSQL，无需生成脚本。

## 如何转储 SQLite 以获得最佳结果

转换器读取转储中的内容。以下选项能让转储保留更多数据：

- 数据库文件本身会原样转换，在浏览器中逐页读取。请先关闭正在写入它的应用，以免有更改遗留在其 -wal 文件中。
- sqlite3 app.db .dump > app.sql 会将同一个数据库写为 SQL，它同样可以转换，格式为 .sql 或 .sql.gz。
- DB Browser for SQLite 通过 File、Export、Database to SQL file 也能写出该 SQL，Python 的 iterdump() 也一样。

## 哪些内容不会转换

视图和触发器是用 SQLite 自己的语言编写的。它们会按名称在报告中列出，绝不会被悄悄丢弃，以便你为 PostgreSQL 重写。FTS5 和 R*Tree 等虚拟表也会列出，其影子表则不包含在内。

## 关于 SQLite 转 PostgreSQL 的问题

### 我的转储会被上传吗？

不会。转换器在你的浏览器中运行，分块从磁盘读取转储，因此转储不会离开你的电脑。桌面应用和命令行也在你的电脑上转换。

### 转储可以多大？

转换器从不把整个转储放进内存：它按范围读取文件两次，边读边写出脚本。几 GB 的转储可以在浏览器、桌面应用和命令行中转换。

### 可以直接转换 .db 文件吗？

可以。拖入 .db、.sqlite 或 .sqlite3 文件：它在浏览器中读取，绝不会上传。含 NUL 字符的文本和 double 值的每一位数字都会按存储原样保留，而 .dump 可能会将其截断。

### 转储可以来自哪些 SQLite 版本？

SQLite 3.0 及更高版本的 UTF-8 或 UTF-16 数据库文件，以及由 sqlite3 shell 的 .dump 或 Python 的 iterdump() 写出的转储。每个测试文件和转储都来自 sqlite3 3.31 到 3.54，包括 STRICT 和 WITHOUT ROWID 表，并与其源数据逐个单元格比较。

### SQLite 不强制类型时，列类型如何确定？

声明的类型只是提示，由值决定：当每个值都符合时，列会变为 integer、numeric、double precision、date、timestamp、boolean、bytea 或 text。包含混合值的列保留其文本，报告会指出是哪些列。

### INTEGER PRIMARY KEY 和 AUTOINCREMENT 会保留吗？

会。INTEGER PRIMARY KEY 变为标识列，每一行都保留其 id，下一个 id 从最大的 id 之后继续；使用 AUTOINCREMENT 时则从 sqlite_sequence 的值之后继续。

### 视图和触发器怎么处理？

它们会按名称在报告中列出，但不会转换，因为每种数据库都用自己的语言编写它们。

### 脚本可以导入哪些 PostgreSQL 版本？

PostgreSQL 13 到 18。同一个脚本可以在其中每个版本上导入，如有问题会在第一个错误处停止。

## 从 SQLite

- [SQLite 到 MySQL](https://intosql.com/zh-cn/sqlite-to-mysql)
- [SQLite 到 MariaDB](https://intosql.com/zh-cn/sqlite-to-mariadb)
- [SQLite 到 SQLite](https://intosql.com/zh-cn/sqlite-to-sqlite)
- [SQLite 到 SQL Server](https://intosql.com/zh-cn/sqlite-to-sql-server)
- [SQLite 到 Oracle](https://intosql.com/zh-cn/sqlite-to-oracle)

## 到 PostgreSQL

- [MySQL 到 PostgreSQL](https://intosql.com/zh-cn/mysql-to-postgresql)
- [MariaDB 到 PostgreSQL](https://intosql.com/zh-cn/mariadb-to-postgresql)
- [PostgreSQL 到 PostgreSQL](https://intosql.com/zh-cn/postgresql-to-postgresql)
- [SQL Server 到 PostgreSQL](https://intosql.com/zh-cn/sql-server-to-postgresql)
- [Oracle 到 PostgreSQL](https://intosql.com/zh-cn/oracle-to-postgresql)

## 指南和所有组合

- [PostgreSQL 到 SQLite](https://intosql.com/zh-cn/postgresql-to-sqlite)
- [SQLite 到 PostgreSQL 迁移指南](https://intosql.com/docs/migrations/sqlite-to-postgresql)
- [SQLite 到 PostgreSQL 的数据类型](https://intosql.com/docs/types/sqlite-to-postgresql)
- [所有数据库组合](https://intosql.com/zh-cn/database-migration)
