

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 pglogical 從 Amazon EC2 上的 PostgreSQL 遷移至 Amazon RDS for PostgreSQL Amazon EC2
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical"></a>

*Rajesh Madiwale，Amazon Web Services*

## 總結
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical-summary"></a>

此模式概述使用 PostgreSQL **pglogical** 延伸模組，將 PostgreSQL 資料庫 (9.5 版及更新版本） 從 Amazon Elastic Compute Cloud (Amazon EC2) 遷移至 Amazon Relational Database Service (Amazon RDS) for PostgreSQL 的步驟。Amazon RDS 現在支援 PostgreSQL 第 10 版的 pglogical 擴充功能。

## 先決條件和限制
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical-prereqs"></a>

**先決條件 **
+ 選擇正確的 Amazon RDS 執行個體類型。如需詳細資訊，請參閱[Amazon RDS 執行個體類型](https://aws.amazon.com/rds/instance-types/)。
+ 請確定 PostgreSQL 的來源和目標版本相同。  
+ 安裝[**pglogical** 延伸模組並與 PostgreSQL](https://github.com/2ndQuadrant/pglogical)on Amazon EC2 整合。

**產品版本**
+ Amazon RDS 上的 PostgreSQL 10 版和更新版本，具有 Amazon RDS 上支援的功能 （請參閱 AWS 文件中的 [Amazon RDS 上的 PostgreSQL](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts))。此模式是透過在 Amazon RDS 上將 PostgreSQL 9.5 遷移至 PostgreSQL 10 版來測試，但也適用於 Amazon RDS 上的 PostgreSQL 更新版本。

## Architecture
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical-architecture"></a>

**資料遷移架構**

![\[Amazon RDS 上 PostgreSQL 的資料遷移架構\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/29af3931-48de-499f-9c4b-e10a98e4bba5/images/5f5b906f-dc1a-49a5-ae3f-3e10ae854784.png)


## 工具
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical-tools"></a>
+ [https://github.com/2ndQuadrant/pglogical](https://github.com/2ndQuadrant/pglogical) 延伸模組
+ PostgreSQL 原生公用程式：[https://www.postgresql.org/docs/9.5/app-pgdump.html](https://www.postgresql.org/docs/9.5/app-pgdump.html) 和 [https://www.postgresql.org/docs/9.6/app-pgrestore.html](https://www.postgresql.org/docs/9.6/app-pgrestore.html)

## 史詩
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical-epics"></a>

### 使用 pglogical 延伸模組遷移資料
<a name="migrate-data-by-using-the-pglogical-extension"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 Amazon RDS PostgreSQL 資料庫執行個體。 | 在 Amazon RDS 中設定 PostgreSQL 資料庫執行個體。如需說明，請參閱 [Amazon RDS for PostgreSQL 文件](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.PostgreSQL.html)。 | DBA | 
| 從來源 PostgreSQL 資料庫取得結構描述傾印，並將其還原至目標 PostgreSQL 資料庫。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical.html) | DBA | 
| 開啟邏輯解碼。 | 在 Amazon RDS 資料庫參數群組中，將`rds.logical_replication`靜態參數設定為 1。如需說明，請參閱 [Amazon RDS 文件](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts.General.FeatureSupport.LogicalDecoding)。 | DBA | 
| 在來源和目標資料庫上建立 pglogical 延伸。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical.html) | DBA | 
| 在來源 PostgreSQL 資料庫上建立發佈者。 | 若要建立發佈者，請執行：<pre>psql -d dbname -p 5432 <<EOF<br />SELECT pglogical.create_node( node_name := 'provider1', dsn := 'host=<ec2-endpoint> port=5432 dbname=source-dbname user=source-dbuser' );<br />EOF</pre> | DBA | 
| 建立複寫集、新增資料表和序列。 | 若要在來源 PostgreSQL 資料庫上建立複寫集，以及將資料表和序列新增至複寫集，請執行：<pre>psql -d dbname -p 5432 <<EOF<br />SELECT pglogical.replication_set_add_all_tables('default', '{public}'::text[],synchronize_data := true);<br />EOF</pre> | DBA | 
| 建立訂閱者。 | 若要在目標 PostgreSQL 資料庫上建立訂閱者，請執行：<pre>psql -h <rds-endpoint> -d target-dbname -U target-dbuser  <<EOF<br />SELECT pglogical.create_node(<br />    node_name := 'subscriber1',<br />    dsn := 'host=<rds-endpoint> port=5432 dbname=target-dbname password=postgres user=target-dbuser'<br />);<br />EOF</pre> | DBA | 
| 建立訂閱。 | 若要在目標 PostgreSQL 資料庫上建立訂閱，請執行：<pre>psql -h <rds-endpoint> -d target -U postgres  <<EOF<br />SELECT pglogical.create_subscription(<br /> subscription_name := 'subscription1',<br /> replication_sets := array['default'],<br />    provider_dsn := 'host=<ec2-endpoint> port=5432 dbname=<source-dbname> password=<password> user=source-dbuser'<br />);</pre> | DBA | 

### 驗證您的資料
<a name="validate-your-data"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢查來源和目標資料庫。 | 檢查來源和目標資料庫，以確認資料已成功複寫。您可以從來源和目標資料表使用 `select count(1)` 執行基本驗證。 | DBA | 

## 相關資源
<a name="migrate-from-postgresql-on-amazon-ec2-to-amazon-rds-for-postgresql-using-pglogical-resources"></a>
+ [Amazon RDS](https://aws.amazon.com/rds/)
+ [Amazon RDS 上的 PostgreSQL 邏輯複寫 ](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts.General.FeatureSupport.LogicalReplication)(Amazon RDS 文件）
+ [pglogical](https://github.com/2ndQuadrant/pglogical) (GitHub 儲存庫）
+ [pglogical 的限制](https://github.com/2ndQuadrant/pglogical#limitations-and-restrictions) (GitHub 儲存庫 README 檔案）
+ [使用邏輯複寫將 PostgreSQL 從內部部署或 Amazon EC2 遷移至 Amazon RDS ](https://aws.amazon.com/blogs/database/migrating-postgresql-from-on-premises-or-amazon-ec2-to-amazon-rds-using-logical-replication/)(AWS 資料庫部落格）