← 返回命令列表

Linux command

pg_dump 命令

文件

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

Dump a database

pg_dump [database_name] > [backup.sql]

Dump with compression

pg_dump -Fc [database_name] > [backup.dump]

Dump specific table

pg_dump -t [table_name] [database_name] > [table.sql]

Dump schema only

pg_dump -s [database_name] > [schema.sql]

Dump data only

pg_dump -a [database_name] > [data.sql]

Dump with connection parameters

pg_dump -h [host] -p [5432] -U [username] [database_name] > [backup.sql]

Dump in directory format

pg_dump -Fd -j [4] [database_name] -f [backup_dir]

Dump excluding table

pg_dump --exclude-table=[pattern] [database_name] > [backup.sql]

说明

pg_dump is a utility for backing up PostgreSQL databases. It creates a consistent snapshot of the database at the time the dump begins, even while the database is being used. The output can be in SQL script format (plain text) or custom archive formats that support compression and parallel restore. SQL scripts can be restored with psql, while archive formats use pg_restore. pg_dump does not block readers or writers during the dump, making it suitable for production backups.

参数

-F _format_
Output format (p=plain, c=custom, d=directory, t=tar).
-f _file_
Output file or directory.
-t _table_
Dump specific table(s).
-T _table_
Exclude table(s).
-n _schema_
Dump specific schema(s).
-N _schema_
Exclude schema(s).
-s, --schema-only
Dump schema only, no data.
-a, --data-only
Dump data only, no schema.
-c, --clean
Include DROP commands.
-C, --create
Include CREATE DATABASE command.
-j _jobs_
Parallel dump jobs (directory format).
-h _host_
Database server host.
-p _port_
Database server port.
-U _user_
Connect as user.
-W
Force password prompt.
-Z _level_
Compression level (0-9).

FAQ

What is the pg_dump command used for?

pg_dump is a utility for backing up PostgreSQL databases. It creates a consistent snapshot of the database at the time the dump begins, even while the database is being used. The output can be in SQL script format (plain text) or custom archive formats that support compression and parallel restore. SQL scripts can be restored with psql, while archive formats use pg_restore. pg_dump does not block readers or writers during the dump, making it suitable for production backups.

How do I run a basic pg_dump example?

Run `pg_dump [database_name] > [backup.sql]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -F _format_ do in pg_dump?

Output format (p=plain, c=custom, d=directory, t=tar).