pg_dump基于二进制格式的部分表的恢复

在PostgreSQL中,使用pg_dump进行二进制格式备份时可以指定schema和表名,但pg_restore恢复时需分开用-n和-t参数。在测试中发现,直接用-t指定带schema的表名无法正确恢复数据,必须拆分成-n指定schema和-t指定表名来恢复单个表。
摘要由CSDN通过智能技术生成

pg_dump做基于二进制格式的备份时,可以用-t指定表名的时候顺便指定schema, 例如pg_dump -t test1.test1 -t test2.test1 -Fc -f tmp.dmp
但在做pg_restore恢复时-t不能指定schema,否则恢复不出来数据,如果要精准恢复单个表,需要拆开成-n和-t。

数据库版本:

PostgreSQL14.5

测试过程如下:

[postgres@duqk04 ~]$ psql
psql (14.5)
Type “help” for help.

postgres=# create schema myschm1;
CREATE SCHEMA
postgres=# create schema myschm2;
CREATE SCHEMA
postgres=# create table myschm1.t1(id int,name varchar(20));
CREATE TABLE
postgres=# create table myschm2.t2(id int,name varchar(20));
CREATE TABLE
postgres=# insert into myschm1.t1 values(1,‘a’);
INSERT 0 1
postgres=# insert into myschm2.t2 values(2,‘b’);
INSERT 0 1
postgres=# \q
[postgres@duqk04 ~]$ pg_dump -F c -d postgres -t myschm1.t1 -t myschm2.t2 -f mydata.dmp
[postgres@duqk04 ~]$ psql
psql (14.5)
Type “help” for help.

postgres=# create database testdb;
CREATE DATABASE
postgres=# \c testdb
You are now connected to database “testdb” as user “postgres”.
testdb=# create schema myschm1;
CREATE SCHEMA
testdb=# \q
[postgres@duqk04 ~]$

—尝试通过myschm1.t1的方式恢复
[postgres@duqk04 ~]$ pg_restore -d testdb -t myschm1.t1 mydata.dmp
[postgres@duqk04 ~]$ psql -d testdb
psql (14.5)
Type “help” for help.

testdb=# select from myschm1.t1;
ERROR: relation “myschm1.t1” does not exist
LINE 1: select from myschm1.t1;
^
testdb=# \d myschm1.
Did not find any relation named “myschm1.”.
testdb=# \q
[postgres@duqk04 ~]$

—尝试通过myschm1.t1加上指定schema的方式恢复
[postgres@duqk04 ~]$ pg_restore -d testdb -n myschm1 -t myschm1.t1 mydata.dmp
[postgres@duqk04 ~]$ psql -d testdb
psql (14.5)
Type “help” for help.

testdb=# \d myschm1.
Did not find any relation named “myschm1.”.
testdb=# select from myschm1.t1;
ERROR: relation “myschm1.t1” does not exist
LINE 1: select from myschm1.t1;
^
testdb=# \q
[postgres@duqk04 ~]$

—尝试通过-n和-t 分别指定schema名和表名的方式恢复
[postgres@duqk04 ~]$ pg_restore -d testdb -n myschm1 -t t1 mydata.dmp
[postgres@duqk04 ~]$ psql -d testdb
psql (14.5)
Type “help” for help.

testdb=# \d myschm1.*
Table “myschm1.t1”
Column | Type | Collation | Nullable | Default
--------±----------------------±----------±---------±--------
id | integer | | |
name | character varying(20) | | |

testdb=# select * from myschm1.t1;
id | name
----±-----
1 | a
(1 row)

testdb=# \q
[postgres@duqk04 ~]$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值