What is the best way to compress postgres database dump file?

I have large databases which im dumping with pg_dump and I need to make dump file smaller. What is the best way to compress this postgres database dump file?


Solution 1:

pg_dump comes with compression feature built-in:

  • -Z 0..9
  • --compress 0..9

Specify the compression level to use. Zero means no compression. For the custom archive format, this specifies compression of individual table-data segments, and the default is to compress at a moderate level. For plain text output, setting a nonzero compression level causes the entire output file to be compressed, as though it had been fed through gzip; but the default is not to compress. The tar archive format currently does not support compression at all.

There are 9 levels of compression - higher the level, higher the compression. 0 means no compression.

pg_dump [connection-option...] --compress=9 [dbname]

for maximum compression.

Solution 2:

xz compresses data better than gzip. Running xz -6 <dumpfile> will compress the database dump with maximum compression.