PostgreSQL自带的命令行工具12- pg_dumpall
基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777
pg_dumpall
是 PostgreSQL 提供的一个命令行工具,用于备份一个 PostgreSQL 数据库服务器上的所有数据库。与 pg_dump
不同的是,pg_dumpall
不仅备份数据库数据,还包括全部全局对象,如角色(用户)、表空间等,这使得它非常适合全面备份整个 PostgreSQL 实例。
通过help查看帮助文档。
[pg16@test ~]$ pg_dumpall --help
pg_dumpall extracts a PostgreSQL database cluster into an SQL script file.
Usage:
pg_dumpall [OPTION]...
General options:
-f, --file=FILENAME output file name
-v, --verbose verbose mode
-V, --version output version information, then exit
--lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
-?, --help show this help, then exit
Options controlling the output content:
-a, --data-only dump only the data, not the schema
-c, --clean clean (drop) databases before recreating
-E, --encoding=ENCODING dump the data in encoding ENCODING
-g, --globals-only dump only global objects, no databases
-O, --no-owner skip restoration of object ownership
-r, --roles-only dump only roles, no databases or tablespaces
-s, --schema-only dump only the schema, no data
-S, --superuser=NAME superuser user name to use in the dump
-t, --tablespaces-only dump only tablespaces, no databases or roles
-x, --no-privileges do not dump privileges (grant/revoke)
--binary-upgrade for use by upgrade utilities only
--column-inserts dump data as INSERT commands with column names
--disable-dollar-quoting disable dollar quoting, use SQL standard quoting
--disable-triggers disable triggers during data-only restore
--exclude-database=PATTERN exclude databases whose name matches PATTERN
--extra-float-digits=NUM override default setting for extra_float_digits
--if-exists use IF EXISTS when dropping objects
--inserts dump data as INSERT commands, rather than COPY
--load-via-partition-root load partitions via the root table
--no-comments do not dump comments
--no-publications do not dump publications
--no-role-passwords do not dump passwords for roles
--no-security-labels do not dump security label assignments
--no-subscriptions do not dump subscriptions
--no-sync do not wait for changes to be written safely to disk
--no-table-access-method do not dump table access methods
--no-tablespaces do not dump tablespace assignments
--no-toast-compression do not dump TOAST compression methods
--no-unlogged-table-data do not dump unlogged table data
--on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands
--quote-all-identifiers quote all identifiers, even if not key words
--rows-per-insert=NROWS number of rows per INSERT; implies --inserts
--use-set-session-authorization
use SET SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership
Connection options:
-d, --dbname=CONNSTR connect using connection string
-h, --host=HOSTNAME database server host or socket directory
-l, --database=DBNAME alternative default database
-p, --port=PORT database server port number
-U, --username=NAME connect as specified database user
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
--role=ROLENAME do SET ROLE before dump
If -f/--file is not used, then the SQL script will be written to the standard
output.
Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>
基本用法
pg_dumpall > outputfile.sql
这个命令会将所有数据库的备份保存到 outputfile.sql
文件中。这个文件是一个纯文本文件,包含了恢复所有数据库和全局对象所需的 SQL 命令。
常用选项
-f
,--file=文件名
: 将输出直接写入指定的文件,而不是标准输出。-g
,--globals-only
: 仅备份全局对象(如角色和表空间),不包括数据库数据。-d
,--dbname=连接字符串
: 通过指定的连接字符串连接到数据库,用于备份。-h
,--host=主机名
: 指定服务器的主机名,用于连接。-p
,--port=端口号
: 指定服务器端口号,用于连接。-U
,--username=用户名
: 指定连接数据库的用户名。-W
,--password
: 提示输入密码进行连接。-v
,--verbose
: 显示详细的操作信息。
示例1
备份所有数据库到文件
[pg16@test backup]$ pg_dumpall > /home/pg16/backup/all_database_20240506.sql
[pg16@test backup]$ ll
total 106812
-rw-rw-r-- 1 pg16 pg16 109353517 May 5 21:35 all_database_20240506.sql
这命令会将所有数据库和全局对象备份到 all_database_20240506.sql
文件中。
示例2
仅备份全局对象
[pg16@test backup]$ pg_dumpall --globals-only > /home/pg16/backup/global_objects_20240506.sql
[pg16@test backup]$ ll
total 106816
-rw-rw-r-- 1 pg16 pg16 833 May 5 21:37 global_objects_20240506.sql
这命令会将所有全局对象(不包括数据库数据)备份到 global_objects_20240506.sql
文件中。
注意事项
pg_dumpall
要求能够连接到 PostgreSQL 数据库的所有数据库,因此执行此命令的用户需要有相应的权限。- 与
pg_dump
类似,pg_dumpall
也是在执行期间获取数据库的一致性快照,所以备份过程中数据库依然可以正常使用。 - 虽然
pg_dumpall
会备份所有数据库和全局对象,但不包括配置文件,如postgresql.conf
和pg_hba.conf
。这些文件需要单独备份。
pg_dumpall
是进行 PostgreSQL 全面备份的重要工具,尤其适用于需要移植、升级或灾难恢复时,确保数据库环境的完整性。
谨记:心存敬畏,行有所止。