PostgreSql pg_restore usage

I. Overview

pg_restore is a utility for restoring PostgreSQL databases from non-text files created by pg_dump.

2. Grammar

pg_restore [connection-option] [option] filename

connection-option Optional parameters:

-h host or --host=host : connection address.
-p port or --port=port : connection port number.
-U username or --username=username : connect user.
-w or --no-password : Do not enter a password, this option is useful for batch tasks and scripts.
-W or --password : Force a password, in some cases it is worth typing -W to avoid an extra connection attempt.
–role=rolename : -U specifies that when the user authority is not enough to restore data, other role authority can be set to assist recovery.

option Optional parameters:

-a or --data-only : only restore data, not restore data definitions, large objects and sequence values ​​will also be restored.
-c or --clean : Clean up database objects before recreating them.
-C or --create : Create a database before restoring it. If --clean is also specified, rebuild the target database before connecting to it.
-d dbname or --dbname=dbname : connect to the database dbname and restore directly to the database.
-e or --exit-on-error : Exit when an error is encountered during recovery. By default, an error count is displayed at the end of the recovery.
-f filename or --file=filename : specify the output file for generated scripts, or for listings when used with the -l option
-F format or --format=format : specify the format of the archive.
-I index or --index=index : Only restore the definition of the mentioned index, you can specify multiple indexes by writing multiple -I switches.
-j number-of-jobs or --jobs=number-of-jobs : Run pg_restore with concurrent jobs, this option only supports custom and directory archive formats, the input must be a regular file or directory.
-l or --list : A table listing the contents of the archive. The output of this operation can be used as input to the -L option. Note that if filter switches such as -n or -t are used with -l, they will limit the items listed.
-L list-file or --use-list=list-file : Restore only the archive elements listed in list-file, and restore them in the order they appear in the file. Note that if filter switches such as -n or -t are used with -L, they will further limit the items to be restored.
-n shcema or --schema=schema : only restore objects in the mentioned schema. Multiple -n switches can be used to specify multiple modes. This can be combined with the -t option to restore only one specified table.
-N schema or --exclude-schema=schema : Do not restore objects in the mentioned schema. Multiple -N switches can be used to specify multiple solutions to be excluded, with a higher priority than -n.
-O or --no-owner : Exclude the original library owner when restoring.
-P function-name(argtype [, …]) or –function=function-name(argtype [, …]) : restore only the mentioned functions, multiple -P switches can be used to specify multiple functions.
-s or --schema-only : Only restore the data definition, not the data.
-S username or --superuser=username : Specifies the superuser name to use when disabling the trigger. This option is only relevant when using --disable-triggers.
-t table or --table=table : Restore only the mentioned table, "table" includes views, materialized views, sequences and external tables.
-T trigger or --trigger=trigger: restore only the mentioned triggers, multiple triggers can be specified with multiple -T switches.
-v or --verbose : Specifies verbose mode.
-V or --version : Print the version of this pg_restore and exit.
-x or --no-privileges or --no-acl : prevent restoration of access privileges (grant/revoke commands).
-1 or --single-transaction : Perform recovery as a single transaction (that is, wrap issued commands in BEGIN/COMMIT), this option implies --exit-on-error.
--disable-triggers : This option is only relevant when performing a data-only restore. It instructs pg_restore to execute commands to temporarily disable triggers on the target table while loading data. Currently, commands issued for --disable-triggers must be done as superuser. Therefore you should also specify a superuser name with -S, or better yet run pg_restore as a PostgreSQL superuser.
--enable-row-security : This option is only relevant when restoring the contents of a table with row security. By default, pg_restore will set row_security to off to ensure that all data is restored to the table. Note that currently this option also requires the dump to be in INSERT format, since COPY FROM does not support row safety.
–if-exists : Use conditional commands (that is, add an IF EXISTS clause) to delete database objects. This option is only valid if --clean is specified.
--no-comments : Do not output commands to restore comments.
--no-data-for-failed-tables : By default, table data will be restored even if a table creation command fails (e.g. because the table already exists). With this option, data for such tables will be skipped. This option is only valid when restoring directly to a database, it has no effect when generating SQL script output.
--no-publications : Do not output the command to restore publications.
--no-security-labels : Do not output commands to restore security labels.
--no-subscriptions : Do not output commands to resume subscriptions.
--no-tablespaces : Do not output command selection tablespaces. With this option, all objects will be created in the default tablespace during recovery.
--section=sectionname : Restore only mentioned sections. Section names can be pre-data, data, or post-data. This option can be specified multiple times to select multiple subsections. The default is to restore all subsections. The data section contains the actual table data and large object definitions. Post-data items consist of index definitions, triggers, rules, and constraints other than validated check constraints. Pre-data items consist of all other data definition items.
--strict-names : Require each schema (-n/--schema) and table (-t/--table) qualifier to match at least one schema/table in the backup file.
--use-set-session-authorization: Output the SQL standard SET SESSION AUTHORIZATION command instead of the ALTER OWNER command to determine object ownership. This would make the dump more standards-compliant, but depending on the history of the objects in the dump, it might not restore correctly.

3. Examples

--备份数据库
pg_dump -Fc -d test > test.dump

--删除数据库
dropdb test

--恢复到原数据库
pg_restore test.dump -d highgo -C

--恢复到其他数据库
pg_restore test.dump -d ddd

--dump二进制文件转为sql文本文件
pg_restore test.dump -C -f test.sql

--dump二进制文件转为列表文本
pg_restore -l test.dump > test.list

--编辑列表文本,排除恢复对象恢复(对应的对象行首加;即为排除该对象)
pg_restore -L test.list test.dump -C -d highgo

如排除表 sssss 恢复:
[highgo@dj ~]$ more test.list
;
; Archive created at 2022-02-23 17:31:42 CST
;     dbname: test
;     TOC Entries: 14
;     Compression: -1
;     Dump Version: 1.13-0
;     Format: CUSTOM
;     Integer: 4 bytes
;     Offset: 8 bytes
;     Dumped from database version: 10.6
;     Dumped by pg_dump version: 10.6
;
;
; Selected TOC Entries:
;
3713; 1262 16441 DATABASE - test highgo
2; 3079 13205 EXTENSION - oraftops
3714; 0 0 COMMENT - EXTENSION oraftops
7; 2615 2200 SCHEMA - public highgo
3715; 0 0 COMMENT - SCHEMA public highgo
1; 3079 13200 EXTENSION - plpgsql
3716; 0 0 COMMENT - EXTENSION plpgsql
3; 3079 13843 EXTENSION - alter_pg_func
3717; 0 0 COMMENT - EXTENSION alter_pg_func
;211; 1259 16442 TABLE public sssss highgo
;3707; 0 16442 TABLE DATA public sssss highgo

Guess you like

Origin blog.csdn.net/songyundong1993/article/details/129789134