前言
以下筆者將介紹如何將 Microsoft SQL Server Database 資料轉到 PostgreSQL Database。
環境 : Windows Server 2019, Microsoft SQL Server 2019, PostgreSQL V.13
使用的工具則是 SQL Server Management Studio (SSMS)
環境準備
- 請先安裝好 SQL Server 及 PostgreSQL
先手動建立測試的 DB (ms2postgre1) ,再執行以下的 SQL ,建立 Tables 及 View ,如下,
1 | USE [ms2postgre1] |
也在 PostgreSQL 建立一個 Database 名為 ms2postgre1。
匯出資料
安裝 ODBC Driver
最簡單的方式就是透 SSMS 將資料透過 ODBC 匯出到 PostgreSQL ,
所以要先安裝 PostgreSQL ODBC driver (psqlODBC)。
因為是 SSMS 工具要使用的,所以請安裝 32bit 版本(psqlodbc_13_00_0000-x86.zip)。


安裝完成後,就可以開啟 ODBC Data Sources (32bit),
然後在 使用者資料來源名稱 的Tab 按下 新增的 Button,
就可以發現有了 PostgreSQL ANSI 及 PostgreSQL Unicode 的驅動程式。

而連到 PostgreSQL 的 odbc 連線字串為
1 |
|
MSSQL => PostgreSQL ODBC Driver => PostgreSQL
Driver 好了後,在 SSMS ms2postgre1 DB 上按右鍵,工作->匯出資料(X)

來源 就選取 SQL Server Native Client 11.0 然後設定 ms2postgre1 這個資料庫 ,

目的地 就選取 .NET Framework Data Provider for Odbc 然後輸入 PostgreSQL ODBC 的連線字串。

再來就選取要轉的資料表,

我們先試轉 Table1 試試看,

再按下 編輯對應(E) ,像 char 可以調整成 varchar , money 則改成 numeric。

如果原本已有 Table 存在,就勾選 卸除並重新建立目的地資料表 這個選項
最後就按下一步到最後。


匯出完成後,就可以到 PostgreSQL 查看資料是否正確!

參考資料
Connect to a PostgreSQL Data Source (SQL Server Import and Export Wizard)
PostgreSQL ODBC driver (psqlODBC)