A slave can be a wonderful thing if you want to scale up reads or if you want to have a backup of your data. But, a slave might not always have to stay a slave. At some point, you might need to turn a slave into a master.
PostgreSQL offers some simple ways to do that. The first and most likely the most convenient way to turn a slave into a master is by using pg_ctl
:
iMac:slavehs$ pg_ctl -D . promote server promoting iMac:slavehs$ psql test psql (9.2.4) Type "help" for help. test=# CREATE TABLE sample (id int4); CREATE TABLE
The promote
command will signal the postmaster and turn your slave into a master. Once this is complete, you can connect and create objects.
In addition to the promote command, there is a second option to turn a slave into a master. Especially when you are trying to integrate PostgreSQL with a high-availability software of your choice, it can be easier to create a simple file than to call an init
script.
To use the file based method, you can add the trigger_file
command to your recovery.conf
file:
trigger_file = '/tmp/start_me_up.txt'
In our case, PostgreSQL will wait for a file called /tmp/start_me_up.txt
to come into existence. The content of this file is totally irrelevant; PostgreSQL simply checks if the file is present, and if it is, it will stop recovery and turn itself into a master.
Creating an empty file is a rather simple task:
iMac:slavehs$ touch /tmp/start_me_up.txt
The database system will react to the new file start_me_up.txt
FATAL: terminating walreceiver proced fire up: LOG: trigger file found: /tmp/start_ss due to administrator command LOG: redo done at 0/50000E0 LOG: selected new timeline ID: 2 LOG: archive recovery complete LOG: database system is ready to accept connections LOG: autovacuum launcher started
PostgreSQL will check for the file you have defined in recovery.conf
every five seconds. For most cases, this is perfectly fine, and by far, fast enough.
18.220.184.6