【OCI云迁移指南】尝试从Aurora PostgreSQL迁移到OCI带有PostgreSQL的数据库
OCI云迁移指南是什么?
以下是一系列文章,由云计算工程师(@araidon,@kazunishi,@yama6)撰写,介绍了从私有云和AWS等多个平台迁移到Oracle Cloud Infrastructure的迁移步骤。
每篇文章都会选取一个样例工作负载,并提供迁移步骤指南。
总结文章如下。
服务迁移到:Aurora PostgreSQL。
本次转移的对象是Amazon Aurora PostgreSQL。
以下将介绍使用PostgreSQL客户端应用程序pg_dump和pg_restore来迁移Aurora PostgreSQL数据库至OCI带有PostgreSQL的数据库的步骤。
转变方法
使用pg dump提取Amazon Aurora PostgreSQL的数据,并使用该转储文件通过pg restore将其迁移到OCI Database with PostgreSQL。

前提条件 (qian2 ti2 tiao2 jian4)
在AWS和OCI上搭建了公共实例的EC2和ComputeVM,以连接到各自的PostgreSQL数据库。
迁移步骤
-
- 创建 Aurora PostgreSQL
-
- 使用 pg_dump 获取转储文件
-
- 创建 OCI 数据库与 PostgreSQL
- 使用 pg_restore 执行恢复
1. 创建Aurora PostgreSQL
登录到AWS控制台,并转到RDS>创建数据库页面。
输入所需的项目。

在输入必要信息后,点击创建按钮。
在等待创建完成的过程中,我们将安装我们此次将使用的 pg_dump。
2. 使用 pg_dump 获取备份文件。
由于我们将使用的pg_dump工具已经包含在PostgreSQL客户端中,因此需要安装PostgreSQL客户端。
2-1. 安装PostgreSQL客户端
这次,我们选择了 Amazon Linux 2023 作为连接用的 EC2 实例的操作系统。
根据环境的不同,请按照适用于各种 Linux 的安装步骤进行安装。
请参考以下各个操作系统的安装步骤。
这次,我参考了以下文章来安装适用于Amazon Linux 2023的系统。
首先,我们来寻找可用的选项。
$ dnf search postgresql
=========================================================================================================================== Name & Summary Matched: postgresql ============================================================================================================================
collectd-postgresql.x86_64 : PostgreSQL plugin for collectd
postgresql-odbc.x86_64 : PostgreSQL ODBC driver
postgresql15.x86_64 : PostgreSQL client programs
postgresql15-contrib.x86_64 : Extension modules distributed with PostgreSQL
postgresql15-docs.x86_64 : Extra documentation for PostgreSQL
postgresql15-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL
postgresql15-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql15-plpython3.x86_64 : The Python3 procedural language for PostgreSQL
postgresql15-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql15-private-devel.x86_64 : PostgreSQL development header files for this build of PostgreSQL server
postgresql15-private-libs.x86_64 : The shared libraries required only for this build of PostgreSQL server
postgresql15-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql15-server-devel.x86_64 : PostgreSQL development header files and libraries
postgresql15-static.x86_64 : Statically linked PostgreSQL libraries
postgresql15-test.x86_64 : The test suite distributed with PostgreSQL
postgresql15-test-rpm-macros.noarch : Convenience RPM macros for build-time testing against PostgreSQL server
postgresql15-upgrade.x86_64 : Support for upgrading from the previous major release of PostgreSQL
我们可以看到PostgreSQL 15现在可用。
进行安装。
sudo dnf install postgresql15
完成安裝後,請尋找pg dump。
pg dump位於/usr/bin目錄下。
$ cd /
$ ls
bin boot dev etc home lib lib64 local media mnt opt proc root run sbin srv sys tmp usr var
$ cd usr/bin
$ ls | grep pg_
pg_dump
pg_dumpall
pg_isready
pg_restore
pg_upgrade
可以确认已安装了 pg_dump 和稍后要使用的 pg_restore。
从PostgreSQL客户端连接到Amazon Aurora PostgreSQL
接下来,让我们连接到Aurora。
请在已安装PostgreSQL客户端的EC2实例上,设置以下入站规则以允许访问Amazon Aurora PostgreSQL。这些规则是在创建Amazon Aurora PostgreSQL时选择的安全组配置。

設定完成後,將對Aurora的寫入端點進行連接。
$ psql --host=test-aurora-postgresql.cluster-cn07mhl9cm4v.ap-northeast-1.rds.amazonaws.com --port=5432 --username=postgres
Password for user postgres:
psql (15.0, server 14.9)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.
postgres=>
连接已确认。
2-3. 插入样本数据
使用以下的样本数据进行数据加载。
准备样本数据。
请从这个URL下载并保存到本地一个zip文件。

使用CloudShell,通过以下命令将zip文件传输到EC2实例。
[cloudshell-user@ip-XXX-XXX-XXX-XXX ~]$ ls
dvdrental.zip
[cloudshell-user@ip-XXX-XXX-XXX-XXX ~]$ scp -i 'EC2秘密鍵' 'ローカルの転送したいファイル' 'EC2ユーザー名@IPアドレス:ファイル配置先
再次通过SSH连接到EC2实例,并使用以下命令检查zip文件。
$ ls
dvdrental.zip
解压缩zip文件。
$ unzip dvdrental.zip
Archive: dvdrental.zip
inflating: dvdrental.tar
$ ls
dvdrental.tar dvdrental.zip
使用 dvdrental.tar 文件加载数据。
2-3-2. 加载样本数据
连接到 Aurora,并创建 dvdrental 数据库。
postgres=> CREATE DATABASE dvdrental;
CREATE DATABASE
我会先登出一次,然后回到EC2实例中。
postgres=> exit
$
使用pg_restore工具将数据加载到dvdrental数据库中。
$ pg_restore -h test-aurora-postgresql.cluster-cn07mhl9cm4v.ap-northeast-1.rds.amazonaws.com -U postgres -d dvdrental dvdrental.tar
Password:
输入密码后,处理将在几秒内执行完毕。
2-3-3. 样本数据的确认 de
让我们确认实际已经输入了数据。
首先,显示数据库列表。
postgres=> \l
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
dvdrental | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
rdsadmin | rdsadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | rdsadmin=CTc/rdsadmin
template0 | rdsadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/rdsadmin +
| | | | | | | rdsadmin=CTc/rdsadmin
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
(5 rows)
接下来,我们将切换数据库。
postgres=> \c dvdrental
psql (15.0, server 14.9)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
You are now connected to database "dvdrental" as user "postgres".
dvdrental=>
显示表格列表。
dvdrental=> \dt;
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+----------
public | actor | table | postgres
public | address | table | postgres
public | category | table | postgres
public | city | table | postgres
public | country | table | postgres
public | customer | table | postgres
public | film | table | postgres
public | film_actor | table | postgres
public | film_category | table | postgres
public | inventory | table | postgres
public | language | table | postgres
public | payment | table | postgres
public | rental | table | postgres
public | staff | table | postgres
public | store | table | postgres
(15 rows)
最后,我们将检查数据的内容。
dvdrental=> select * from actor limit 5;
actor_id | first_name | last_name | last_update
----------+------------+--------------+------------------------
1 | Penelope | Guiness | 2013-05-26 14:47:57.62
2 | Nick | Wahlberg | 2013-05-26 14:47:57.62
3 | Ed | Chase | 2013-05-26 14:47:57.62
4 | Jennifer | Davis | 2013-05-26 14:47:57.62
5 | Johnny | Lollobrigida | 2013-05-26 14:47:57.62
(5 rows)
我們已經確認數據已經成功加載到 Aurora。
2-4. 制作样本数据的Dump文件。
在Aurora中使用exit命令退出,并在EC2实例上执行pg dump命令。
$ pg_dump -h test-aurora-postgresql.cluster-cn07mhl9cm4v.ap-northeast-1.rds.amazonaws.com -U postgres -O -x --format=t --file=fromaws_dvdrental.tar dvdrental
Password:
$ ls
dvdrental.tar dvdrental.zip fromaws_dvdrental.tar
将会以.tar文件格式创建名为fromaws_dvdrental.tar的转储文件。此外,为了将此转储文件恢复到另一个数据库中,需要添加-O选项。这是为了不输出与对象所有权匹配原始数据库的命令而设定的选项。通过-x选项,抑制了访问权限(grant/revoke命令)的转储。
请参考下面的参考资料以获取详细信息。
使用此处创建的fromaws_dvdrental.tar文件,将数据库迁移到OCI Database with PostgreSQL。
首先从EC2传输到CloudShell,然后使用CloudShell功能将文件下载到本地保存。
scp -i test-pub-key.pem ec2-user@ec2-52-199-86-11.ap-northeast-1.compute.amazonaws.com:/home/ec2-user/fromaws_dvdrental.tar ~/fromaws_dvdrental.tar
到此为止,AWS方面的工作已经完成。
创建带有PostgreSQL的OCI数据库
我們將推進OCI側的設定。
首先,由於需要數據庫,我們將從控制台畫面開始創建。


点击下一步按钮,然后填写必要的项目。

输入必要项目后,系统将显示摘要画面,请点击发送按钮。
请等待10-20分钟,直到生成完成。

数据库已创建完毕!
4. 使用pg_restore进行恢复操作
安装PostgreSQL客户端。
与安装PostgreSQL客户端相同,我们需要在OCI的Compute VM实例上安装PostgreSQL客户端。需要注意的是,OCI的操作系统是Oracle Linux 8。
关于安装方法,我参考了这篇文章。
4-2. 连接到OCI数据库中的PostgreSQL
执行下面的命令并连接:
可以从OCI Database with PostgreSQL的DB系统详细信息中进行确认。
如果要从同一VCN中的计算VM实例进行连接,则可以从Endpoint和FQDN两个方面都可以进行连接。
[opc@test-instance ~]$ psql -h <endpoint> -U <usename> -d postgres
Password for user yamadas:
psql (14.10, server 14.9)
WARNING: psql major version 10, server major version 14.
Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=>
连接已确认!
4-3. 运行恢复操作
首先,我们需要创建数据库。
postgres=> create database oci_dvdrental;
CREATE DATABASE
使用从本地上传的 fromaws_dvdrental.tar 文件进行恢复,并跳过 PostgreSQL 步骤。
postgres=> exit
[opc@test-instance ~]$ pg_restore -h <endpoint> -U <usename> -O -d oci_dvdrental fromaws_dvdrental.tar
确认测试数据。
再次连接到OCI数据库与PostgreSQL。
切换到oci_dvdrental数据库,检查表格并确认数据。
postgres=> \c oci_dvdrental
psql (14.10, server 14.9)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
You are now connected to database "oci_dvdrental" as user "yamadas".
oci_dvdrental=> \dt;
List of relations
Schema | Name | Type | Owner
--------+---------------+-------+---------
public | actor | table | yamadas
public | address | table | yamadas
public | category | table | yamadas
public | city | table | yamadas
public | country | table | yamadas
public | customer | table | yamadas
public | film | table | yamadas
public | film_actor | table | yamadas
public | film_category | table | yamadas
public | inventory | table | yamadas
public | language | table | yamadas
public | payment | table | yamadas
public | rental | table | yamadas
public | staff | table | yamadas
public | store | table | yamadas
(15 rows)
oci_dvdrental=> select * from actor limit 5;
actor_id | first_name | last_name | last_update
----------+------------+--------------+------------------------
1 | Penelope | Guiness | 2013-05-26 14:47:57.62
2 | Nick | Wahlberg | 2013-05-26 14:47:57.62
3 | Ed | Chase | 2013-05-26 14:47:57.62
4 | Jennifer | Davis | 2013-05-26 14:47:57.62
5 | Johnny | Lollobrigida | 2013-05-26 14:47:57.62
(5 rows)
我在Aurora PostgreSQL中确认了相同的数据!
总结
我已经验证了使用pg dump和pg restore从Amazon Aurora PostgreSQL迁移到在11月中旬发布的OCI Database with PostgreSQL。如果这对您的迁移工作有帮助,我会感到非常高兴。