Base Backup, Restore and Point In Time Recovery(PITR) by Using PostgreSQL in Windows OS


We will explore the process of performing a point-in-time recovery (PITR) in PostgreSQL. "Point-in-time recovery" allows you to restore a database to a specific moment in the past, making it an invaluable feature for disaster recovery and data restoration purposes.
Step 1: Initial Setup
Create a folder named "pgbasebackup" in "c:\Program Files\PostgreSQL\15" to store backup files. Inside "pgbasebackup," create two subfolders named "bck1" and "data.before_recovery." Create a folder named "wal_archive" in "c:\Program Files\PostgreSQL\15" and ensure it has Full Control security properties enabled.
"C:\Program Files\PostgreSQL\15\pgbasebackup\bck1" "C:\Program Files\PostgreSQL\15\pgbasebackup\data.before_recovery"
Step 2: Configure PostgreSQL for Archiving
Access the PostgreSQL configuration file, "PostgreSQL.conf," usually located at "c:\Program Files\PostgreSQL\15\data\postgresql.conf."
Set the following parameters to enable archiving:
wal_level = replica
full_page_writes = on
archive_mode = on
archive_command = 'copy "%p" "C:\Program Files\PostgreSQL\15\wal_archive\%f"'
archive_timeout = 300 sec.
Step 3: Allow Replication in pg_hba.conf
Open the "pg_hba.conf" file located at
"C:\ProgramFiles\PostgreSQL\15\data\pg_hba.conf."
Copy the last two lines and comment out the last two lines.
Modify the newly copied lines to enable replication for the "Postgres" user:
#host replication all 127.0.0.1/32 scram-sha-256 #host replication all ::1/128 scram-sha-256 host replication postgres 127.0.0.1/32 md5 host replication postgres ::1/128 md5
Step 4: Restart PostgreSQL Service
Check the status of the PostgreSQL service using the command:
pg_ctl status -D "c:\\Program Files\\PostgreSQL\\15\\data\\"
Restart the PostgreSQL service if it's running.
Step 5: Taking Full Backup
Open a command prompt and run the following command to take a base backup into the "bck1" folder:
pg_basebackup --host=localhost --port=5432 --username=postgres --pgdata="C:\Program Files\PostgreSQL\15\pgbasebackup\bck1"
Step 6: Add Extra Data (Optional)
- Perform additional data modifications in specific tables of your PostgreSQL database, if required.
Point-in-Time Recovery:
Step 1: Stop Database Server
Open a command prompt and stop the PostgreSQL server using the following command:
pg_ctl stop -D "c:\\Program Files\\PostgreSQL\\15\\data\\"
Step 2: Backup Current Database Files
Create a backup of the current database files using the following command:
xcopy /O/X/E/H/K "C:\\Program Files\\PostgreSQL\\15\\data" "C:\\Program Files\\PostgreSQL\\15\\pgbasebackup\\data.before_recovery"
Step 3: Remove Contents in Data Directory
Clear the contents of the data directory using the following command:
rmdir /S/Q "c:\\Program Files\\PostgreSQL\\15\\data"
Step 4: Restore the Database to Point in Time
Create a new empty data directory for PostgreSQL:
mkdir "c:\\Program Files\\PostgreSQL\\15\\data"
Copy the backup files from "bck1" to the data directory:
xcopy /O/X/E/H/K "c:\\Program Files\\PostgreSQL\\15\\pgbasebackup\\bck1" "c:\\Program Files\\PostgreSQL\\15\\data"
Step 5: Cleanup Log Files
Delete the log files in the data directory:
del /S/F/Q "C:\Program Files\PostgreSQL\\15\\data\\log"
Set Full Control permissions for the data directory.
Step 6: Create Recovery.conf File
Inside the new data directory, create a file named "recovery. conf".
Write the following command in the "recovery. conf" file:
restore_command = 'copy "C:\\Program Files\\PostgreSQL\\15\\wal_archive\\%f" "%p"' recovery_target_time = '2023-07-24 12:00:00'
Step 7: Start the Server in Recovery Mode
Open a command prompt and start the PostgreSQL server in recovery mode:
pg_ctl start -D "c:\\Program Files\\PostgreSQL\\15\\data\\"
Conclusion: In this blog post, we've learned how to set up point-in-time recovery for PostgreSQL. This powerful feature ensures data integrity and allows you to restore your database to a specific moment in time, minimizing the risk of data loss and providing better disaster recovery capabilities. By following the step-by-step guide, you can confidently perform point-in-time recovery in your PostgreSQL environment.
Subscribe to my newsletter
Read articles from Bodreddy Purushotham Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
