7

I'm running tests with PostgreSQL hot standby with 1 master, and exactly 1 slave.

I am using the instructions on this guide: http://www.howtoforge.com/how-to-set-up-a-postgresql-9.0-hot-standby-streaming-replication-server-with-repmgr-on-opensuse-11.4

I'm using PostgreSQL version 9.1, repmgr 1.1.0 and Ubuntu 10.04 LTS.

I followed steps upto step-6 in the guide where I ran

repmgr -D /var/lib/postgresql/9.1/main -d pgbench -p 5432 -R postgres --verbose standby clone pgmaster

on pgslave.

Then I did a

/etc/init.d/postgresql start

on it and it the script (seemingly) finished successfully.
However, executing psql throws error:

psql: FATAL: the database system is starting up

Any help on proceeding further is welcome.

1
  • I think the hot_stanby = on should be set on the primary since if you set it on the standby you will delete this file in the step Empty the directory completely with rm -rf *
    – user39579
    Commented Jun 5, 2014 at 12:58

1 Answer 1

12

I searched around and realized that the problem was due to not enabling hot_standby on the standby server. I wrote a corrected, updated and simplified version of the above article as an odt for future personal reference. Here is the text in its entirety (converted to html from odt using openoffice.org) for those who are interested in setting up a single read-only clone of a database using repmgr:


Single Slave Streaming Replication with PostgreSQL

Introduction

This guide aims to quickly help you configure a PostgreSQL 9.1 server with a database, and have it replicated to a slave that can be used for read-only queries. There is no concept of failover involved here, and the slave will only have a read-only copy of the master's data.


1. Tools Needed
  1. A stable GNU/Linux Distribution (Recommended OS: CentOS 6.2 x86-64)

  2. PostgreSQL 9.1 – you may install from here: http://yum.postgresql.org/ (also make sure that the following packages or their equivalnets are installed)

  3. postgresql

  4. postgresql-client

  5. postgresql-contrib

  6. postgresql-server

  7. postgresql-server-dev

  8. 2 workstations – a master that runs the primary database and a slave that runs the replicated read-only database (for the purpose of this document, their respective IP addresses have been replaced with pgmaster and pgslave so make those changes in /etc/hosts of both machines if you want to follow the following instructions word for word)


2. Installation Check and Password Creation
  1. Run /etc/init.d/postgresql start on both systems to check if PostgreSQL is functional or not.

  2. Run /etc/init.d/postgresql stop on both systems to stop PostgreSQL. We will not be using PostgreSQL till we finish some configuration related tasks.

  3. Set a password for user postgres on both the systems. This user by default has no password, but we need a password to help create an SSH tunnel between pgmaster and pgslave.

  4. Run sudo passwd postgres on both systems and type a new Unix password for both.


3. SSH Tunnel Creation
  1. On the pgmaster do the following:

  2. su postgres

  3. ssh-keygen -t rsa (press enter at every prompt)

  4. ssh-copy-id -i ~/ssh/id_rsa.pub pgslave (you need to enter pgslave's postgres password)

  5. ssh pgslave and see if you are able to login without password

  6. Repeat the above steps on pgslave

  7. su postgres

  8. sh-keygen -t rsa

  9. ssh-copy-id -i ~/ssh/id_rsa.pubaster

  10. ssh pgmaster and see if you are able to login without password

  11. Make sure you log out from the remote machine after you finish checking connectivity


4. Editing postgresql.conf on pgmaster

You need to make the following changes in the file postgresql.conf that resides in the configuration directory inside /etc/postgresql/ on your machine pgmaster:

  • listen_addresses = '*'

  • wal_level = hot_standby

  • checkpoint_segments=30

  • archive_mode=on

  • archive_command='cd .'

  • max_wal_senders=2

  • wal_keep_segments=5000


5. Editing postgresql.conf on pgslave

You need to make the following change in the file postgresql.conf that resides in the configuration directory inside /etc/postgresql/ on machine pgslave:

  • hot_standby=on


6. Editing pg_hba.conf on pgmastger

You need to make the following changes in the file pg_hba.conf that resides in the configuration directory inside /etc/postgresql/ on machine pgmaster:

  • host all all 192.168.5.0/24 trust

  • host replication all 192.168.5.0/24 trust


7. Adding PostgreSQL bin folder to PATH

There are a bunch of nifty postgresql utilities we will be using here, so lets set the PATH variable so that it knows where to find them

  1. Execute locate pgbench

  2. The output will be something like /usr/lib/postgresql/9.1/bin/pgbench

  3. Excluding final word pgbench, copy the above path and add it to your path variable.

  4. Execute nano ~/.bashrc

  5. add line export PATH+=:/usr/lib/postgresql/9.1/bin/ (or whatever location the locate command revealed)

  6. Close the file and save changes

  7. You may need to log out and login (or open a new shell) for changes to take effect.


8. Loading pgmaster's PostgreSQL server with dummy data
  1. On pgmaster start the PostgreSQL server first: /etc/init.d/postgresql start

  2. We create a test database and load it with some dummy data with the following commands:

    1. su postgres

    2. createdb pgbench

    3. pgbench -i -s 10 pgbench

  3. Alternatively open the database pgbench yourself, create a sample table and insert sample data into it.


9. Erasing pgslave's data and checking pgmaster connectivity
  1. We are going to erase data directory of pgslave, so execute following on that machine:

  2. First stop PostgreSQL server /etc/init.d/postgresql stop

  3. Move into the PostgreSQL default data directory folder: cd /var/lib/pgsql/data (or to the data directory that is default for your installation)

  4. Empty the directory completely with rm -rf *

  5. Now execute psql -h pgmaster -d pgbench and see if you are able to access the database on pgmaster through pgslave.


10. Installing repmgr

Though single slave replication is fairly straightforward, we are using a tool called repmgr to help make the process even more simpler. Here is how you install it:

  1. Grab repmgr source code from http://projects.2ndquadrant.it/sites/default/files/repmgr-1.1.0.tar.gz and copy it to /tmp for installation.

  2. Have the following packages installed to ensure that compiling repmgr is possible:

  3. make

  4. gcc

  5. postgresql-devel

  6. libxslt-devel

  7. pam-devel

  8. libopenssl-devel

  9. krb5-devel

  • Extract the downloaded archive and enter the source code folder.

  • Compile with make USE_PGXS=1

  • Install with make USE_PGXS=1 install

  • Execute below two commands to check if repmgr is installed correctly:

  • repmgr --version

  • repmgrd --version


    11. Cloning pgmaster onto pgslave using repmgr
    1. Execute su postgres and login as the postgres user on pgslave

    2. Run this to clone database: repmgr -D /var/lib/pgsql/data -d pgbench -p 5432 -R postgres --verbose standby clone pgmaster

    3. Note that the -D parameter /var/lib/pgsql/data should be replaced with the appropriate location of the data folder on pgslave

    4. Also note that 5432 is the default port PostgreSQL runs in.

    5. When the command finishes executing (it is going to take several seconds to finish if you had used pgbench to insert random data) you may start PostgreSQL on pgslave with /etc/init.d/postgresql start


    12. Testing streaming replication

    Congratulations! You have successfully configured pgslave to copy pgmaster's database via streaming replication. Note that due to being in continuous recovery mode, pgslave can only be used to execute read-only queries and no insertions or modifications are possible.

    1. Insert some values into sample_table on pgmaster.

    2. execute select * from sample_table; on pgslave's psql prompt after connecting to pgbench database.

    3. If everything worked properly, you should be able to view newly added tupples on the output from pgslave.


    Conclusion

    We just learned how to quickly configure a PostgreSQL database to act as a read-only mirror to another database. Note that in case the slave goes offline, it will automatically recover data from the master and would be soon up-to-date. Streaming replication is at database level, so you need to execute the command in part 11for each new database that needs to be replicated via streaming replication.

  • 2
    • 1
      The above solution seems to be missing several steps: Setup repmgr configuration file with the cluster name, node name, node # and conninfo register the master and slave node using: repmgr -f /var/lib/postgresql/repmgr/repmgr.conf --verbose master register And starting repmgrd
      – user22214
      Commented Apr 5, 2013 at 22:20
    • @DavidBrossard thanks for adding this info: I'd edit it in but I'm not sure where it goes. Perhaps either MHG will do it or you can yourself once you have enough rep :) Commented Apr 6, 2013 at 5:53

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.