<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>惜分飞 &#187; pg备份还原</title>
	<atom:link href="https://www.xifenfei.com/tag/pg%e5%a4%87%e4%bb%bd%e8%bf%98%e5%8e%9f/feed" rel="self" type="application/rss+xml" />
	<link>https://www.xifenfei.com</link>
	<description>提供7*24专业数据库(Oracle,SQL Server,MySQL,PostgreSQL等)恢复和技术支持@Tel:17813235971</description>
	<lastBuildDate>Wed, 04 Nov 2026 15:33:30 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>pg_rman 备份还原测试</title>
		<link>https://www.xifenfei.com/2018/08/pg_rman_backup_restore.html</link>
		<comments>https://www.xifenfei.com/2018/08/pg_rman_backup_restore.html#comments</comments>
		<pubDate>Sun, 05 Aug 2018 15:32:17 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[pg_rman]]></category>
		<category><![CDATA[pg备份还原]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=7655</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2018/08/pg_rman_backup_restore.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2018/08/pg_rman_backup_restore.html" target="_blank">pg_rman 备份还原测试</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p>通过试验验证pg_rman的基础使用<br />
<strong>创建测试环境</strong><br />
模拟创建新表空间,新数据库,新用户,新schema,并且创建表插入测试数据，这样的环境下,pg_rman 备份还原效果</p>
<pre class="brush: sql; title: ; notranslate">
[root@localhost ~]# psql
Password: 
psql.bin (10.4)
Type &quot;help&quot; for help.

postgres=#  CREATE USER u_xifenfei WITH
postgres-#  LOGIN
postgres-#  SUPERUSER
postgres-#  CREATEDB
postgres-#  CREATEROLE
postgres-#  INHERIT
postgres-#  REPLICATION
postgres-#  CONNECTION LIMIT -1
postgres-#  PASSWORD 'xifenfei';
CREATE ROLE
postgres=#  CREATE TABLESPACE tbs_xifenfei
postgres-#    OWNER u_xifenfei
postgres-#    LOCATION '/opt/PostgreSQL/10/tbs_xifenfei';
CREATE TABLESPACE
postgres=#  CREATE DATABASE db_xifenfei
postgres-#      WITH
postgres-#      OWNER = u_xifenfei
postgres-#      ENCODING = 'UTF8'
postgres-#      TABLESPACE = tbs_xifenfei
postgres-#      CONNECTION LIMIT = -1;
CREATE DATABASE
postgres=# \q
[root@localhost ~]# psql -U u_xifenfei
Password for user u_xifenfei: 
psql.bin (10.4)
Type &quot;help&quot; for help.

postgres=# \q
[root@localhost ~]# psql -U u_xifenfei -d db_xifenfei
Password for user u_xifenfei: 
psql.bin (10.4)
Type &quot;help&quot; for help.

db_xifenfei=# 
db_xifenfei=# 
db_xifenfei=# create schema u_xifenfei;
CREATE SCHEMA
db_xifenfei=# create table t_xifenfei as select * from pg_tables;
SELECT 69
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 69
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 138
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 276
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 552
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 1104
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 2208
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 4416
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 8832
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 17664
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 35328
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 70656
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 141312
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 282624
db_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 565248
db_xifenfei=# select count(*) from t_xifenfei;
  count  
---------
 1130496
(1 row)

db_xifenfei=# 
</pre>
<p><strong>第一次全备数据库</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost backup]# pg_rman backup --backup-mode=full \
[root@localhost backup]# --backup-path=/backup -d db_xifenfei -U u_xifenfei -h 127.0.0.1
Password for user u_xifenfei: 
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
</pre>
<p><strong>模拟继续插入数据</strong><br />
t_xifenfei表一共有数据2260992条</p>
<pre class="brush: sql; title: ; notranslate">
b_xifenfei=# insert into t_xifenfei select * from t_xifenfei;
INSERT 0 1130496
db_xifenfei=# select count(*) from t_xifenfei;
  count  
---------
 2260992
(1 row)
</pre>
<p><strong>查看全备情况</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost 10]#  pg_rman show --backup-path=/backup
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2018-08-05 10:47:29  2018-08-05 10:47:43  FULL   611MB     1  DONE
</pre>
<p><strong>备份归档日志</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost 10]# pg_rman backup --backup-mode=archive \
[root@localhost 10]# --backup-path=/backup -d db_xifenfei -U u_xifenfei -h 127.0.0.1
Password for user u_xifenfei: 
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
</pre>
<p><strong>检查全备和归档备份情况</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost 10]#  pg_rman show --backup-path=/backup
=====================================================================
 StartTime           EndTime              Mode    Size   TLI  Status 
=====================================================================
2018-08-05 10:54:51  2018-08-05 10:56:07  ARCH   620MB     1  DONE
2018-08-05 10:47:29  2018-08-05 10:47:43  FULL   611MB     1  DONE
</pre>
<p><strong>停掉pg</strong></p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost data]# su - postgres
Last login: Sun Aug  5 02:19:57 EDT 2018 on pts/1
-bash-4.2$ source pg_env.sh 
-bash-4.2$ pg_ctl stop
waiting for server to shut down.... done
server stopped
-bash-4.2$ ps -ef|grep postgres
root      39902  30494  0 11:05 pts/0    00:00:00 su - postgres
postgres  39903  39902  0 11:05 pts/0    00:00:00 -bash
postgres  40021  39903  0 11:06 pts/0    00:00:00 ps -ef
postgres  40022  39903  0 11:06 pts/0    00:00:00 grep --color=auto postgres
</pre>
<p><strong>删除原库并创建相关目录</strong><br />
注意:对应的空间目录权限为700,所有者和组为postgres</p>
<pre class="brush: bash; title: ; notranslate">
[root@localhost 10]# pwd
/opt/PostgreSQL/10
[root@localhost 10]# mv data data_bak
[root@localhost 10]# mv tbs_xifenfei tbs_xifenfei_bak
[root@localhost 10]# mkdir data
[root@localhost 10]# mkdir tbs_xifenfei
[root@localhost 10]# chmod 700 data
[root@localhost 10]# chmod 700 tbs_xifenfei
[root@localhost 10]# chown postgres:postgres data
[root@localhost 10]# chown postgres:postgres tbs_xifenfei
</pre>
<p><strong>还原数据库</strong></p>
<pre class="brush: bash; title: ; notranslate">
-bash-4.2$ pg_rman restore --backup-path=/backup
WARNING: pg_controldata file &quot;/opt/PostgreSQL/10/data/global/pg_control&quot; does not exist
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of latest full backup as recovery target: 1
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: &quot;2018-08-05 10:47:29&quot;
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: &quot;2018-08-05 10:47:29&quot; backup and archive log files by SIZE
INFO: backup &quot;2018-08-05 10:47:29&quot; is valid
INFO: restoring database files from the full mode backup &quot;2018-08-05 10:47:29&quot;
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup &quot;2018-08-05 10:47:29&quot; is valid
INFO: restoring WAL files from backup &quot;2018-08-05 10:47:29&quot;
INFO: restoring online WAL files and server log files
INFO: generating recovery.conf
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.
</pre>
<p><strong>启动pg</strong></p>
<pre class="brush: bash; title: ; notranslate">
-bash-4.2$ pg_ctl start
waiting for server to start....2018-08-05 11:23:40.190 EDT [40855] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
2018-08-05 11:23:40.190 EDT [40855] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
2018-08-05 11:23:40.193 EDT [40855] LOG:  listening on Unix socket &quot;/tmp/.s.PGSQL.5432&quot;
2018-08-05 11:23:40.200 EDT [40855] LOG:  redirecting log output to logging collector process
2018-08-05 11:23:40.200 EDT [40855] HINT:  Future log output will appear in directory &quot;log&quot;.
 done
server started

-bash-4.2$ ps -ef|grep postgres
root      40328  21806  0 11:14 pts/3    00:00:00 su - postgres
postgres  40329  40328  0 11:14 pts/3    00:00:00 -bash
postgres  40855      1  0 11:23 pts/3    00:00:00 /opt/PostgreSQL/10/bin/postgres
postgres  40856  40855  0 11:23 ?        00:00:00 postgres: logger process   
postgres  40857  40855 57 11:23 ?        00:00:16 postgres: startup process   waiting for 000000010000000000000025
postgres  40859  40855  0 11:23 ?        00:00:00 postgres: checkpointer process  
postgres  40860  40855  7 11:23 ?        00:00:02 postgres: writer process   
postgres  40862  40855  0 11:23 ?        00:00:00 postgres: stats collector process  
postgres  40892  40329  0 11:24 pts/3    00:00:00 ps -ef
postgres  40893  40329  0 11:24 pts/3    00:00:00 grep --color=auto postgres
-bash-4.2$ 
</pre>
<p><strong>验证数据库还原结果</strong></p>
<pre class="brush: sql; title: ; notranslate">
-bash-4.2$ psql -U u_xifenfei -d db_xifenfei
Password for user u_xifenfei: 
psql.bin (10.4)
Type &quot;help&quot; for help.

db_xifenfei=#  select count(*) from t_xifenfei;
  count  
---------
 2260992
(1 row)

db_xifenfei=# 
</pre>
<p>破坏环境之前表条数和还原之后完全匹配,证pg_rman在功能上备份恢复没有问题</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/pg-tablespace-database-schema.html" rel="bookmark" class="crp_title">PostgreSQL的表空间、数据库、用户之间的关系</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/postgresql-schema.html" rel="bookmark" class="crp_title">PostgreSQL模式理解</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/postgresql-archive_mode.html" rel="bookmark" class="crp_title">PostgreSQL修改归档模式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2018/08/psql-fatal-no-pg_hba-conf-entry-for-host.html" rel="bookmark" class="crp_title">psql: FATAL:  no pg_hba.conf entry for host</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2022/05/pg-wal-lost-recovery.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:wal日志丢失恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/04/pg-createdb-gbk.html" rel="bookmark" class="crp_title">pg创建gbk字符集库</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/pg_control-rebuild.html" rel="bookmark" class="crp_title">pg_control丢失/损坏处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2024/04/postgresql-walminer.html" rel="bookmark" class="crp_title">PostgreSQL解析wal日志之&#8212;walminer</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2024/04/pg_filedump_batch.html" rel="bookmark" class="crp_title">PostgreSQL恢复系列:pg_filedump批量处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/09/pg-truncate-recovery.html" rel="bookmark" class="crp_title">Postgres数据库truncate表无有效备份恢复</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2025/03/invalid-checkpoint-record.html" rel="bookmark" class="crp_title">pg启动报invalid checkpoint record处理</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2015/06/%e5%9c%a8linux%e4%b8%8a%e5%ae%89%e8%a3%85postgresql-9-4%e5%b9%b6%e5%90%af%e5%8a%a8%e5%92%8c%e5%85%b3%e9%97%ad%e6%95%b0%e6%8d%ae%e5%ba%93.html" rel="bookmark" class="crp_title">在linux上安装PostgreSQL 9.4并启动和关闭数据库</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2018/08/pg_rman_backup_restore.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
