Patroni allows customizing creation of a new replica. It also supports defining what happens when the new empty cluster
is being bootstrapped. The distinction between two is well defined: Patroni creates replicas only if the
key is present in DCS for the cluster. If there is no
key - Patroni calls bootstrap exclusively on the
first node that takes the initialize key lock.
command to initialize a new cluster and Patroni calls it by default. In certain cases,
particularly when creating a new cluster as a copy of an existing one, it is necessary to replace a built-in method with
custom actions. Patroni supports executing user-defined scripts to bootstrap new clusters, supplying some required
arguments to them, i.e. the name of the cluster and the path to the data directory. This is configured in the
section of the Patroni configuration. For example:
: command: [param1 [, ...]]keep_existing_recovery_conf: False no_params: False recovery_conf: recovery_target_action: promote recovery_target_timeline: latest restore_command:
Each bootstrap method must define at least a
. A special
method is available to trigger
the default behavior, in which case
parameter can be omitted altogether. The
can be specified using either
an absolute path, or the one relative to the
command location. In addition to the fixed parameters defined
in the configuration files, Patroni supplies two cluster-specific ones:
Name of the cluster to be bootstrapped
Path to the data directory of the cluster instance to be bootstrapped
Passing these two additional flags can be disabled by setting a special
If the bootstrap script returns 0, Patroni tries to configure and start the PostgreSQL instance produced by it. If any of the intermediate steps fail, or the script returns a non-zero value, Patroni assumes that the bootstrap has failed, cleans up after itself and releases the initialize lock to give another node the opportunity to bootstrap.
block is defined in the same section as the custom bootstrap method, Patroni will generate a
before starting the newly bootstrapped instance. Typically, such recovery.conf should contain at least
one of the
parameters, together with the
is defined and set to
, Patroni will not remove the existing
file if it exists.
This is useful when bootstrapping from a backup with tools like pgBackRest that generate the appropriate
Bootstrap methods are neither chained, nor fallen-back to the default one in case the primary one fails
Patroni uses tried and proven
in order to create new replicas. One downside of it is that it requires
a running master node. Another one is the lack of ‘on-the-fly’ compression for the backup data and no built-in cleanup
for outdated backup files. Some people prefer other backup solutions, such as
others, or simply roll their own scripts. In order to accommodate all those use-cases Patroni supports running custom
scripts to clone a new replica. Those are configured in the
postgresql: create_replica_methods: -
: command: keep_data: True no_params: True no_master: 1
postgresql: create_replica_methods: - wal_e - basebackup wal_e: command: patroni_wale_restore no_master: 1 envdir: WALE_ENV_DIR use_iam: 1 basebackup: max-rate: '100M'
postgresql: create_replica_methods: - pgbackrest - basebackup pgbackrest: command: /usr/bin/pgbackrest --stanza=
--delta restorekeep_data: True no_params: True basebackup: max-rate: '100M'
defines available replica creation methods and the order of executing them. Patroni will
stop on the first one that returns 0. Each method should define a separate section in the configuration file, listing the command
to execute and any custom parameters that should be passed to that command. All parameters will be passed in a
format. Besides user-defined parameters, Patroni supplies a couple of cluster-specific ones:
Which cluster this replica belongs to
Path to the data directory of the replica
Connection string to connect to the cluster member to clone from (master or other replica). The user in the connection string can execute SQL and replication protocol commands.
parameter, if defined, allows Patroni to call the replica creation method even if there is no
running master or replicas. In that case, an empty string will be passed in a connection string. This is useful for
restoring the formerly running cluster from the binary backup.
parameter, if defined, will instruct Patroni to not clean PGDATA folder before calling restore.
parameter, if defined, restricts passing parameters to custom command.
method is a special case: it will be used if
is empty, although it is possible
to list it explicitly among the
methods. This method initializes a new replica with the
, the base backup is taken from the master unless there are replicas with
tag, in which case one
of such replicas will be used as the origin for pg_basebackup. It works without any configuration; however, it is
possible to specify a
configuration section. Same rules as with the other method configuration apply,
namely, only long (with –) options should be specified there. Not all parameters make sense, if you override a connection
string or provide an option to created tar-ed or compressed base backups, patroni won’t be able to make a replica out
of it. There is no validation performed on the names or values of the parameters passed to the
Also note that in case symlinks are used for the WAL folder it is up to the user to specify the correct
path as an option, so that after replica buildup or re-initialization the symlink would persist. This option is supported
only since v10 though.
You can specify basebackup parameters as either a map (key-value pairs) or a list of elements, where each element
could be either a key-value pair or a single key (for options that does not receive any values, for instance,
Consider those 2 examples:
postgresql: basebackup: max-rate: '100M' checkpoint: 'fast'
postgresql: basebackup: - verbose - max-rate: '100M' - waldir: /pg-wal-mount/external-waldir
If all replica creation methods fail, Patroni will try again all methods in order during the next event loop cycle.
Another available option is to run a "standby cluster", that contains only of standby nodes replicating from some remote master. This type of clusters has:
"standby leader", that behaves pretty much like a regular cluster leader, except it replicates from a remote master.
cascade replicas, that are replicating from standby leader.
Standby leader holds and updates a leader lock in DCS. If the leader lock expires, cascade replicas will perform an election to choose another leader from the standbys.
For the sake of flexibility, you can specify methods of creating a replica and
recovery WAL records when a cluster is in the "standby mode" by providing
section. It is distinct from
creating replicas, when cluster is detached and functions as a normal cluster,
which is controlled by
"standby" and "normal"
reference keys in
To configure such cluster you need to specify the section
in a patroni configuration:
bootstrap: dcs: standby_cluster: host: 18.104.22.168 port: 5432 primary_slot_name: patroni create_replica_methods: - basebackup
Note, that these options will be applied only once during cluster bootstrap, and the only way to change them afterwards is through DCS.
If you use replication slots on the standby cluster, you must also create the corresponding replication slot on the primary cluster. It will not be done automatically by the standby cluster implementation. You can use Patroni’s permanent replication slots feature on the primary cluster to maintain a replication slot with the same name as
, or its default value if
is not provided.