使用PostgreSQL这么多年后有时吃惊的一些实用工具,在某种程度上,有能力仍相对不知名的几人。让我给你介绍体现。
这两个pg_dump和pg_restore开关使我们感兴趣的区域在特定对象如函数、表、数据等等,当我们创建和恢复逻辑转储。但总有一天在表达这种粒度时作为参数可以变得过于复杂,当我们看几十,如果不是数以百计,对象。
输入清单
PostgreSQL转储商店不仅数据库对象和数据的描述性元数据了。当生成一个文本文件,一个PostgreSQL清单一行上的每个对象的细节。这些线可以编辑通过移除或重新排序。编辑文件可以使用pg_restore恢复实体规定的顺序。
这是它是如何工作的;第一步是使用自定义压缩参数生成标准的转储足球俱乐部
|
1
2
|
#生成逻辑转储
pg_dump
- - - - - -
C
- - - - - -
足球俱乐部
mydatabase
>
mydatabase
.db
|
一旦创建了转储清单使用pg_restore和生成- l到标准输出的开关:
|
1
2
|
#生成清单,元数据,原文如此描述逻辑的转储
pg_restore
- - - - - -
l
mydatabase
.db
>
mydatabase_manifest
. ini
|
就像一个食谱,清单用于阅读和恢复转储以下方向:
|
1
2
|
#恢复使用manifest转储
pg_restore
- - - - - -
l
mydatabase_manifest
. ini
- - - - - -
d
postgres
mydatabase
.db
|
当然,在生成和阅读清单,需要编辑它的机会:
让我们用一个更实际的。pgbench。第一步初始化数据库基准测试,注意使用外键:
|
1
2
3
|
dropdb
- - -
如果
- - - - - -
存在
pgbench
createdb
pgbench
pgbench
- - - - - -
我
- - -
外国
- - - - - -
键
- - - - - -
年代
50
pgbench
|
让我们复杂景观包括通过添加一些看法物化观点:
|
1
2
3
4
5
6
|
psql
pgbench
< <
_eof_
创建
或
取代
视图
v1_pgbench_accounts
作为
选择
*
从
pgbench_accounts
;
创建
或
取代
视图
v2_pgbench_accounts
作为
选择
*
从
pgbench_accounts
;
创建
或
取代
视图
v3_pgbench_accounts
作为
选择
*
从
pgbench_accounts
;
创建
物化
视图
m_pgbench_accounts
作为
选择
*
从
v1_pgbench_accounts
订单
通过
随机
(
)
限制
1 e3
;
_eof_
|
现在让我们创建数据库转储。记住,体现只能创建时生成转储使用自定义格式开关足球俱乐部。
|
1
|
pg_dump
- - - - - -
足球俱乐部
- - - - - -
C
pgbench
>
pgbench
.db
|
现在,让我们来生成清单。我选择使用ini扩展文件名作为我的代码编辑器识别分号作为相应的注释行和颜色:
|
1
|
pg_restore
- - - - - -
C
- - - - - -
l
pgbench
.db
>
pgbench_manifest
. ini
|
按照正常的生成逻辑转储和恢复的过程,认为从下面列出的输出命令创建数据库,从- c开关,紧随其后的是视图、表定义数据的人口,和约束,并完成数据人口的物化视图:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20.
21
22
23
24
25
26
27
28
29日
30.
31日
32
33
34
35
36
37
|
;
;档案在2022-08-24 10:21:46 PDT创建的
;dbname: pgbench
;TOC条目:25
;压缩:1
;转储文件版本:1.14 0
;格式:自定义
;整数:4字节
;抵消:8个字节
;抛售从数据库版本:14.0
;甩了pg_dump版本:14.0
;
;
;选择TOC条目:
;
3361年
;1262 25332 - pgbench postgres数据库
211年
;1259 25339表公共pgbench_accounts postgres
213年
;1259 25380查看公共v1_pgbench_accounts postgres
216年
;1259 25392公共m_pgbench_accounts postgres物化视图
212年
;1259 25342表公共pgbench_branches postgres
209年
;1259 25333表公共pgbench_history postgres
210年
;1259 25336表公共pgbench_tellers postgres
214年
;1259 25384查看公共v2_pgbench_accounts postgres
215年
;1259 25388查看公共v3_pgbench_accounts postgres
3353年
;0 25339表数据公共pgbench_accounts postgres
3354年
;0 25342表数据公共pgbench_branches postgres
3351年
;0 25333表数据公共pgbench_history postgres
3352年
;0 25336表数据公共pgbench_tellers postgres
3200年
;2606 25354约束公共pgbench_accounts pgbench_accounts_pkey postgres
3202年
;2606 25350约束公共pgbench_branches pgbench_branches_pkey postgres
3198年
;2606 25352约束公共pgbench_tellers pgbench_tellers_pkey postgres
3207年
;2606 25360颗约束公共pgbench_accounts pgbench_accounts_bid_fkey postgres
3205年
;2606 25375颗约束公共pgbench_history pgbench_history_aid_fkey postgres
3203年
;2606 25365颗约束公共pgbench_history pgbench_history_bid_fkey postgres
3204年
;2606 25370颗约束公共pgbench_history pgbench_history_tid_fkey postgres
3206年
;2606 25355颗约束公共pgbench_tellers pgbench_tellers_bid_fkey postgres
3355年
;0 25392物化视图数据公共m_pgbench_accounts postgres
|
一旦生成,体现现在可以根据我们的要求,在这种情况下是恢复数据库的一个子集转储文件如下:
- 创建数据库pgbench_restore
- 只恢复视图v1_pgbench_accounts
- 恢复和填充表pgbench_accounts
- 恢复和填充物化视图m_pgbench_accounts
您会注意到,为了恢复表pgbench_accounts有必要不加约束,即把颗约束。
一些编辑后生成的清单是什么样子:
|
1
2
3
4
5
6
|
211年
;1259 25339表公共pgbench_accounts postgres
213年
;1259 25380查看公共v1_pgbench_accounts postgres
216年
;1259 25392公共m_pgbench_accounts postgres物化视图
3353年
;0 25339表数据公共pgbench_accounts postgres
3200年
;2606 25354约束公共pgbench_accounts pgbench_accounts_pkey postgres
3355年
;0 25392物化视图数据公共m_pgbench_accounts postgres
|
小创意并不是大的飞跃动态生成适当的清单:
|
1
2
3
|
GREP
=
“表公共pgbench_accounts |查看公共v1_pgbench_accounts |物化|数据公开pgbench_accounts | pgbench_accounts_pkey |数据公共m_pgbench_accounts”
pg_restore
- - - - - -
C
- - - - - -
l
pgbench
.db
|
grep
- - - - - -
E
" $ GREP "
>
pgbench_manifest
. ini
|
现在我们进行最后的恢复步骤:
|
1
2
3
4
5
6
7
8
9
10
11
|
dropdb
- - -
如果
- - - - - -
存在
pgbench_restore
createdb
pgbench
_恢复
#
#看看生成的SQL之前上传的逻辑转储
#
pg_restore
- - - - - -
1
- - - - - -
l
pgbench_manifest
. ini
- - - - - -
f
- - - - - -
|
少
- - - - - -
年代
#
#逻辑转储现在可以上传
#
pg_restore
- - - - - -
1
- - - - - -
l
pgbench_manifest
. ini
- - - - - -
d
pgbench_restore
pgbench
.db
|
看看结果数据库成功确认:
|
1
|
psql
pgbench_restore
- - - - - -
c
“\ d”
|
|
1
2
3
4
5
6
|
列表
的
关系
模式
|
的名字
|
类型
|
老板
|
持久性
|
访问
方法
|
大小
- - -
- - -
- - -
- - -
+
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - - - - -
+
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - - - - -
+
- - -
- - -
- - -
- - -
- - -
+
- - -
- - -
- - -
- - -
- - -
- - -
- - - - - -
+
- - -
- - -
- - -
- - -
- - -
- - -
- - -
- - - - - -
+
- - -
- - -
- - -
- - -
- - - - - -
公共
|
m_pgbench_accounts
|
物化
视图
|
postgres
|
永久
|
堆
|
144年
kB
|
公共
|
pgbench_accounts
|
表
|
postgres
|
永久
|
堆
|
640年
MB
|
公共
|
v1_pgbench_accounts
|
视图
|
postgres
|
永久
|
|
0
字节
|
|
这就是所有!
最后一个警告:一个编辑manifest通过删除整行。不可能自己编辑行,所以不要改变什么,也没有新行可以任意添加到清单中。记住,表现就像指针,它们只能引用什么已经存在于数据库转储。






谢谢你的文章。我总是将使用参数与pg_dump每次fc,现在。
但我想,我找到了一个小错误。
“pg_restore 1 - l pgbench_manifest。ini - f - | - s”
失踪的转储文件。
解决办法:
“pg_restore 1 - l pgbench_manifest。ini - f - pgbench。db | - s”
你好,弗兰克,
我很高兴这是你可以使用。
我所犯的错误并没有解释这条线是我用我的转储是什么样子的管道少。
我爱“少”。