MOCKSTACKS
EN
Questions And Answers

More Tutorials









postgreSQL Backup and Restore

Backing up the filesystem instead of using pg_dumpall and pg_dump


It's very important that if you use this, you call the pg_start_backup() function before and pg_stop_backup() function after. Doing filesystem backups is not safe otherwise; even a ZFS or FreeBSD snapshot of the filesystem backed up without those function calls will place the database in recovery mode and may lose transactions.

I would avoid doing filesystem backups instead of regular Postgres backups, both for this reason, and because Postgres backup files (especially in the custom format) are extremely versatile in supporting alternate restores. Since they're single files, they're also less hassle to manage.

Examples

Backing up one database


pg_dump -Fc -f DATABASE.pgsql DATABASE

The -Fc selects the "custom backup format" which gives you more power than raw SQL; see pg_restore for more details. If you want a vanilla SQL file, you can do this instead:

pg_dump -f DATABASE.sql DATABASE

or even

pg_dump DATABASE > DATABASE.sql

Restoring backups


psql < backup.sql

A safer alternative uses -1 to wrap the restore in a transaction. The -f specifies the filename rather than using shell redirection.

psql -1f backup.sql

Custom format files must be restored using pg_restore with the -d option to specify the database:

pg_restore -d DATABASE DATABASE.pgsql

The custom format can also be converted back to SQL:

pg_restore backup.pgsql > backup.sql

Usage of the custom format is recommended because you can choose which things to restore and optionally enable parallel processing.

You may need to do a pg_dump followed by a pg_restore if you upgrade from one postgresql release to a newer one.

Conclusion

In this page (written and validated by ) you learned about postgreSQL Backup and Restore . What's Next? If you are interested in completing postgreSQL tutorial, your next topic will be learning about: postgreSQL Backing up the whole cluster.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.