How to Enable Archive Log Mode in PostgreSQL 15
How to Enable Archive Log Mode in PostgreSQL 15
Enabling archive log mode in PostgreSQL is crucial for backup and recovery strategies. It allows you to store Write-Ahead Log (WAL) files safely, which are essential for point-in-time recovery and replication setups. Below is a step-by-step guide to enable archive logging in PostgreSQL 15 using a custom archive command path.
Step 1: Check Current Archive Settings
First, connect to your PostgreSQL instance and check the current archiving-related parameters:
SHOW archive_mode; SHOW archive_command; SHOW wal_level;
This helps you understand the existing configuration before making changes.
Step 2: Set Required Parameters
Enable archiving and configure the archive command. Run the following commands in your PostgreSQL session:
ALTER SYSTEM SET archive_mode = 'on'; ALTER SYSTEM SET archive_command = 'cp %p /var/lib/pgsql/15/data/archive/%f'; ALTER SYSTEM SET wal_level = 'replica';
- archive_mode = ‘on’ → Turns on WAL archiving.
- archive_command → Defines how WAL files are copied to the archive location. Here,
%p
represents the path of the WAL file, and%f
is the filename. - wal_level = ‘replica’ → Ensures WAL files contain sufficient information for archiving and replication.
Step 3: Restart PostgreSQL
For these changes to take effect, restart PostgreSQL:
Using systemctl:
sudo systemctl restart postgresql-15
Or using pg_ctl:
Step 4: Verify Settings
After restart, verify the updated parameters:
SHOW archive_mode; SHOW archive_command; SHOW wal_level;
You should see:
archive_mode = on
archive_command
showing your configured pathwal_level = replica
Step 5: Test Archiving
To confirm archiving is working, force a WAL switch:
SELECT pg_switch_wal();
Then, check the archive directory:
You should see newly generated WAL files.
Final Notes
- Ensure the archive directory exists before enabling archiving:
mkdir -p /var/lib/pgsql/15/data/archivechown postgres:postgres /var/lib/pgsql/15/data/archivechmod 700 /var/lib/pgsql/15/data/archive
- If PostgreSQL cannot write to the directory, archiving will fail.
By completing these steps, you have successfully enabled archive log mode in PostgreSQL 15. This setup ensures your WAL files are safely archived, supporting recovery and replication strategies.
At Learnomate Technologies, we specialize in delivering the best training for PostgreSQL and other database technologies. Whether you’re a beginner or looking to advance your skills, our expert-led programs are designed to make you industry-ready.
Want to dive deeper into topics like streaming replication and other PostgreSQL features? Check out our YouTube channel for insightful tutorials: www.youtube.com/@learnomate.
Explore more about our PostgreSQL training program on our website here: https://learnomate.org/training/postgresql-training/.
Don’t forget to connect with me on LinkedIn for regular updates and discussions on trending tech topics: https://www.linkedin.com/in/ankushthavali/.
If reading blogs is your thing, we’ve got you covered. From PostgreSQL to other trending technologies, our blog page is packed with helpful content. Start exploring here: https://learnomate.org/blogs/.
Let’s keep learning and growing together!