---
title: "在浏览器中将 SQL Server 迁移到 PostgreSQL"
description: "将 SQL Server 数据库迁移到 PostgreSQL：把 SSMS 生成的脚本转换为表、键和数据相同的脚本，并报告每项更改。"
canonical: https://intosql.com/zh-cn/sql-server-to-postgresql
last_updated: 2026-10-15
---

# 在浏览器中将 SQL Server 迁移到 PostgreSQL

拖入 SSMS 的"生成脚本"写出的包含架构和数据的脚本（.sql 或 .sql.gz，UTF-16 或 UTF-8），即可下载包含相同表、键、索引和数据的 PostgreSQL 脚本。PostgreSQL 无法原样存储的每个值，以及它不支持的每个对象，都会在报告中列出。

[SQL Server 到 PostgreSQL 迁移指南](https://intosql.com/docs/migrations/sql-server-to-postgresql)

[SQL Server 到 PostgreSQL 的数据类型](https://intosql.com/docs/types/sql-server-to-postgresql)

## 从 SQL Server 到 PostgreSQL 的变化

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

| 在 SQL Server 中 | 在 PostgreSQL 中 | 报告记录 |
| --- | --- | --- |
| bit | boolean |  |
| tinyint | smallint，并带有确保非负的 CHECK | `unsigned_widened` |
| char(n), varchar(n), nchar(n), nvarchar(n), varchar(max), text | varchar(n) 和 text |  |
| decimal(p,s), money, smallmoney | numeric(p,s)；money 为 numeric(19,4) |  |
| datetime, datetime2, smalldatetime | timestamp(p) |  |
| datetime2(7), time(7) and datetimeoffset(7) | 秒保留六位小数，第七位截断 | `fraction_cut` |
| datetimeoffset | timestamptz，时刻相同 |  |
| time | time(p) |  |
| json (SQL Server 2025) | jsonb |  |
| uniqueidentifier | uuid |  |
| binary, varbinary, image | bytea |  |
| geometry, geography | bytea，存放 SQL Server 自己的字节 | `spatial_as_native` |
| rowversion, timestamp | bytea，空 | `rowversion_empty` |
| sql_variant, hierarchyid, vector | text | `type_replaced` |
| collations that ignore case (_CI_) | 精确比较 | `case_insensitive_collation` |
| IDENTITY | GENERATED BY DEFAULT AS IDENTITY，从最大的 id 之后继续 |  |
| CREATE SEQUENCE | CREATE SEQUENCE，选项相同 |  |
| computed columns | 生成列，STORED | `virtual_as_stored` |
| computed columns with functions PostgreSQL lacks | 普通列 | `generated_not_translated` |
| defaults with functions PostgreSQL lacks, and NEXT VALUE FOR | 无默认值 | `default_not_translated` |
| CHECK constraints | 已转换 |  |
| CHECK constraints with functions PostgreSQL lacks | 不创建 | `check_skipped` |
| CHECK constraints added WITH NOCHECK or disabled | 不创建 | `check_not_enforced` |
| filtered and columnstore indexes | 不创建 | `index_skipped` |
| foreign keys | 外键，在行之后创建 |  |
| foreign keys to tables the dump doesn't create | 不创建 | `foreign_key_skipped` |
| tables on a partition scheme | 单个表，包含每个分区的行 | `partitions_dropped` |
| temporal tables (SYSTEM_VERSIONING) | 两个表：当前行和历史记录 | `system_versioning_dropped` |
| NOT NULL columns with values that become NULL | 去除 NOT NULL | `not_null_relaxed` |
| views, functions, procedures, triggers, synonyms, types and partition functions | 在报告中列出 | `not_converted_objects` |
| scripts without CREATE TABLE (data only) | 根据值推断类型 | `no_schema` |
| expressions in INSERT values | NULL | `expression_value` |
| types IntoSQL doesn't know | text | `unknown_type` |
| dates that aren't valid | NULL | `date_out_of_range` |
| NUL characters in text | 删除 | `nul_removed` |
| text that isn't valid in its encoding | 错误的字节变为替换字符 | `invalid_utf8_replaced` |
| text decoded twice (Ã±) | 按原样复制 | `mojibake_suspected` |

## 将脚本导入 PostgreSQL

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

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

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

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

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

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

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

- 在 SSMS 中右键单击数据库，依次选择"任务"、"生成脚本"，然后选择表。dbatools、mssql-scripter 以及其他基于同一库的工具也会写出相同的脚本。
- 在"高级"中，将"要编写脚本的数据的类型"设为"架构和数据"。只含架构的脚本中，表没有行。
- 以 SSMS 默认的 Unicode 或 UTF-8 保存：两者的读取方式相同，也支持 gzip。

## 哪些内容不会转换

视图、函数、存储过程、触发器、同义词、规则和安全策略都是用 T-SQL 编写的。它们会按名称在报告中列出，绝不会被悄悄丢弃，以便你为 PostgreSQL 重写。

## 关于 SQL Server 转 PostgreSQL 的问题

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

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

### 转储可以多大？

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

### 它能读取 SSMS 保存的 UTF-16 文件吗？

能。SSMS 默认以 UTF-16 保存脚本，而转换器同样能读取 UTF-16 和 UTF-8，无论是否经过 gzip 压缩，批处理之间的 GO 也能识别。

### 脚本可以来自哪些 SQL Server 版本？

由 SSMS、dbatools 或 mssql-scripter 从 SQL Server 2017 及更高版本写出的脚本。每个测试脚本都来自 SQL Server 2017、2022 和 2025，并与其源数据逐个单元格比较。

### IDENTITY 值会保留吗？

会。IDENTITY 列变为标识列，每一行都保留其 id，下一个 id 从最大的 id 之后继续。序列以相同的选项创建。

### money、datetimeoffset 和其他 SQL Server 类型会怎样处理？

bit 变为 boolean，money 变为 numeric(19,4)，uniqueidentifier 变为 uuid，xml 仍为 xml，datetimeoffset 变为 timestamptz。datetime2 保留六位秒的小数，第七位截断。不区分大小写的列改为精确比较，报告会说明是哪些列。

### 存储过程、触发器和视图怎么处理？

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

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

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

## 从 SQL Server

- [SQL Server 到 MySQL](https://intosql.com/zh-cn/sql-server-to-mysql)
- [SQL Server 到 MariaDB](https://intosql.com/zh-cn/sql-server-to-mariadb)
- [SQL Server 到 SQLite](https://intosql.com/zh-cn/sql-server-to-sqlite)
- [SQL Server 到 SQL Server](https://intosql.com/zh-cn/sql-server-to-sql-server)
- [SQL Server 到 Oracle](https://intosql.com/zh-cn/sql-server-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)
- [SQLite 到 PostgreSQL](https://intosql.com/zh-cn/sqlite-to-postgresql)
- [Oracle 到 PostgreSQL](https://intosql.com/zh-cn/oracle-to-postgresql)

## 指南和所有组合

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