文档中心>云数据库 PostgreSQL>操作指南>备份与恢复>在云服务器上恢复 PostgreSQL 数据

在云服务器上恢复 PostgreSQL 数据

最近更新时间:2025-05-12 10:10:42

我的收藏
当用户出现数据丢失或者损坏时,可通过控制台 克隆实例 功能对数据进行按时间点恢复或者按备份集恢复,可恢复的时间根据日志保留时间和全量备份时间来决定。您也可以直接下载备份恢复至自建数据库。下面为您详细描述。
注意:
如果云数据库 PostgreSQL 实例开启了透明数据加密(TDE),则不支持将云数据库的数据恢复至自建数据库。

通过控制台下载备份进行恢复

1. 前置条件

待恢复数据的云服务器 CVM 中必须安装与备份数据相同版本的 PostgreSQL 数据库。详细安装教程请参考 PostgreSQL 官方文档。Linux 系统的云服务器配置请参见 快速配置 Linux 云服务器

2. 使用 postgres 用户创建恢复目录

要恢复数据,首先需要在云服务器中创建数据的恢复目录,并设置权限。
mkdir -p /var/lib/pgsql/16/recovery
chown postgres /var/lib/pgsql/16/recovery
其中,16为示例的数据库大版本号,recovery 为示例目录,您可根据实际情况自定义恢复目录名称。后续示例中不同版本目录名将不再区分描述,请以实际为准,如 PostgreSQL 12.x 为/var/lib/pgsql/12。

3. 下载全量备份文件

3.1. 登录 PostgreSQL 控制台,在实例列表,单击操作列的管理进入管理页面。
3.2. 选择备份恢复页,在数据备份列表中选择需要恢复的备份集,单击操作列的下载
3.3. 根据提供的 VPC 网络地址或外网地址链接下载备份文件。
若使用 VPC 网络地址下载备份,云数据库须与云服务器处于同一 VPC。执行如下命令,将备份下载至云服务器中提前建好的恢复目录,示例使用 /var/lib/pgsql/16/recovery 目录。其中, file_name 为用户自定义文件名, download_address 为系统给出的下载地址。
若选择本地下载备份文件,下载后需要将备份文件上传至云服务器中提前建好的恢复目录,示例使用 /var/lib/pgsql/16/recovery 目录。具体操作方法请参见 如何将本地文件拷贝到云服务器 。
备份下载或上传完成后,可通过以下命令查询是否成功将备份文件放到指定目录:
[root@VM-10-5-tencentos postgresql-16.8]# ls -lh /var/lib/pgsql/16/recovery
total 3952-rw-r--r-- 1 root root 4045802 May  7 20:42 manual-20250507204222.tar.zst

4. 解压全量备份文件

备份文件解压需要用到 zstd 解压工具,您可通过以下命令查询当前云服务器中是否存在该工具。
[root@VM-10-5-tencentos postgresql-16.8]# zstd –version
*** zstd command line interface 64-bits v1.4.4, by Yann Collet ***
若显示版本号信息,说明已经安装了 zstd 解压工具,若提示其他信息,则当前云服务器中还未安装。请执行以下命令进行安装。
[root@VM-10-5-tencentos postgresql-16.8]# sudo yum install epel-release
[root@VM-10-5-tencentos postgresql-16.8]# sudo yum install zstd
完成安装后,执行如下命令,在恢复目录中解压全量备份文件。
[root@VM-10-5-tencentos postgresql-16.8]# cd /var/lib/pgsql/16/recovery
[root@VM-10-5-tencentos recovery]# tar -I zstd -xvf file_name.tar.zst
解压后,执行下面的命令查看解压出的文件:
[root@VM-10-5-tencentos recovery]# ls -lh
total 4.0M
-rw------- 1 postgres postgres  225 May 7 20:42 backup_label
-rw------- 1 postgres postgres  225 May 7 20:31 backup_label.old
drwx------ 6 postgres postgres 4.0K May  7 21:13 base
-rw------- 1 postgres postgres   56 May 7 20:31 current_audit_logfiles
-rw------- 1 postgres postgres   35 May 7 20:31 current_logfiles
drwx------ 2 postgres postgres 4.0K May  7 21:13 global
-rw-r--r-- 1 root     root    3.9M May  7 20:42 manual-20250507204222.tar.zst
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_commit_ts
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_dynshmem
-rw------- 1 postgres postgres  308 May 7 20:31 pg_hba.conf
-rw------- 1 postgres postgres 2.6K May  7 20:31 pg_ident.conf
drwx------ 4 postgres postgres 4.0K May  7 20:31 pg_logical
drwx------ 4 postgres postgres 4.0K May  7 20:31 pg_multixact
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_notify
drwx------ 2 postgres postgres 4.0K May  7 20:42 pg_replslot
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_serial
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_snapshots
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_stat
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_stat_tmp
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_subtrans
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_tblspc
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_twophase
-rw------- 1 postgres postgres    3 May 7 20:31 PG_VERSION
drwx------ 3 postgres postgres 4.0K May  7 21:13 pg_wal
drwx------ 2 postgres postgres 4.0K May  7 20:31 pg_xact
-rw------- 1 postgres postgres 3.2K May  7 20:31 postgresql.conf
-rw------- 1 postgres postgres    0 May 7 20:31 standby.signal
-rw------- 1 postgres postgres    0 May 7 20:42 tablespace_map
-rw------- 1 postgres postgres. 2 May 7 20:31 TENCENTDB_RELEASE

5. 删除多余的临时文件

执行如下命令,删除多余的临时文件。
[root@VM-10-5-tencentos recovery]# rm -rf backup_label

6. 修改配置文件

执行以下命令,使用vi编辑器修改配置文件 postgresql.conf 。
[root@VM-10-5-tencentos recovery]# vi postgresql.conf
命令执行后进入文件内容页面,按i键即可进行编辑。
6.1. 在文件中找到以下内容,在每一行的行首添加 #,将其变为注释。 如有重复出现,则全部注释掉。
pg_stat_statements.track
synchronous_standby_names
extension_blacklist
archive_mode
basebackup_exclude_paths
tencentdb_syscache_max_num
shared_preload_libraries
tencentdb_relcache_max_num
archive_command
disable_dblink_connect_to_other
tencentdb_az_five
tencentdb_az_five
tencentdb_relcache_evict_num
pg_stat_statements.max
soft_limit_connections
tencentdb_syscache_evict_num
tencentdb_enable_trusted_extension
synchronous_commit
tencentdb_enable_superuser_unsafe_behaviour
tencentdb_enable_copy_to
local_preload_libraries
说明:
特别地,必须注释 include = 'standby.conf' 这一行。
6.2. 将 log_destination = ‘csvlog,auditlog’ 修改为 log_destination = ‘csvlog’。
log_destination = ‘csvlog’
6.3. 在文件中找到 port ,将其值修改为5433,以免与主实例端口冲突。
port = '5433' ##将port参数的值修改为5433
6.4. 在文件末尾文件末尾增加如下内容,表示不再使用强同步模式。
synchronous_commit = local
synchronous_standby_names = ''
修改完成后,按 esc 键退出编辑模式,再输入 :wq 保存修改并退出文件。

7. 使用 root 用户更改文件夹权限

chmod 0700 /var/lib/pgsql/16/recovery
chown postgres:postgres /var/lib/pgsql/16/recovery -R
修改后,可输入下列命令,查看修改结果。其中,返回的第三列为文件权限拥有者,在更改后应显示为 postgres。
[root@VM-10-5-tencentos recovery]# ls -al
total 4064
drwx------ 19 postgres postgres    4096 May 8 09:43 .
drwxr-xr-x  3 root    root        4096 May  7 20:44 ..
-rw-------  1 postgres postgres     225 May 7 20:42 backup_label
-rw-------  1 postgres postgres     225 May 7 20:31 backup_label.old
drwx------  6 postgres postgres    4096 May 7 21:13 base
-rw-------  1 postgres postgres      56 May 7 20:31 current_audit_logfiles
-rw-------  1 postgres postgres      35 May 7 20:31 current_logfiles
drwx------  2 postgres postgres    4096 May 7 21:13 global
-rw-r--r--  1 postgres postgres 4045802 May  7 20:42 manual-20250507204222.tar.zst
drwx------  2 postgres postgres    4096 May 7 20:31 pg_commit_ts
drwx------  2 postgres postgres    4096 May 7 20:31 pg_dynshmem
-rw-------  1 postgres postgres     308 May 7 20:31 pg_hba.conf
-rw-------  1 postgres postgres    2640 May 7 20:31 pg_ident.conf
drwx------  4 postgres postgres    4096 May 7 20:31 pg_logical
drwx------  4 postgres postgres    4096 May 7 20:31 pg_multixact
drwx------  2 postgres postgres    4096 May 7 20:31 pg_notify
drwx------  2 postgres postgres    4096 May 7 20:42 pg_replslot
drwx------  2 postgres postgres    4096 May 7 20:31 pg_serial
drwx------  2 postgres postgres    4096 May 7 20:31 pg_snapshots
drwx------  2 postgres postgres    4096 May 7 20:31 pg_stat
drwx------  2 postgres postgres    4096 May 7 20:31 pg_stat_tmp
drwx------  2 postgres postgres    4096 May 7 20:31 pg_subtrans
drwx------  2 postgres postgres    4096 May 7 20:31 pg_tblspc
drwx------  2 postgres postgres    4096 May 7 20:31 pg_twophase
-rw-------  1 postgres postgres       3 May 7 20:31 PG_VERSION
drwx------  3 postgres postgres    4096 May 7 21:13 pg_wal
drwx------  2 postgres postgres    4096 May 7 20:31 pg_xact
-rw-------  1 postgres postgres    3276 May 7 21:35 postgresql.conf
-rw-------  1 postgres postgres       0 May 7 20:31 standby.signal
-rw-------  1 postgres postgres       0 May 7 20:42 tablespace_map
-rw------- 1 postgres postgres 2 May 7 20:31 TENCENTDB_RELEASE

8.(可选)应用增量备份文件

经过前面的步骤,恢复出的数据库的内容为全量备份的数据库内容。云数据库 PostgreSQL 的备份方式包括全量备份和增量备份,具体说明请参见 备份原理和方案
全量备份与增量备份文件的格式不同,恢复方法也不同。若您还希望将增量备份文件中的数据恢复到 CVM ,则需要完成本步骤。
如跳过本步骤,则恢复出的数据库的内容为开始做全量备份时数据库的内容。
例如,12:00时做的全量备份,在使用该全量备份文件完成恢复的基础上,再将12:00至13:00的所有 xlog 文件放置在在 pg_wal 文件夹下,则数据库将恢复到13:00时的数据内容。
说明:
PostgreSQL 版本为 9.x 时,则则放置 xlog 文件的文件夹路径为 /var/lib/pgsql/9.x/recovery/pg_xlog 文件夹。
8.1. 选择备份恢复页,在日志备份列表中,选择需要恢复的日志备份集,单击操作列的下载
与全量备份文件相似,您可直接在云服务器上将文件下载到 pg-wal 目录中,也可下载到本地后再上传到云服务器的恢复目录下的 pg_wal 目录中。
若您通过 VPC 网络地址下载备份,云数据库需与云服务器处于同一 VPC 。执行如下命令,将备份下载至恢复目录下的 pg_wal 目录下。
[root@VM-10-5-tencentos postgresql-16.8]# wget -O /var/lib/pgsql/16/recovery/pg_wal/file_name.tar.zst "download_address”
下载后执行以下命令,确认是否成功将文件放到指定位置。
[root@VM-10-5-tencentos recovery]# cd /var/lib/pgsql/16/recovery/pg_wal
 [root@VM-10-5-tencentos pg_wal]# ls -lh
total 33M
-rw------- 1 postgres postgres  16M May 7 20:42 000000010000000000000003
-rw------- 1 postgres postgres  16M May 7 20:42 000000010000000000000004
-rw-r--r-- 1 root     root    1.5K May  8 10:31 20250508103101_20250508103101-20250508101527-000000010000000000000005_000000010000000000000005.tar.zst
drwx------ 2 postgres postgres 4.0K May  7 21:13 archive_status
若您选择本地下载文件,则下载后需要将备份文件上传至恢复目录下的 pg_wal 目录中。具体操作方法请参见 如何将本地文件拷贝到云服务器
8.2. 将增量备份文件解压至 pg_wal 文件夹,得到 xlog 文件。
[root@VM-10-5-tencentos pg_wal]# tar -I zstd -xvf file_name.tar.zst
000000010000000000000005
执行以下命令查看解压结果。
[root@VM-10-5-tencentos pg_wal]# ls -lh
total 49M
-rw------- 1 postgres postgres  16M May 7 20:42 000000010000000000000003
-rw------- 1 postgres postgres  16M May 7 20:42 000000010000000000000004
-rw------- 1     1003 users     16M May 8 10:31 000000010000000000000005
-rw-r--r-- 1 root     root    1.5K May  8 10:31 20250508103101_20250508103101-20250508101527-000000010000000000000005_000000010000000000000005.tar.zst
drwx------ 2 postgres postgres 4.0K May  7 21:13 archive_status

9. 验证恢复结果

9.1. 使用 postgres 用户启动数据库
[root@VM-10-5-tencentos pg_wal]# su – postgres
[postgres@VM-10-5-tencentos ~]$ /usr/local/pgsql/bin/pg_ctl start -D /var/lib/pgsql/16/recovery -l logfile
waiting for server to start.... done
server started
9.2. 验证数据库是否运行。
若提示" server is running ",则代表数据库正在运行。
[postgres@VM-10-5-tencentos ~]$ /usr/local/pgsql/bin/pg_ctl status -D /var/lib/pgsql/16/recovery
pg_ctl: server is running (PID: 609549)
/usr/local/pgsql/bin/postgres "-D" "/var/lib/pgsql/16/recovery"
9.3. 登录数据库
[postgres@VM-10-5-tencentos ~]$ cd /var/lib/pgsql/16/recovery
[postgres@VM-10-5-tencentos recovery]$ /usr/local/pgsql/bin/psql -h10.0.0.13 -p 5432 -Udbadmin -dpostgres
Password for user dbadmin:
psql (16.8)
Type "help" for help.
 
postgres=>
通过查询语句查看数据即可。

通过手动导出数据进行恢复

您也可以直接在云服务器上导出实例备份数据,然后进行恢复操作。该方案在 Windows 和 Linux 下同样适用,与物理文件所在的文件系统无关。
注意:
本方法需要用到 pg_dump 和 pg_restore 工具,两个工具均仅支持13及以上的 PostgreSQL 版本。
在云服务器上安装的 pg_dump 、 pg_restore 工具版本必须大于或等于云数据库 PostgreSQL 的版本。
不建议使用 postgres 数据库作为目标数据库。
建议您在目标端使用新建且未存放其他数据的 database 来导入数据。
如果您在目标端使用了已存在数据的 database 来导入,可能会因为对象冲突而失败。

1.在云服务器导出数据

在云服务器登陆到自建数据库,执行如下命令。若提示 Password:,输入访问账号密码即可。
[root@VM-10-5-tencentos ~]# pg_dump -h*.*.*.* -p 5432 -U dbadmin -Fc testdb > testdb_bkp.dump
Password:
命令格式为:pg_dump -h <host> -p <port> -U <username> -Fc <dbname> <dumpdir>,更多使用指引请参考 pg_dump 官方文档
参数
说明
host
云数据库 PostgreSQL 实例的连接地址。
若云数据库与云服务器处于同一 VPC 下,推荐使用内网地址进行连接。
port
云数据库 PostgreSQL 实例的端口。
username
云数据库 PostgreSQL 实例的账号名称。
-Fc
输出格式,-Fc 格式适合 pg_restore 进行还原。
dbname
需要导出的 database 名称。
dumpdir
导出的备份文件的路径和名称。
执行下列命令,查看文件是否成功生成。
[root@VM-10-5-tencentos ~]#ll testdb_bkp.dump

2.在自建数据库上创建对应的 user

执行下列命令,连上目标端的自建数据库。
[postgres@VM-0-14-tencentos root]$ psql -h *.*.*.* -p5432 -U postgres -d postgres
psql (16.0)
Type "help" for help.

postgres=# \\du  
                             List of roles
 Role name |                         Attributes                        
-----------+------------------------------------------------------------
 postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
 postgres=#
需确保在目标数据库存在可用账号。如果尚无对应账号,可执行下列命令创建对应的数据库账号。
postgres=# CREATE USER dbadmin WITH PASSWORD '123456' SUPERUSER;
CREATE ROLE
postgres=# \\du
                             List of roles
 Role name |                         Attributes                        
-----------+------------------------------------------------------------
 dbadmin  | Superuser
 postgres | Superuser, Create role, Create DB, Replication, Bypass RLS

postgres=#
以上示例中创建的账号为 superuser ,您也可以基于自身需要进行 user 的系统权限和对象权限的赋权。

3.在自建数据库上创建对应的 database

如果在目标端使用了已经存在数据的 database 进行导入,则可能出现冲突而失败。因此建议您使用未存放任何数据的 database 进行数据恢复。若您已准备好database,则可跳过该步骤。
使用新创建的账号登录数据库。命令如下:
[postgres@VM-0-14-tencentos root]$ psql -h127.0.0.1 -p5432 -U dbadmin -d postgres
psql (16.0)
Type "help" for help.

postgres=# select datname from pg_database;
 datname 
-----------
 template1
 template0
 postgres
(3 rows)
执行下行命令,新建 database 用于存放要恢复的数据。
postgres=# create database testdb;
CREATE DATABASE
postgres=# select datname from pg_database;
 datname 
-----------
 template1
 template0
 postgres
 testdb
(4 rows)

4.在云服务器上恢复数据

执行命令,在自建数据库中恢复数据。
pg_restore -h *.*.*.* -p 5432 -U dbadmin -d testdb testdb_bkp.dump -c
命令格式为:pg_restore -h <host> -p <port> -U <username> -d <dbname> <dumpdir> -c ,更多使用指引请参考 pg_restore 官方文档
参数
说明
host
自建 PostgreSQL 实例的连接地址。
port
自建 PostgreSQL 实例的端口。
username
自建 PostgreSQL 实例的账号名称。
dbname
需要导入的 database 名称。
dumpdir
需要导入的备份文件的路径和名称。
-c
-c 为可选参数,表示在恢复数据之前先清空目标数据库中涉及的数据。

5.登录数据库查验数据

使用在自建数据库新建的账号登录数据库,并查看数据是否恢复。示例如下:
[postgres@VM-0-14-tencentos root]$ psql -h*.*.*.* -p5432 -Udbadmin -dpostgres
psql (16.0)
Type "help" for help.

postgres=# \\c testdb
You are now connected to database "testdb" as user "dbadmin".
testdb=#
可使用 select 查询语句查看数据。
testdb=# SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
 table_name
------------
 sbtest1
 sbtest10
 sbtest2
 sbtest3
 sbtest4
 sbtest5
 sbtest6
 sbtest7
 sbtest8
 sbtest9
(10 rows)

testdb=#
若数据与云数据库中的数据一致,则恢复成功。