How To Configure Warm Standby with pitrtools

This how-to demonstrates how to configure a warm standby using PostgreSQL 8.4 and pitrtools.

What Is pitrtools and Why Would You Want To Use It?

Essentially, pitrtools is a wrapper around standard tools, such as rsync and PostgreSQL's internal functionality, that makes, among other things, creating and managing standby configurations and subsequent failover to a standby a snap.

With the help of pitrtools you could do more, namely:
  • secure shipping of log files to configured standby server over SSL protected link;
  • streaming replication;
  • enable/disable archiving without the need to restart PostgreSQL;
  • stay informed by generating alerts based on various levels of severity of events happening during the process on both ends of configuration;
  • automatically take a base backup, including table spaces, restore archives and purge old ones (if PostgreSQL >8.3);
  • failover to the latest restore point and point-in-time recovery (restore using timestamps);
  • etc.

Obviously, pitrtools attempts to make things simpler, more secure and easier to manage.

Naming Conventions

Master server can be referred to also as archiver.
Slave is often called standby.

These names are used interchangeably.

The Test Setup

It's a good idea to first setup pitrtools and play with it before you actually go ahead and change configuration of your production servers. To show how pitrtools is configured and work, I'll describe the entire process using a 2 hosts test setup as an example.

So, there are 2 hosts named bitarena and bitarena-clone, both being virtualized instances of Debian Squeeze. I assume you're experienced enough to install Debian yourself and know how to find your way around the system.

bitarena is designated role of master server aka archiver, bitarena-clone is a slave aka standby server. pitrtools is installed on both hosts, but each host uses different tools from the package.

The Process

These are major steps in actual order of execution that one would have to follow to get pitrtools-enabled setup running:

On master server

  • Turn on archiving
  • Install helper scripts
  • cmd_archiver -C $CONFIG -I

On slave server

  • cmd_standby -C $CONFIG -I
  • cmd_standby -C $CONFIG -B
  • cmd_standby -C $CONFIG -S

Installing and Configuring pitrtools

If your system doesn't have a package for pitrtools download a tarball from pitrtools project page at

Installing pitrtools is a matter of unpacking an archive file. The recommended default is `postgres' home directory.

You can put it anywhere in your filesystem as long as the files belong to and can be accessed by `postgres'.

SSH Key-based Login for pitrtools

pitrtools relies on rsync and SSH heavily to do its work, e.g. making a base backup and shipping WAL log files from master to slave server -- all that happens over an SSL-protected communication channel. This is the area where pitrtools makes one's life easier, because otherwise you'd have to find a way to copy log files to a slave server somehow (most likely it would be some sort of networking mount point).

Therefore one of the prerequisites is to configure SSH key-based logins between the two hosts for postgres system user that don't require a password or a passphrase.


root@bitarena:~# su - postgres

postgres@bitarena:~$ ssh-keygen -t rsa
postgres@bitarena:~$ ls -la /var/lib/postgresql/.ssh/
total 16
drwx------ 2 postgres postgres 4096 Sep 28 09:17 .
drwxr-xr-x 5 postgres postgres 4096 Sep 28 09:17 ..
-rw------- 1 postgres postgres 1675 Sep 28 09:17 id_rsa
-rw-r--r-- 1 postgres postgres  399 Sep 28 09:17


root@bitarena-clone:~# su - postgres
postgres@bitarena-clone:~$ ssh-keygen -t rsa

Answer all the questions and ssh-keygen will create both private and public (*.pub file) RSA keys. For now just create the key pair, one on on each host (master and slave). When done, proceed to exchanging public keys between the hosts like this:


postgres@bitarena:~$ ssh-copy-id bitarena-clone
Now try logging into the machine, with "ssh 'bitarena-clone'", and check in:


to make sure we haven't added extra keys that you weren't expecting.

postgres@bitarena:~$ ssh bitarena-clone
Linux bitarena-clone 2.6.32-5-686 #1 SMP Sun May 6 04:01:19 UTC 2012 i686

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Oct 18 06:07:22 2012 from bitarena.localdomain
postgres@bitarena-clone:~$ less .ssh/authorized_keys

This will copy postgres system user's public SSH key on master to standby into a file authorized_keys, which will allow postgres on master to login to slave host without a password and also in a secure fashion.

Remember that this has been done on master server only. In a similar manner, you should take care of the slave host.

It's worth noting that pitrtools runs some actions remotely. For example, when a base backup action is run on a slave host, cmd_standby script establishes SSH session to a master host and runs various psql commands to deal with checkpoints, copy log files, etc. This also requires PostgreSQL user password to access the database, and it is often needed to be entered 4 and more times for a base backup action to complete.

Once you played around with pitrtools enough to get hang of things, you could avoid having to enter password manually each time by either using .pgpass file (which is a standard feature of PostgreSQL) or make sure there's a trust relationship configured for localhost in pg_hba.conf file.

For the sake of clarity, consider these examples:



host postgres postgres trust

In the console output examples you're going to see below I entered password manually, but this, of course, is really inconvenient in production environment.

Master Configuration File

Sample configuration files are pretty good as they are with the defaults they come. Chances are you're not going to change a lot in there. Make sure you read all *.README files, though, because they contain helpful extra information about pitrtools and configuration parameters that will help you decide how to best configure your servers.

We start with the master host by editing pitrtools configuration file for master/archiver.

postgres@bitarena:~$ vim pitrtools/etc/cmd_archiver.ini

    ; online or offline
    state: online                    

    ; The base database directory

    pgdata: /var/lib/postgresql/8.4/main

    ; where to remotely copy archives
    r_archivedir: /var/lib/postgresql/archive

    ; where to locally copy archives
    l_archivedir: /var/lib/postgresql/archive

    ; where is ssh and what flags to add
    ssh: /usr/bin/ssh

    ; where is rsync                
    rsync_bin: /usr/bin/rsync            

    ; extra rsync flags
    rsync_flags: -z

    ; option 2 or 3, if running RHEL5 or similar it is likely 2
    ; if you are running something that ships remotely modern software
    ; it will be 3

    rsync_version = 3

    ; IP of slave                    
    slaves: bitarena-clone

    ; the user for the ssh connection to a standby
    user: postgres                

    ; if rsync can't connect in 10 seconds error
    ssh_timeout: 10

    ; command to process in ok                    
    notify_ok: echo OK

    ; command to process in warning
    notify_warning:  echo WARNING

    ; command to process in critical
    notify_critical: echo CRITICAL

    ; if you want to debug on/off only             
    debug: on

    ; if you want ssh debug (warning noisy)
    ssh_debug: off

Note that you can use domain names instead of IP addresses for slaves: parameter, it works either way just fine. You might also want to turn debugging on while you're learning pitrtools. It helps to see what software does, if you try to understand better how it works.

Turn On and Configure Archiving

Now we need to turn on archiving functionality in PostgreSQL. This is purely a PostgreSQL feature, but we can also take advantage of using pitrtools thanks to flexible design of PostgreSQL.

postgres@bitarena:~$ vim /etc/postgresql/8.4/main/postgresql.conf

archive_command = '/var/lib/postgresql/pitrtools/bin/cmd_archiver -C /var/lib/postgresql/pitrtools/etc/cmd_archiver.ini -F %p'

Here we explicitly turn archiving mode on and tell PostgreSQL to use cmd_archiver, part of pitrtools, as the archiving command.
It must be provided with a path to a configuration file (-C switch), and a path to a log file (-F switch). %p is substituted by PostgreSQL with the actual log file location in the file system. To learn more about how to use cmd_archiver run

postgres@bitarena:~$ pitrtools/bin/cmd_archiver --help

Please, keep in mind that pitrtools isn't supposed to be run by root user. Technically, it can be run by any system user other than root, and unless you have a customized configuration your default PostgreSQL user will be postgres, so pitrtools are expected to be run as that system user.

Restart PostgreSQL for changes to take effect.

postgres@bitarena:~$ /etc/init.d/postgresql restart
Restarting PostgreSQL 8.4 database server: main.

Install Helper Scripts

Apply cmd_standby.sql to the database of pitrtools user (usually postgres). This is required for master server only.

postgres@bitarena:~$ psql -U postgres < ~/pitrtools/scripts/cmd_standby.sql

Note: If you are running Postgres 9.2, use cmd_standby.92.sql

Initialize Master Environment

At this point we're pretty much done configuring the master server. All that is left to do is initialize master environment by running

postgres@bitarena:~$ pitrtools/bin/cmd_archiver -C pitrtools/etc/cmd_archiver.ini -I
We are initializing queues, one moment.

NOTICE: init_env_func()
NOTICE: generate_slave_list_func()
NOTICE: Your slaves are: ['bitarena-clone']
postgres@bitarena:~$ ls -lah
total 40K
drwxr-xr-x  7 postgres postgres 4.0K Sep 28 10:25 .
drwxr-xr-x 34 root     root     4.0K Sep 27 02:32 ..
drwxr-xr-x  3 postgres postgres 4.0K Sep 24 15:10 8.4
drwx------  2 postgres postgres 4.0K Sep 27 02:23 .aptitude
drwxr-xr-x  3 postgres postgres 4.0K Sep 28 10:25 archive
-rw-------  1 postgres postgres 1001 Sep 28 10:03 .bash_history
drwxr-xr-x  2 postgres postgres 4.0K Sep 28 10:25 pitrtools
-rw-------  1 postgres postgres 1.4K Sep 28 05:08 .psql_history
drwx------  2 postgres postgres 4.0K Sep 28 09:29 .ssh
-rw-------  1 postgres postgres 3.6K Sep 28 10:25 .viminfo
postgres@bitarena:~$ ls -lah archive/bitarena-clone/
total 8.0K
drwxr-xr-x 2 postgres postgres 4.0K Sep 28 10:25 .
drwxr-xr-x 3 postgres postgres 4.0K Sep 28 10:25 ..

As you can see -I switch tells cmd_archiver to do a couple of internal actions, as well as prepare file system layout by creating necessary directories.

archive/ directory has been created automatically by cmd_archiver and contains sub-directory named after IP address or DNS name of a slave, bitarena-clone in this example. This sub-directory is used in those circumstances when master fails to successfully transfer WAL log files to slave and stores them temporarily in this local directory. Once slave is back online, these files should be transferred to slave.

Effectively, after you've initialized master it starts trying to ship WAL files to slave. However, the slave host isn't configured yet, so the log delivery will fail and the log files should be found in l_archivedir/slave_FQDNorIP/ folder on master host. Once we configure slave, these will be shipped as soon as a new WAL segment is created on master.

Slave aka Standby Server

Now we can prepare the slave host. SSH key-based login for postgres system user should be working in both directions, from master to slave, as well as from slave to master.

Assuming you've arranged for this as it was suggested before, we can now go on and edit slave configuration file to perform first important step and initialize slave environment.

One of the configuration file parameters, namely pgdata:, will ask you to specify PostgreSQL data directory. You could look it up in postgresql.conf, or, If your PostgreSQL is already running, you could see this information like this:

postgres@bitarena-clone:~$ ps axuwf |grep postgre
postgres  3352  0.0  0.4  46452  5464 ?        S    09:59   0:03 /usr/lib/postgresql/8.4/bin/postgres 
-D /var/lib/postgresql/8.4/main -c config_file=/etc/postgresql/8.4/main/postgresql.conf

Remember, note down the path somewhere or copy it to the clipboard. We'll need it in a minute.

Slave Configuration File

Now edit slave configuration file. Most defaults are good and safe options, but your setup may be different from what I have in this how-to. So, be careful to understand what you're doing.

postgres@bitarena-clone:~$ vim pitrtools/etc/cmd_standby.ini

; what major version are we using?
pgversion: 8.4

; Used for PostgreSQL version 8.2 and below, should be set 
; to something > than checkpoint_segments on master
numarchives: 10

; Whether or not to use streaming replication.  If this is set to "on" 
; pitrtools will configure the standby server to replicate from master
; using streaming replication.
; This can only be used with PostgreSQL 9.0 and up.
use_streaming_replication: no

; File to touch to end replication when using streaming replication.
trigger_file: /var/lib/postgresql/pitrtools/cmd_end_recovery

; User to connect to master DB while using streaming replication,
; ignored if not using streaming replication.
repl_db_user: replication

; Password for the user repl_db_user.
repl_db_password: replication

; sslmode to use when connecting for streaming replication.
; Accepted values: the same as libpq: disable, allow, prefer, require, verify-ca and verify-full
; Default: sslmode: prefer
sslmode: prefer

; Commands needed for execution

; absolute path to ssh, note you can also add flags such as -P for port.
ssh: /usr/bin/ssh

; absolute path to rsync
rsync: /usr/bin/rsync

; extra rsync flags
rsync_flags: -z

; the path to the postgres bin
pg_standby: /usr/lib/postgresql/8.4/bin/pg_standby
pg_ctl: /usr/lib/postgresql/8.4/bin/pg_ctl

; set it for 9.0 and above if streaming replication is enabled
pg_archivecleanup: /usr/lib/postgresql/9.0/bin/pg_archivecleanup

; path to psql on the Master
r_psql: /usr/lib/postgresql/8.4/bin/psql

; Generalized information

; the port postgresql runs on (master)
port: 5432

; IP or name of Master server
master_public_ip: bitarena

; the IP address we should use when processing remote shell

; the user performed initdb
user: postgres

; on or off
debug: on

; on or off
ssh_debug: off

; the timeout for ssh before we throw an alarm
ssh_timeout: 30

; should be the same as r_archivedir for archiver
archivedir: /var/lib/postgresql/archive

; where you executed initdb -D to
pgdata: /var/lib/postgresql/8.4/main

; Confs

; This is the postgresql.conf to be used for the failover
pg_conf_failover: /var/lib/postgresql/pitrtools/etc/pg_conf_failover/postgresql.conf

; This is the pg_hba.conf to be used for the failover
pg_hba_conf_failover: /var/lib/postgresql/pitrtools/etc/pg_conf_failover/pg_hba.conf

; This is the postgresql.conf to be used for Postgres when in standby mode
pg_conf: /var/lib/postgresql/pitrtools/etc/pg_conf/postgresql.conf

; This is the pg_hba.conf to be used for Postgres when in standby mode
pg_hba_conf: /var/lib/postgresql/pitrtools/etc/pg_conf/pg_hba.conf

; By default postgresql.conf and pg_hba.conf will be copied from the
; locations specified above to pgdata directory on failover.
; Uncomment the following to make postgres actually use the above conf
; files w/o copying them to pgdata.
;no_copy_conf: true

; The recovery.conf file to create when starting up
; Defaults to %(pgdata)/recovery.conf
recovery_conf: /var/lib/postgresql/8.4/main/recovery.conf

; Useful when postgresql.conf doesn't specify log destination
; Will be passed with -l to pg_ctl when starting the server.
; If you're worried about having complete logs, either make sure
; postgresql.conf points to a log file, or use the logfile: parameter.
; Otherwise postgresql will print on standard stdout and nothing
; will be recorded in the logs
logfile: /var/log/postgresql/postgresql-8.4-main.log

; Alarms

notify_critical: echo CRITICAL
notify_warning: echo WARNING
notify_ok: echo OK

; On failover action

; Whatever is placed here will be executed on -FS must return 0,
; must have execution bit for at least owner (chmod u+x)

action_failover: /var/lib/postgresql/pitrtools/scripts/

action_failover: script has to exist and have permissions of at least chmod u+x equivalent, it could be just a placeholder script with a simple action like this:

touch /var/lib/postgresql/pitrtools/failover_happened

but it's meant as a way to let you do certain actions on failover, those would be very specific for each given setup. It's good to know, though, that pitrtools lets you take actions automatically when failover happens. Use this feature to make your setup more sophisticated.

In addition, when doing a failover, there are two more additional options to take into consideration, namely pg_conf_failover: and pg_hba_conf_failover:. Both allow you to start server on failover using alternative configuration. This is meant to provide users with a way to prepare their failover scenario configuration in advance.

Initialize Slave Environment

First stop PostgreSQL, then initialize slave environment.

postgres@bitarena-clone:~/pitrtools$ bin/cmd_standby -C etc/cmd_standby.ini -I
NOTICE: check_pgpid_func()
DEBUG: executing query 'SELECT * FROM cmd_get_data_dirs()' by /usr/bin/ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no  postgres@bitarena  "/usr/lib/postgresql/8.4/bin/psql -A -t -Upostgres -p5432 -dpostgres -h127.0.0.1 
DEBUG: /var/lib/postgresql/8.4/main
Standby is ready

Please, note that if archivedir: /var/lib/postgresql/archive hasn't been created, you should do so manually as postgres system user (or set postgres user and group as the ownership information for this directory). pitrtools should do this automatically for you, but earlier versions were known not to do so. This is important, and the next step in slave configuration, which is base backup, will fail if archivedir: doesn't exist.

Making a Base Backup

Before you proceed check if archivedir: exists on slave and WAL files are being shipped to it from master host. WAL files are generated and shipped only when new data is stored in the database on master host. To help simulate data flow and check whether archiving and shipping is happening, try this SQL statement on master host:

postgres@bitarena:~$ psql
psql (8.4.13)
Type "help" for help.

postgres=# create table testpitrtools1 as select * from pg_class, pg_description;
postgresq=# \q

You could create a couple of tables like that to generate enough WAL segments. See archivedir: directory on slave to check whether any WAL files have been copied there. If they have, everything works as expected and you can try to make a base backup on slave host:

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -B
NOTICE: check_pgpid_func()
DEBUG: executing query  'checkpoint'  by /usr/bin/ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no  postgres@bitarena  "/usr/lib/postgresql/8.4/bin/psql -A -t -Upostgres -p5432 -dpostgres -h127.0.0.1 
DEBUG: executing query  'SELECT cmd_pg_start_backup()'  by /usr/bin/ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no  postgres@bitarena  "/usr/lib/postgresql/8.4/bin/psql -A -t -Upostgres -p5432 -dpostgres -h127.0.0.1 
DEBUG: cmd_pg_start_backup:  1
DEBUG: executing query 'SELECT * FROM cmd_get_data_dirs()' by /usr/bin/ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no  postgres@bitarena  "/usr/lib/postgresql/8.4/bin/psql -A -t -Upostgres -p5432 -dpostgres -h127.0.0.1 
DEBUG: executing query 'SELECT * FROM cmd_get_pgdata() LIMIT 1' by /usr/bin/ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no  postgres@bitarena  "/usr/lib/postgresql/8.4/bin/psql -A -t -Upostgres -p5432 -dpostgres -h127.0.0.1 
DEBUG: cleaning up /var/lib/postgresql/8.4/main/pg_xlog/ directory before rsync
receiving incremental file list
deleting recovery.done
deleting backup_label.old
deleting global/pgstat.stat

Number of files: 1199
Number of files transferred: 12
Total file size: 30675262 bytes
Total transferred file size: 353574 bytes
Literal data: 289873 bytes
Matched data: 63701 bytes
File list size: 13862
File list generation time: 0.008 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 1606
Total bytes received: 54020

sent 1606 bytes  received 54020 bytes  37084.00 bytes/sec
total size is 30675262  speedup is 551.46
DEBUG: executing query  'SELECT cmd_pg_stop_backup()'  by /usr/bin/ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no  postgres@bitarena  "/usr/lib/postgresql/8.4/bin/psql -A -t -Upostgres -p5432 -dpostgres -h127.0.0.1 
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
CONTEXT:  SQL function "cmd_pg_stop_backup" statement 1
DEBUG: cmd_pg_stop_backup: 1
Base backup finished successfully

As you can see what happens is that pitrtools puts master into backup mode, synchronizes data directories (including tablespaces, if any) from master to slave and then exits backup mode on master. If base backup action had failed before it properly finished (say, you had lost connection to slave while rsync was copying files over to it), you'd need to intervene and run manually -Astop_basebackup:

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -Astop_basebackup

After that run base backup action again. Just make sure it finishes its work properly (use console output for successful base backup action above as a reference).

Start a Standby

If you want a cold standby you're done. If you need a warm standby, then run:

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -S
NOTICE: check_pgpid_func()
server starting
NOTICE: check_pgpid_func()
Successfully entered standby mode

Take a look at logfile:, the /var/log/postgresql/postgresql-8.4-main.log in our case. A typical picture will look like this:

2012-10-16 02:47:31 PDT LOG:  database system was interrupted; last known up at 2012-10-16 02:44:37 PDT
2012-10-16 02:47:31 PDT LOG:  starting archive recovery
2012-10-16 02:47:31 PDT LOG:  restore_command = '/usr/lib/postgresql/8.4/bin/pg_standby -s5 -w0 -c -d  /var/lib/postgresql/archive %f %p %r '
Trigger file         : <not set>
Waiting for WAL file    : 00000001.history
WAL file path        : /var/lib/postgresql/archive/00000001.history
Restoring to        : pg_xlog/RECOVERYHISTORY
Sleep interval        : 5 seconds
Max wait interval    : 0 forever
Command for restore    : cp "/var/lib/postgresql/archive/00000001.history" "pg_xlog/RECOVERYHISTORY" 
Keep archive history    : 000000000000000000000000 and later
running restore        :cp: cannot stat `/var/lib/postgresql/archive/00000001.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000001.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000001.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000001.history': No such file or directory
not restored
history file not found
Trigger file         : <not set>
Waiting for WAL file    : 000000010000000100000025.00000020.backup
WAL file path        : /var/lib/postgresql/archive/000000010000000100000025.00000020.backup
Restoring to        : pg_xlog/RECOVERYHISTORY
Sleep interval        : 5 seconds
Max wait interval    : 0 forever
Command for restore    : cp "/var/lib/postgresql/archive/000000010000000100000025.00000020.backup" "pg_xlog/RECOVERYHISTORY" 
Keep archive history    : 000000000000000000000000 and later
running restore        : OK
2012-10-16 02:48:01 PDT LOG:  restored log file "000000010000000100000025.00000020.backup" from archive
Trigger file         : <not set>
Waiting for WAL file    : 000000010000000100000025
WAL file path        : /var/lib/postgresql/archive/000000010000000100000025
Restoring to        : pg_xlog/RECOVERYXLOG
Sleep interval        : 5 seconds
Max wait interval    : 0 forever
Command for restore    : cp "/var/lib/postgresql/archive/000000010000000100000025" "pg_xlog/RECOVERYXLOG" 
Keep archive history    : 000000000000000000000000 and later
running restore        : OK

2012-10-16 02:48:02 PDT LOG:  restored log file "000000010000000100000025" from archive
2012-10-16 02:48:02 PDT LOG:  automatic recovery in progress
2012-10-16 02:48:02 PDT LOG:  redo starts at 1/25000020, consistency will be reached at 1/2504FFC4
2012-10-16 02:48:03 PDT LOG:  consistent recovery state reached
Trigger file         : <not set>
Waiting for WAL file    : 000000010000000100000026
WAL file path        : /var/lib/postgresql/archive/000000010000000100000026
Restoring to        : pg_xlog/RECOVERYXLOG
Sleep interval        : 5 seconds
Max wait interval    : 0 forever
Command for restore    : cp "/var/lib/postgresql/archive/000000010000000100000026" "pg_xlog/RECOVERYXLOG" 
Keep archive history    : 000000010000000100000025 and later
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.
WAL file not present yet.

At this point PostgreSQL armed with pitrtools on master server will be continuously shipping log files to archivedir: on slave. Once shipped, the WAL files will be immediately replayed, because slave in standby mode continuously scans archivedir: for new WAL files and replays them as soon as they become available (this can be seen from example above).

Failing Over

Now you have a warm standby mirroring changes occurring on the master server. When your master server becomes unavailable due to any reason, you could turn this warm standby server into a production instance by simply running on standby machine a failover action as shown below. For this PostgreSQL on master must not be running, otherwise pitrtools will throw out a warning and refuse to failover.

postgres@bitarena-clone:~$ $ bin/cmd_standby -C etc/cmd_standby.ini -F999
NOTICE: check_pgpid_func()
NOTICE: Statistics are not replicated in warm standy mode.
HINT: Execute ANALYZE on your databases
Failover finished

Looking at the PostgreSQL's log file this is what happened:

2012-10-16 02:56:20 PDT LOG:  received fast shutdown request
2012-10-16 02:56:20 PDT LOG:  aborting any active transactions
waiting for server to shut down....2012-10-16 02:56:20 PDT LOG:  shutting down
2012-10-16 02:56:20 PDT LOG:  database system is shut down
server stopped
server starting
postgres@bitarena-clone:~$ 2012-10-16 02:56:22 PDT LOG:  database system was interrupted while in recovery at log time 2012-10-16 02:54:35 PDT
2012-10-16 02:56:22 PDT HINT:  If this has occurred more than once some data might be corrupted and you might need to choose an earlier recovery target.
2012-10-16 02:56:22 PDT LOG:  starting archive recovery
2012-10-16 02:56:22 PDT LOG:  restore_command = 'cp /var/lib/postgresql/archive/%f "%p"'
cp: cannot stat `/var/lib/postgresql/archive/00000001.history': No such file or directory
2012-10-16 02:56:23 PDT LOG:  restored log file "000000010000000100000026" from archive
2012-10-16 02:56:23 PDT LOG:  automatic recovery in progress
2012-10-16 02:56:23 PDT LOG:  redo starts at 1/2635B428, consistency will be reached at 1/27000000
cp: cannot stat `/var/lib/postgresql/archive/000000010000000100000027': No such file or directory
2012-10-16 02:56:23 PDT LOG:  could not open file "pg_xlog/000000010000000100000027" (log file 1, segment 39): No such file or directory
2012-10-16 02:56:23 PDT LOG:  redo done at 1/265AD744
2012-10-16 02:56:23 PDT LOG:  last completed transaction was at log time 2012-10-16 02:55:07.387709-07
2012-10-16 02:56:24 PDT LOG:  restored log file "000000010000000100000026" from archive
cp: cannot stat `/var/lib/postgresql/archive/00000002.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000003.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000004.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000005.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000006.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000007.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000008.history': No such file or directory
cp: cannot stat `/var/lib/postgresql/archive/00000009.history': No such file or directory
2012-10-16 02:56:24 PDT LOG:  selected new timeline ID: 9
cp: cannot stat `/var/lib/postgresql/archive/00000001.history': No such file or directory
2012-10-16 02:56:25 PDT LOG:  archive recovery complete
2012-10-16 02:56:26 PDT LOG:  database system is ready to accept connections
2012-10-16 02:56:26 PDT LOG:  autovacuum launcher started

This will create recovery.conf file under pg_data: directory and restart PostgreSQL to enter production mode of operation.

After this, you'd be basically running a copy of production master server. Keep in mind that you would also need to change IP addresses and/or load balancing configuration, routing, firewall rules or anything else that might be in the way of establishing a successful connection to this host. This is where action_failover: script could come in handy.

Plan in advance, figure this all out to avoid any downtime before you will need to failover.

Other things you could do with pitrtools and some tips

This how-to is meant to help you get started with pitrtools. pitrtools can do more, though, than just help you configure standby.

Point-In-Time Recovery

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -F999 -R '2008-05-28 11:00:38.059389'

This is essentially a restore to a specific point in time action. In general, you can only restore to a point in time while using cold standby, because PITR will "stop" recovering at the point in time you've specified, and both warm and hot standby servers have already recovered all the WAL files they can. In this regard, it would be a good idea to have a cold standby around for disaster (at logical level) recovery.

Once this has been done, you can't choose another timestamp to restore to.

Entering Standby Mode After Failover On Slave

Suppose you failed over to your standby slave, which is running now as a replacement of master for your applications. You've fixed problems with the actual master and want this slave host to enter standby mode again. Here's how you'd do it:

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -Astop

This will stop entire PostgreSQL service. You could also use PostgreSQL init script to achieve the same instead. If you need more fine-grained control use pg_ctlcluser 8.4 main stop (see man pg_ctlcluster for more details). Take a new base backup as before and enter standby mode:

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -B

postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -S

Again, if you want a cold standby just don't run -S action after -B.


Alerting is designed to run your custom scripts. You could easily integrate pitrtools alerting to your existing NMS be it Nagios, Zabbix, or anything else, send e-mails or take actions you decide.


It bears repeating -- just In case you overlooked this in standby sample configuration file notes -- if your postgresql.conf doesn't specify a log file to write to and you don't use logfile: parameter in cmd_standby.ini the output will be directed to stdout (your console) and nothing will ever be written to a log file on disk.

If you restart PostgreSQL, that'll fix the problem, but you can avoid it in the first place by either specifying log file to write to in postgresql.conf or by using logfile: parameter in cmd_standby.ini.


Set debug: parameter in configuration files to on and scrutinize the information. PostgreSQL log file is also a good place to look at.

Streaming Replication and Hot Standby

pitrools is designed to ship logs, even when you're running SR. The cmd_archiver, when used as archive_command in postgresql.conf will ship logs to the specified destination. Usually the standby or network storage. The cmd_standby assumes a user needs log-shipping as well, again even if SR is enabled.

The rationale behind this behavior is that running SR alone is considered less reliable. You can easily lose your standby if wal_keep_segments option is overrun during the long standby outage, and, if you need to switch a former standby to a master and be able to point other standbys to it, wal-shipping is your only alternative (until 9.3 comes up).

Master configuration

  • In postgresql.conf set:
wal_level = hot_standby
archive_mode = on
archive_command = '/var/lib/postgresql/pitrtools/bin/cmd_archiver -C /var/lib/postgresql/pitrtools/etc/cmd_archiver.ini -F %p'
  • Install pitrtools helper scripts
postgres@bitarena:~$ psql -U postgres < ~/pitrtools/scripts/cmd_standby.sql

Note: If you are running Postgres 9.2, use cmd_standby.92.sql

  • Create a new dedicated replication role
postgres@bitarena:~$ psql -c "CREATE USER replication SUPERUSER LOGIN ENCRYPTED PASSWORD 'changeme';" 
  • Configure .pgapss and pg_hba.conf



host postgres postgres trust
host replication   all   bitarena-clone.localdomain      trust

  • Start/restart PostgreSQL after you've started the slave (so it won't fall behind too far).
  • Initialize the master
postgres@bitarena:~$ pitrtools/bin/cmd_archiver -C pitrtools/etc/cmd_archiver.ini -I

Slave configuration

  • In postgresql.conf set:
hot_standby = on
  • In cmd_standby.ini set:
use_streaming_replication: on
trigger_file: /var/lib/postgresql/pitrtools/cmd_end_recovery
repl_db_user: replication
repl_db_password: changeme
pg_archivecleanup: /usr/lib/postgresql/9.1/bin/pg_archivecleanup
  • Initialize slave
postgres@bitarena-clone:~/pitrtools$ bin/cmd_standby -C etc/cmd_standby.ini -I
  • Make a base backup
postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -B
  • Start up the slave in Hot Standby mode with Streaming Replication
postgres@bitarena-clone:~$ pitrtools/bin/cmd_standby -C pitrtools/etc/cmd_standby.ini -S

Make sure to take care of failover configs.

Getting Help

A very low-traffic mailing list for pitrtools can be found here
There is also consulting available from Command Prompt