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 ~]$