# Create a database and a table
createdb example_db
psql example_db
CREATE TABLE example_table (id INT, name VARCHAR(50));
# Insert some data into the table
INSERT INTO example_table VALUES (1, 'John'), (2, 'Jane'), (3, 'Bob');
# Create a backup of the database using the pg_dump utility
pg_dump example_db > backup.sql
# Modify the table by deleting a row
DELETE FROM example_table WHERE id=3;
# Create a write-ahead log (WAL) file to record the changes
SELECT pg_switch_xlog();
# Modify the table again by inserting a new row
INSERT INTO example_table VALUES (4, 'Alice');
# Create another WAL file
SELECT pg_switch_xlog();
# Simulate a crash by stopping the PostgreSQL server
sudo systemctl stop postgresql.service
# Restore the backup
psql example_db < backup.sql
# Apply the WAL files to roll the database forward to the current state
pg_waldump < pg_wal/000000010000000000000001 | psql example_db
pg_waldump < pg_wal/000000010000000000000002 | psql example_db
# Verify that the database is now in the correct state
psql example_db
SELECT * FROM example_table;