Taking Base Backup in PostgreSQL

We can use the simple command to take base backup as below:


(multiple options are available to be called here as arguments, to suite the need)

The procedure for making a base backup. It is very important that these steps are executed in sequence, and that the success of a step is verified before proceeding to the next step.

  1. Ensure that WAL archiving is enabled and working.
  2. Connect to the database as a superuser and issue the command:


SELECT pg_start_backup(‘label’);


By default, pg_start_backup can take a long time to finish. This is because it performs a checkpoint, and the I/O required for the checkpoint will be spread out over a significant period of time, by default half your inter-checkpoint interval (see the configuration parameter checkpoint_completion_target). This is usually what you want, because it minimizes the impact on query processing. If you want to start the backup as soon as possible, use:

SELECT pg_start_backup(‘label’,true);

This forces the checkpoint to be done as quickly as possible.

  1. Perform the backup, using any convenient file-system-backup tool such as tar or cpio (not pg_dump or pg_dumpall). It is neither necessary nor desirable to stop normal operation of the database while you do this.
  2. Again connect to the database as a superuser, and issue the command:
SELECT pg_stop_backup();

This terminates the backup mode and performs an automatic switch to the next WAL segment. The reason for the switch is to arrange for the last WAL segment file written during the backup interval to be ready to archive.

  1. Once the WAL segment files active during the backup are archived, you are done. The file identified by pg_stop_backup's result is the last segment that is required to form a complete set of backup files. If archive_mode is enabled, pg_stop_backup does not return until the last segment has been archived. Archiving of these files happens automatically since you have already configured archive_command. In most cases this happens quickly, but you are advised to monitor your archive system to ensure there are no delays. If the archive process has fallen behind because of failures of the archive command, it will keep retrying until the archive succeeds and the backup is complete. If you wish to place a time limit on the execution of pg_stop_backup, set an appropriate statement_timeout value.