本文介绍了如何将 PostgreSQL 数据库迁移到 SQLServer 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PostgreSQL 数据库,我想将它迁移到 SQL Server —— 架构和数据.我很穷,所以我不想付任何钱.我也很懒,所以我不想做太多的工作.目前我正在逐表做这个,大约有100个表要做.这是非常乏味的.

I have a PostgreSQL database that I want to move to SQL Server -- both schema and data. I am poor so I don't want to pay any money. I am also lazy, so I don't want to do very much work. Currently I'm doing this table by table, and there are about 100 tables to do. This is extremely tedious.

有什么技巧可以满足我的要求吗?

Is there some sort of trick that does what I want?

推荐答案

您应该能够在此 Serverfault 页面的已接受答案中找到一些有用的信息:https://serverfault.com/questions/65407/best-tool-to-migrate-a-postgresql-database-to-ms-sql-2005.

You should be able to find some useful information in the accepted answer in this Serverfault page: https://serverfault.com/questions/65407/best-tool-to-migrate-a-postgresql-database-to-ms-sql-2005.

如果您可以在没有数据的情况下转换架构,则可以使用此命令缩短数据步骤:

If you can get the schema converted without the data, you may be able to shorten the steps for the data by using this command:

pg_dump --data-only --column-inserts your_db_name > data_load_script.sql

这种加载会很慢,但是 --column-inserts 选项会为每行数据生成最通用的 INSERT 语句,并且应该兼容.

This load will be quite slow, but the --column-inserts option generates the most generic INSERT statements possible for each row of data and should be compatible.

关于转换架构的建议如下:

Suggestions on converting the schema follows:

我将从转储架构开始,但删除与所有权或权限有关的任何内容.这应该足够了:

I would start by dumping the schema, but removing anything that has to do with ownership or permissions. This should be enough:

pg_dump --schema-only --no-owner --no-privileges your_db_name > schema_create_script.sql

编辑此文件以将行 BEGIN TRANSACTION; 添加到开头,将 ROLLBACK TRANSACTION; 行添加到末尾.现在您可以加载它并在 SQL Server 的查询窗口中运行它.如果遇到任何错误,请确保转到文件底部,突出显示 ROLLBACK 语句并运行它(在突出显示语句时按 F5).

Edit this file to add the line BEGIN TRANSACTION; to the beginning and ROLLBACK TRANSACTION; to the end. Now you can load it and run it in a query window in SQL Server. If you get any errors, make sure you go to the bottom of the file, highlight the ROLLBACK statement and run it (by hitting F5 while the statement is highlighted).

基本上,您必须解决每个错误,直到脚本干净地运行.然后你可以将 ROLLBACK TRANSACTION 更改为 COMMIT TRANSACTION 并运行最后一次.

Basically, you have to resolve each error until the script runs through cleanly. Then you can change the ROLLBACK TRANSACTION to COMMIT TRANSACTION and run one final time.

不幸的是,我无法帮助您解决可能会看到的错误,因为我从未从 PostgreSQL 转到 SQL Server,只是反过来.但是,有些事情我认为会成为问题(显然,不是详尽的清单):

Unfortunately, I cannot help with which errors you may see as I have never gone from PostgreSQL to SQL Server, only the other way around. Some things that I would expect to be an issue, however (obviously, NOT an exhaustive list):

  • PostgreSQL 通过使用 DEFAULTNOT NULL INTEGER 字段链接到 SEQUENCE 来自动增加字段.在 SQL Server 中,这是一个 IDENTITY 列,但它们并不完全相同.我不确定它们是否等效,但如果您的原始模式充满了id"字段,您可能会遇到一些麻烦.我不知道 SQL Server 是否有 CREATE SEQUENCE,所以您可能需要删除它们.
  • 数据库函数/存储过程不会在 RDBMS 平台之间进行转换.您需要删除所有 CREATE FUNCTION 语句并手动转换算法.
  • 注意数据文件的编码.我是 Linux 人,所以我不知道如何在 Windows 中验证编码,但您需要确保 SQL Server 期望的内容与您从 PostgreSQL 导入的文件相同.pg_dump 有一个选项 --encoding= 可以让你设置特定的编码.我似乎记得 Windows 倾向于对 Unicode 使用两字节的 UTF-16 编码,而 PostgreSQL 使用 UTF-8.由于 UTF-16 输出,我在从 SQL Server 到 PostgreSQL 的过程中遇到了一些问题,因此值得研究.
  • PostgreSQL 数据类型 TEXT 只是一个没有最大长度的 VARCHAR.在 SQL Server 中,TEXT 很复杂(并且已弃用).原始架构中声明为 TEXT 的每个字段都需要针对适当的 SQL Server 数据类型进行审核.
  • SQL Server 为 UNICODE 数据提供了额外的数据类型.我对它不够熟悉,无法提出建议.我只是指出这可能是个问题.
  • PostgreSQL does auto-increment fields by linking a NOT NULL INTEGER field to a SEQUENCE using a DEFAULT. In SQL Server, this is an IDENTITY column, but they're not exactly the same thing. I'm not sure if they are equivalent, but if your original schema is full of "id" fields, you may be in for some trouble. I don't know if SQL Server has CREATE SEQUENCE, so you may have to remove those.
  • Database functions / Stored Procedures do not translate between RDBMS platforms. You'll need to remove any CREATE FUNCTION statements and translate the algorithms manually.
  • Be careful about encoding of the data file. I'm a Linux person, so I have no idea how to verify encoding in Windows, but you need to make sure that what SQL Server expects is the same as the file you are importing from PostgreSQL. pg_dump has an option --encoding= that will let you set a specific encoding. I seem to recall that Windows tends to use two-byte, UTF-16 encoding for Unicode where PostgreSQL uses UTF-8. I had some issue going from SQL Server to PostgreSQL due to UTF-16 output so it would be worth researching.
  • The PostgreSQL datatype TEXT is simply a VARCHAR without a max length. In SQL Server, TEXT is... complicated (and deprecated). Each field in your original schema that are declared as TEXT will need to be reviewed for an appropriate SQL Server data type.
  • SQL Server has extra data types for UNICODE data. I'm not familiar enough with it to make suggestions. I'm just pointing out that it may be an issue.

这篇关于如何将 PostgreSQL 数据库迁移到 SQLServer 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:55