Backup database
Info
PostgreSQL offer for us to using multiple way backup database inside, manipulate the output backup, moreover
Documentation: PostgreSQL official - Chapter 26. Backup and Restore
Use pg_dump
for backup the database
- Basic usage for dumping data with command
But also, you want to specify the location, user or what database for dumping. Add flag or environment variable like
PGHOST
=-h
: Host connection parameterPGPORT
=-p
: Port connection parameterPGDATABSE
=-d
: Database that use for connectionPGUSER
=-U
: User that use for connectionPGPASSWORD
: Password parameter provide for connectionPGPASSFILE
:passfile
that provide password for connection-W, --password
: force password prompt (should happen automatically)
- Manipulation output with
gzip
orcompression
Use gzip for compressing
Use -F
for decide output type
Use pg_dump
with parallel
Archive File System for backup
Question
When you decide to choose this method, make sure you know where location of data storage, how to connect and backup that on
server:server
orgeography
You tar for compress your file_system, It will scale down your size to minimal
Info
There are two restrictions, however, which make this method impractical, or at least inferior to theΒ pg_dumpΒ method:
- The database serverΒ mustΒ be shut down in order to get a usable backup. Half-way measures such as disallowing all connections willΒ notΒ work (in part becauseΒ
tar
Β and similar tools do not take an atomic snapshot of the state of the file system, but also because of internal buffering within the server). Information about stopping the server can be found inΒ SectionΒ 19.5. Needless to say, you also need to shut down the server before restoring the data.- If you have dug into the details of the file system layout of the database, you might be tempted to try to back up or restore only certain individual tables or databases from their respective files or directories. This willΒ notΒ work because the information contained in these files is not usable without the commit log files,Β
pg_xact/*
, which contain the commit status of all transactions. A table file is only usable with this information. Of course it is also impossible to restore only a table and the associatedΒpg_xact
Β data because that would render all other tables in the database cluster useless. So file system backups only work for complete backup and restoration of an entire database cluster.
Related documentation:
Restore database
Info
Mapping backup and restore methods will have appropriate and corresponding methods
Basic usage
On this situation, your backup is raw SQL file. You just use pgsql
for restoring, but before that removing and create a new database again
Manipulate compress situation
- Use
gzip
You can use gzip
with -d
or --decompress
to extract the backup, or you can use gunzip
with compatible functionality
- Use
-F
flag to compress
On this situation, you use customize dump, It will not exist on raw
SQL, you must be used pg_restore
for instead
- For dump with multiple thread, you can do same thing with restore with
-j
flag
Conclusion
Quote
Through the post, you will learn some about methodology which use to backup, restore PostgreSQL and how to manipulate compression when huge database come in. Stay safe and feel free to expand more !!! πππ