postgresql_16:4.11.1.3 Handling Large Databases
PostgreSQL 16 > Server Administration > Backup and Restore > SQL Dump > Handling Large Databases
Some operating systems have maximum file size limits that cause problems when creating large pg_dump output files. Fortunately, pg_dump can write to the standard output, so you can use standard Unix tools to work around this potential problem. There are several possible methods:
Use compressed dumps. You can use your favorite compression program, for example gzip: pg_dump *dbname* | gzip > *filename*.gz Reload with: gunzip -c *filename*.gz | psql *dbname* or: cat *filename*.gz | gunzip | psql *dbname*
Use split. The split command allows you to split the output into smaller files that are acceptable in size to the underlying file system. For example, to make 2 gigabyte chunks: pg_dump *dbname* | split -b 2G - *filename* Reload with: cat *filename** | psql *dbname* If using GNU split, it is possible to use it and gzip together: pg_dump *dbname* | split -b 2G --filter='gzip > $FILE.gz' It can be restored using zcat.
Use pg_dump's custom dump format. If PostgreSQL was built on a system with the zlib compression library installed, the custom dump format will compress data as it writes it to the output file. This will produce dump file sizes similar to using gzip, but it has the added advantage that tables can be restored selectively. The following command dumps a database using the custom dump format: pg_dump -Fc *dbname* > *filename* A custom-format dump is not a script for psql, but instead must be restored with pg_restore, for example: pg_restore -d *dbname* *filename* See the (see postgresql_16:7.2.11) and (see postgresql_16:7.2.16) reference pages for details.
For very large databases, you might need to combine split with one of the other two approaches.
Use pg_dump's parallel dump feature. To speed up the dump of a large database, you can use pg_dump's parallel mode. This will dump multiple tables at the same time. You can control the degree of parallelism with the -j parameter. Parallel dumps are only supported for the "directory" archive format. pg_dump -j *num* -F d -f *out.dir* *dbname* You can use pg_restore -j to restore a dump in parallel. This will work for any archive of either the "custom" or the "directory" archive mode, whether or not it has been created with pg_dump -j.