One, the meaning of pg_dump
pg_dump is a utility for backing up Postgresql databases. The dump file backed up by pg_dump is completely consistent, because the dump is a snapshot of the data when pg_dump starts to run. pg_dump will not prevent other users from accessing the database. If ordinary users have been granted database permissions, they can run pg_dump. pg_dump can only backup one database. Pg_dumpall is used to back up the entire cluster or the common objects common to all databases in the cluster.
Two, pg_dump file type
The dump file can be exported in script or archive file format.
The script dump file is a plain text file that contains the SQL commands needed to restore the database to the state it was in when it was saved. Use the script created in this way; it is sufficient to use psql or other tools to run the generated sql file to restore the database to its current state. The related database must be created before this command can be run, otherwise the object will be created in the postgres database by default.
If you don't want to create a database manually, you must use pg_dump to create a backup in the archive file format, in a custom, directory, and tar way. However, in this case, you need to use the pg_restore tool to restore the data.
Three, pg_dump syntax
pg_ dump [connection-option …] [option …] [dbname]
The -h parameter specifies the host of the database to be backed up
-p parameter specifies the port
-U parameter specifies the database user
Four, pg_dump backup and recovery
4.1 Using pg_dump to backup and restore the database
1. Back up the database
Several different syntaxes can be used to generate a SQL dump file of the database named zabbix.
pg_dump zabbix > db.sql
pg_dump -h localhost -p 5432 -U postgres zabbix > /home/postgres/dump/db_data.sql
pg_dump -h localhost -p 5432 -U postgres zabbix -f /home/postgres/dump/db_data.sql
2. Restore the database
You can restore the created dump file to another database named zabbix.
psql -d zabbix -f /home/postgres/dump/db_zabbix_data.sql
psql -f /home/postgres/dump/db_zabbix_data.sql -d newzabbix -p 5432 -U postgres
4.2, pg_dump custom format
Use the following command to create a dump of the zabbix database in a custom file format. The backup file generated in this way will be much smaller than the sql file, because the file is compressed using zlib in the background.
pg_dump -Fc zabbix > /home/postgres/dump/zabbix_db.dump
Restore the database from a custom file format dump
pg_restore -d test_zabbix /home/postgres/dump/zabbix_db.dump
4.3 pg_dump directory format
Use the following command to create a dump of the zabbix database in catalog format.
pg_dump -Fd zabbix -f dumpdirectory
Or use parallel backup
pg_dump -Fd zabbix -j 5 -f dumpdir
Restore database from catalog file
pg_restore -Fd -l dumpdirectory
4.4, pg_dump tar format backup
The following command creates a dump file of the mydb database in tar file format.
pg_dump -Ft zabbix> /home/postgres/dump/zabbix_db.tar
Restore Postgres database from tar file
pg_restore -Ft -d zabbix /home/postgres/dump/zabbix_db.tar
4.5, pg_dump specified table backup
1. Back up the specified table
pg_dump -t table1 -d zabbix > /home/postgres/dump/table1.sql
2. Back up the table whose name starts with tbl
pg_dump -t 'tbl*' -d zabbix > /home/postgres/dump/tbl.sql
3. Back up the table whose name starts with tbl but not tbl_mustafa
pg_dump -t 'tbl*' -T tbl_mustafa -d zabbix > /home/postgres/dump/tbl1.sql
4. Back up all patterns whose names start with East or West and end with gsm, and exclude all patterns whose names contain test
pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' zabbix >/home/postgres/dump/tbl2.sql
pg_dump -n '(east|west)*gsm' -N '*test*' zabbix > db.sql
5. Export data, non-leading table structure
pg_dump -a zabbix > /home/postgres/dump/tbl1.sql
pg_dump --data-only zabbix > /home/postgres/dump/tbl1.sql
4.6, pg_dump compression
The following command is used to perform compressed backup through pg_dump.
pg_dump zabbix | gzip -9 > zabbix.gz
4.7, pg_dump split
Split the dump output into multiple files of a certain size
pg_dump zabbix | split -b 1m – files
Recover split dump file
cat files* | psql zabbix
4.8、pg_dump ON_ERROR_STOP
Default; If an error occurs during restore, psql will ignore the error and continue to run. We can customize this situation by setting the ON_ERROR_STOP variable. After executing this command, the code will run until the line where the error occurs, but subsequent lines will not be processed.
psql --set zabbix < zabbix.sql
4.9, pg_dumpall backup
pg_dumpall backs up all the contents of the cluster. pg_dumpall creates a backup of each database in a given cluster, and creates backups of data such as cluster-wide roles and tablespace definitions. When using pg_dumpall for recovery, super user access is always required to recover role and tablespace information. pg_dumpall sends commands to create roles, tables and empty databases, and then runs pg_dump for each database.
Backup the entire library:
pg_dumpall > all_dbdata.sql
pg_dumpall -f all_dbdata.sql
Restore the entire library:
psql postgres < all_dbdata.sql
psql -f all_dbdata.sql postgres
From "ITPUB Blog", link: http://blog.itpub.net/28833846/viewspace-2742419/, if you need to reprint, please indicate the source, otherwise legal liability will be pursued.