How to move pg_wal to another location in PostgreSQL

In this article, we will see how to move pg_wal to another location in PostgreSQL.

To move the WAL(xlog) to different directory than default, we must restart PostgreSQL server. This requires downtime, so we have to plan appropriately.

Steps to move pg_wal to another location in PostgreSQL:

1. Create directory on a new disk or new path and assign ownership to user postgres:

mkdir -p /wals chown postgres:postgres /wals

2. Stop the PostgreSQL server.

pg_ctl -D $PGDATA stop -mf

3. Now, move the all existing WALs and the archive_status directory inside pg_wal(pg_xlog) to the new directory on another disk. After move, make sure that pg_wal is empty and everything is moved to a new directory.

mv $PGDATA/pg_wal/* /wals

4. Now, create a symbolic link after removing the old WAL directory:

rmdir $PGDATA/pg_wal ln -s /wals pg_wal

5. Lets verify the symbolic link is pointing to new location or not.

ls -ltarh pg_wal

6. Restart our PostgreSQL server.

pg_ctl -D $PGDATA start