迁移至Databases for PostgreSQL
将数据从现有PostgreSQL数据库迁移到IBM Cloud® Databases for PostgreSQL 有多种选择。 我们专注于最简单、最有效的方法。 要开始使用,您需要在本地安装PostgreSQL,以便拥有 psql
和 pg_dump
工具。 虽然并非严格要求,但Cloud DatabasesCLICLI 使连接和恢复到新的Databases for PostgreSQL部署变得更容易。
pg_dump
在源数据库上运行 pg_dump
创建 SQL 文件,用于重新创建数据库。 At a minimum, pg_dump
takes a hostname (-h
flag), port number (-p
flag), database name (-d
flag), username (-U
flag), and a file
(or directory name) to write the dump to (-f
flag).
例如,下面的命令使用管理员用户转储托管在sl-eu-lon-2-portal.4.dblayer.com 上、端口为 17980 的PostgreSQL"compose "数据库,并将结果保存在 dump.sql
中。
pg_dump -h sl-eu-lon-2-portal.4.dblayer.com -p 17980 -d compose -U admin -f dump.sql
恢复 pg_dump 的输出
随后,pg_dump
的输出结果可上载到新的Databases for PostgreSQL部署中。 由于输出是 SQL,因此只需通过 psql
命令将其发送到数据库即可。 我们建议使用管理员用户进行导入。
有关如何使用 psql
以 管理员身份连接的详细信息,请参阅连接 psql
。 要使用 psql
命令连接,需要管理员用户的连接字符串和 TLS 证书。 证书需要从base64解码,并存储为任意本地文件。 To import
the previously created dump.sql
into a database deployment named example-psql
, the psql
command can be called with -f dump.sql
as a parameter. 该参数指示 psql
读取并执行文件中的 SQL 语句。 命令内容如下
PGPASSWORD=yourpasswordhere PGSSLROOTCERT=cert.crt psql 'host=c7798cf6-e5d2-4513-b17f-3d3fa67d8291.8f7bfd8f3faa4218aec56e069eb46187.databases.appdomain.cloud port=32484 dbname=ibmclouddb user=admin sslmode=verify-full' -f dump.sql
As noted in that 连接 psql
documentation, the Cloud Databases CLI plug-in simplifies connecting. 之前的 psql
导入可以按以下方式进行:
ibmcloud cdb deployment-connections example-psql -s -- -f dump.sql
如果没有指定用户,命令会自动使用 admin 用户。 它还会以交互方式提示输入密码。 TLS 证书会自动检索和使用。 一旦从应用程序接口获得详细信息,-s
就会启动 psql
(或任何已配置的命令)。 --
后面的内容都会传递给命令。
当还原过程运行时,它会发出一些信息,说明它对数据库部署所做的更改。
使用 pg_restore 的额外迁移选项
For users with a TAR file containing sql and data separately, the command pg_restore
can be used to migrate your data in addition to the psql
commands previously noted. pg_restore
命令的示例如下
PGPASSWORD=yourpasswordhere PGSSLROOTCERT=cert.crt pg_restore -h c7798cf6-e5d2-4513-b17f-3d3fa67d8291.8f7bfd8f3faa4218aec56e069eb46187.databases.appdomain.cloud -p 32484 -U admin -F t -d ibmclouddb tarfile.tar