[postgres@lxm ~]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# create database test template template0;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table test( id int primary key , name varchar);
CREATE TABLE
test=# insert into test select n, n
test-# ||'name' from generate_series(1,10) n;
INSERT 0 10
test=# select * from test;
id | name
----+--------
1 | 1name
2 | 2name
3 | 3name
4 | 4name
5 | 5name
6 | 6name
7 | 7name
8 | 8name
9 | 9name
10 | 10name
(10 rows)
test=# create table test2 (like test including all);
CREATE TABLE
test=# \d+ test2
Table "public.test2"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+-------------------+-----------+----------+---------+----------+--------------+-------------
id | integer | | not null | | plain | |
name | character varying | | | | extended | |
Indexes:
"test2_pkey" PRIMARY KEY, btree (id)
Access method: heap
test=# insert into test2 select * from test;
INSERT 0 10
test=# select count(*) from test2;
count
-------
10
(1 row)
test=#
test=# \q
[postgres@lxm test2]$ pg_dump -U postgres -Ft -f test_tar.dmp -C -c --if-exists test
Password:
[postgres@lxm test2]$ ls
test_tar.dmp
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# drop database test;
DROP DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \q
[postgres@lxm test2]$ pg_restore -U postgres -d postgres -C -c --if-exists test_tar.dmp
Password:
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | test | table | postgres
public | test2 | table | postgres
(2 rows)
test=# select count(*) from test;
count
-------
10
(1 row)
test=# select count(*) from test2;
count
-------
10
(1 row)
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# drop database test;
DROP DATABASE
postgres=# \q
[postgres@lxm t
生成 toc 文件
[postgres@lxm test2]$ pg_restore -U postgres -l -f test.toc test_tar.dmp
[postgres@lxm test2]$ ls
test_tar.dmp test.toc
将 test2相关代码注释掉
[postgres@lxm test2]$ more test.toc
;
; Archive created at 2020-06-16 12:43:38 CST
; dbname: test
; TOC Entries: 10
; Compression: 0
; Dump Version: 1.14-0
; Format: TAR
; Integer: 4 bytes
; Offset: 8 bytes
; Dumped from database version: 13beta1
; Dumped by pg_dump version: 13beta1
;
;
; Selected TOC Entries:
;
200; 1259 16545 TABLE public test postgres
201; 1259 16551 TABLE public test2 postgres
3076; 0 16545 TABLE DATA public test postgres
3077; 0 16551 TABLE DATA public test2 postgres
2945; 2606 16558 CONSTRAINT public test2 test2_pkey postgres
2943; 2606 16560 CONSTRAINT public test test_pkey postgres
[postgres@lxm test2]$
[postgres@lxm test2]$ vim test.toc
[postgres@lxm test2]$
[postgres@lxm test2]$ more test.toc
;
; Archive created at 2020-06-16 12:43:38 CST
; dbname: test
; TOC Entries: 10
; Compression: 0
; Dump Version: 1.14-0
; Format: TAR
; Integer: 4 bytes
; Offset: 8 bytes
; Dumped from database version: 13beta1
; Dumped by pg_dump version: 13beta1
;
;
; Selected TOC Entries:
;
200; 1259 16545 TABLE public test postgres
;201; 1259 16551 TABLE public test2 postgres
3076; 0 16545 TABLE DATA public test postgres
;3077; 0 16551 TABLE DATA public test2 postgres
;2945; 2606 16558 CONSTRAINT public test2 test2_pkey postgres
2943; 2606 16560 CONSTRAINT public test test_pkey postgres
[postgres@lxm test2]$
恢复 数据库 test 以及 表 test
[postgres@lxm test2]$ pg_restore -U postgres -d postgres -Ft -L test.toc -C -c --if-exists test_tar.dmp
Password:
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
test=# select count(*) from test;
count
-------
10
(1 row)
test=# select * from test;
id | name
----+--------
1 | 1name
2 | 2name
3 | 3name
4 | 4name
5 | 5name
6 | 6name
7 | 7name
8 | 8name
9 | 9name
10 | 10name
(10 rows)
test=#
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# drop database test;
DROP DATABASE
postgres=# \q
[postgres@lxm test2]$ pg_restore -U postgres -d postgres -Ft -t test -C -c --if-exists test_tar.dmp
Password:
[postgres@lxm test2]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
test=# \d+ test
Table "public.test"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+-------------------+-----------+----------+---------+----------+--------------+-------------
id | integer | | not null | | plain | |
name | character varying | | | | extended | |
Access method: heap
test=# select count(*) from test;
count
-------
10
(1 row)
[postgres@lxm test_function]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# create database test template template0;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table test(id int primary key , name varchar);
CREATE TABLE
test=# insert into test values(1,'a'),(2,'b');
INSERT 0 2
test=# CREATE OR REPLACE FUNCTION func_test()
test-# RETURNS integer
test-# AS
test-# $BODY$
test$# declare
test$# BEGIN
test$#
test$# END;
test$# $BODY$ LANGUAGE plpgsql;
CREATE FUNCTION
test=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-----------+------------------+---------------------+------
public | func_test | integer | | func
(1 row)
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
test=# \q
[postgres@lxm test_function]$ ls
test_t.dmp
[postgres@lxm test_function]$ rm -f test_t.dmp
[postgres@lxm test_function]$ pg_dump -U postgres -Ft -f test_t.dmp test
Password:
[postgres@lxm test_function]$ ls
test_t.dmp
[postgres@lxm test_function]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# drop database test;
DROP DATABASE
postgres=# \q
[postgres@lxm test_function]$ pg_restore -Upostgres -d postgres -t test -P func_test\(\) -C -c --if-exists test_t.dmp
Password:
[postgres@lxm test_function]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-----------+------------------+---------------------+------
public | func_test | integer | | func
(1 row)
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
test=# \q
[postgres@lxm test_function]$
[postgres@lxm test_function]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+------+------------------+---------------------+------
(0 rows)
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
test=# create table test2 (like test including all);
CREATE TABLE
test=#
test=# CREATE OR REPLACE FUNCTION func_add(a int, b int)
test-# RETURNS integer
test-# AS
test-# $BODY$
test$# declare
test$# BEGIN
test$# return $1+$2;
test$# END;
test$# $BODY$ LANGUAGE plpgsql;
CREATE FUNCTION
test=#
test=# CREATE OR REPLACE FUNCTION func_test()
test-# RETURNS integer
test-# AS
test-# $BODY$
test$# declare
test$# BEGIN
test$#
test$# END;
test$# $BODY$ LANGUAGE plpgsql;
CREATE FUNCTION
test=# \q
备份
[postgres@lxm test_function]$ pg_dump -U postgres -Ft -f test_tar.dmp test
生成 toc文件
[postgres@lxm test_function]$ pg_restore -Upostgres -l -f test.toc test_tar.dmp
[postgres@lxm test_function]$ ls
test_tar.dmp test.toc
[postgres@lxm test_function]$ more test.toc
;
; Archive created at 2020-06-16 13:54:24 CST
; dbname: test
; TOC Entries: 10
; Compression: 0
; Dump Version: 1.14-0
; Format: TAR
; Integer: 4 bytes
; Offset: 8 bytes
; Dumped from database version: 13beta1
; Dumped by pg_dump version: 13beta1
;
;
; Selected TOC Entries:
;
202; 1255 16700 FUNCTION public func_add(integer, integer) postgres
203; 1255 16701 FUNCTION public func_test() postgres
200; 1259 16688 TABLE public test postgres
201; 1259 16694 TABLE public test2 postgres
3074; 0 16688 TABLE DATA public test postgres
3075; 0 16694 TABLE DATA public test2 postgres
删除 test 数据库
[postgres@lxm test_function]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# drop database if exists test;
DROP DATABASE
postgres=# \q
恢复
[postgres@lxm test_function]$ pg_restore -Upostgres -d postgres -L test.toc -C -c --if-exists test_tar.dmp
Password:
[postgres@lxm test_function]$ psql
Password for user postgres:
psql (13beta1)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | C | C |
(4 rows)
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row)
test=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+----------+------------------+----------------------+------
public | func_add | integer | a integer, b integer | func
(1 row)
根据实际测试体验,发现通过-P恢复带有参数的函数,没有恢复成功,可能是我语法哪里有问题,相比较而言如果只是恢复指定的带有参数的函数可以通过生成并编辑toc文件来实现比较方便;