How To Set Up PostgreSQL For High Availability and Replication With Hot Standby - Solutions Google Cloud Platform
How To Set Up PostgreSQL For High Availability and Replication With Hot Standby - Solutions Google Cloud Platform
Learn how to conãgure PostgreSQL to run in Hot Standby mode on Google Cloud Platform. You'll use two
Compute Engine instances. One instance will run the primary PostgreSQL server and the other instance will
run the standby server.
For most applications, data is a critical commodity. Storing data in one place is a risky proposition, so you
PostgreSQL is an open-source,
need to have a strategy and a plan in place to ensure that you can recover from a failure as quickly as
object-relational database system.
possible. One way to help prevent data loss is to store the same data on multiple servers and keep those
databases synchronized.
PostgreSQL, or Postgres, offers various ways to archive and replicate the primary database for backup, high-
availability, and load balancing scenarios. In Hot Standby mode, the system employs two or more servers:
A primary server runs the active database. This database accepts connections from clients and permits read-write operations.
One or more standby servers run a copy of the active database. These databases are conãgured to accept connections from clients and permit
read-only operations. If the primary database server fails, the system can fail over to the standby server, which makes the standby server the active
primary server.
The rest of this tutorial will use and discuss a single standby server.
Postgres uses write-ahead logging (WAL) to continuously archive database transactions. For each change made to the data ãles, WAL writes an
entry in a log ãle. The system uses these log entries to perform point-in-time restoration from archives and to keep the standby server up to date.
This means that when you set up Hot Standby, you're also setting up archiving.
The process of updating the standby server with WAL entries is called streaming replication. This process operates asynchronously, which means it
can take some time for the standby server to receive an update from the primary server. Though this delay can be very short, synchronization
between the servers is not instantaneous. If your application requires strict consistency between the database instances, you should consider
another approach.
Postgres doesn't provide software to automatically fail over when the primary server fails. This is a manual operation unless you use a third-party
solution to manage failover.
Load balancing is not automatic with Hot Standby. If load balancing is a requirement for your appliction, you must provide a load-balancing solution
that uses the primary server for read-write operations and the standby server for read-only operations.
Important: This tutorial covers a basic setup of two servers in a Hot Standby conãguration. The tutorial doesn't try to cover every conãguration that is available
to you for this scenario. For a complete understanding of how to optimize Postgres in standby scenarios, refer to the Postgres documentation.
Objectives
Set up two Compute Engine instances running Postgres.
Create a new table for a guestbook app.
Conãgure the primary server.
Back up the primary server to the standby server.
Conãgure the standby server to run in Hot Standby mode.
Start the standby server and test it.
Prerequisites
Select or create a Cloud Platform Console project.
https://cloud.google.com/solutions/setuppostgreshotstandby 1/6
4/17/2016 How to Set Up PostgreSQL for High Availability and Replication with Hot Standby Solutions — Google Cloud Platform
Important: This tutorial uses billable components of Google Cloud Platform, including:
Use the Google Cloud Platform Pricing Calculator to generate a cost estimate based on your projected usage. New Cloud Platform users may be eligible for a free
trial.
$ sudo ‐s
2. Run PSQL as user postgres and access the database named postgres:
$ sudo ‐u postgres psql postgres
3. At the PSQL prompt, enter the following command to create the table:
CREATE TABLE guestbook (visitor_email text, vistor_id serial, date timestamp, message text);
INSERT INTO guestbook (visitor_email, date, message) VALUES ( '[email protected]', current_date, 'This is a test.');
Don't exit the root shell. You'll use the root shell throughout this tutorial.
$ sudo ‐u postgres createuser ‐U postgres repuser ‐P ‐c 5 ‐‐replication
sudo ‐u postgres ensures that the createuser command runs as the user postgres. Otherwise, Postgres will try to run the command by using
peer authentication, which means the command will run under your Ubuntu user account. This account probably doesn't have the right privileges to
create the new user, which would cause an error.
The ‐U option tells the createuser command to use the user postgres to create the new user.
https://cloud.google.com/solutions/setuppostgreshotstandby 2/6
4/17/2016 How to Set Up PostgreSQL for High Availability and Replication with Hot Standby Solutions — Google Cloud Platform
The name of the new user is repuser. You'll enter that username in the conãguration ãles.
Important: For any system with an Internet connection, use a strong password to help keep the system secure.
‐c sets a limit for the number of connections for the new user. The value 5 is sufãcient for replication purposes.
In the SSH terminal for the primary server, enter the following command:
$ mkdir ‐p ../../var/lib/postgresql/main/mnt/server/archivedir
Edit pg_hba.conf
This conãguration ãle contains the settings for client authentication. You must add an entry for the user repuser to enable replication.
1. Edit the ãle. For PostgreSQL version 9.3, you can enter::
$ nano ../../etc/postgresql/9.3/main/pg_hba.conf
2. After the example replication entries, add the following lines. Replace <standby‐IP> with the external IP address of the standby server:
# Allow replication connections
host replication repuser <standby‐IP>/32 md5
Edit postgresql.conf
This conãguration ãle contains the main settings for Postgres. Here, you will modify the ãle to enable archiving and replication.
Important: Don't forget to uncomment any lines you edit in the conãguration ãles, or your changes won't take effect.
1. Edit the ãle. In the terminal for the primary server, enter the following command:
$ nano ../../etc/postgresql/9.3/main/postgresql.conf
2. In the WRITE AHEAD LOG section, in the Settings section, change the WAL level:
wal_level = hot_standby
archive_mode = on
4. Change the value for the archive command. This setting tells Postgres to write the archive ãles to the directory that you created in a previous step:
archive_command = 'test ! ‐f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
5. In the REPLICATION section, in the Sending Server(s) section, change the value for the maximum number of WAL sender processes:
max_wal_senders = 3
For this tutorial, the value of 3 is sufãcient to enable backup and replication.
$ sudo service postgresql restart
https://cloud.google.com/solutions/setuppostgreshotstandby 3/6
4/17/2016 How to Set Up PostgreSQL for High Availability and Replication with Hot Standby Solutions — Google Cloud Platform
$ sudo service postgresql stop
Important: Don't start the service again until all conãguration and backup steps are complete. You must bring up the standby server in a state where it is ready to
be a backup server. This means that all conãguration settings must be in place and the databases must be already synchronized. Otherwise, streaming
replication will fail to start.
1. Make sure you're running commands in the root shell. In the SSH terminal for the standby server, enter the following command:
$ sudo ‐s
Continue to use the root shell for the remainder of this tutorial.
2. The backup utility won't overwrite existing ãles, so you must rename the data directory on the standby server. Run the following command:
$ mv ../../var/lib/postgresql/9.3/main ../../var/lib/postgresql/9.3/main_old
3. Run the backup utility. Replace <primary‐IP> with the external IP address of the primary server.
$ sudo ‐u postgres pg_basebackup ‐h <primary IP> ‐D /var/lib/postgresql/9.3/main ‐U repuser ‐v ‐P ‐‐xlog‐method=stream
The backup utility will prompt you for the password for the user named repuser.
The backup process should take just a few moments. When it's done, you can move on to conãguring the standby server.
Edit postgresql.conf
For the standby server, you only need to change one setting in this ãle. Follow these steps:
1. Edit the ãle. In the terminal for the standby server, enter the following command:
$ nano ../../etc/postgresql/9.3/main/postgresql.conf
2. In the REPLICATION section, in the Standby Servers section, turn on Hot Standby and uncomment the line:
hot_standby = on
1. Copy the sample recovery ãle to the proper location. In the terminal for the standby server, enter the following command:
$ cp ‐avr ../../usr/share/postgresql/9.3/recovery.conf.sample /../../var/lib/postgresql/9.3/main/recovery.conf
$ nano /../../var/lib/postgresql/9.3/main/recovery.conf
standby_mode = on
4. Set the connection string to the primary server. Replace <primary‐external‐IP> with the external IP address of the primary server. Replace
<password> with the password for the user named repuser.
https://cloud.google.com/solutions/setuppostgreshotstandby 4/6
4/17/2016 How to Set Up PostgreSQL for High Availability and Replication with Hot Standby Solutions — Google Cloud Platform
primary_conninfo = 'host=<primary‐external‐IP> port=5432 user=repuser password=<password>'
trigger_file = '/tmp/postgresql.trigger.5432'
The trigger_file path that you specify is the location where you can add a ãle when you want the system to fail over to the standby server. The
presence of the ãle "triggers" the failover. Alternatively, you can use the pg_ctl promote command to trigger failover.
$ service postgresql start
Recall that you have already added one row to the table on the primary server. Start by verifying that the standby server has the same information.
$ sudo ‐u postgres psql postgres
select * from guestbook;
You should see that the table contains the single row that you originally added. Now, add a second row on the primary server.
$ sudo ‐u postgres psql postgres
INSERT INTO guestbook (visitor_email, date, message) VALUES ( '[email protected]', current_date, 'Now we are replicating.');
5. Switch back to the standby server terminal and repeat the query for all rows of the guestbook:
select * from guestbook;
You should now see that the standby server has received the update from the primary server.
Troubleshooting
After completing all the steps, if you're not seeing the data replicate, you might have missed a step or some small detail. Common mistakes include:
If you ãnd yourself in this state, here are the steps to follow:
1. Look at the Postgres log on each server. These logs can contain information that will help you troubleshoot the issue.
$ less ../../var/log/postgresql/postgresql‐9.3‐main.log
2. Check the primary server settings. If there are mistakes, ãx them and then restart the server.
https://cloud.google.com/solutions/setuppostgreshotstandby 5/6
4/17/2016 How to Set Up PostgreSQL for High Availability and Replication with Hot Standby Solutions — Google Cloud Platform
4. Check the standby server settings and correct them if needed.
5. On the standby server, rename the main folder to something new, such as main_old_2:
$ mv ../../var/lib/postgresql/9.3/main ../../var/lib/postgresql/9.3/main_old_2
6. On the standby server, run pgbasebackup again to synchronize the data. Substitute <primary‐IP> with your primary server's external IP address:
$ sudo ‐u postgres pg_basebackup ‐h <primary‐IP> ‐D /var/lib/postgresql/9.3/main ‐U repuser ‐v ‐P ‐‐xlog‐method=stream
7. The main folder now needs a copy of recovery.conf. You can simply copy it from the folder that you renamed to main_old_2:
$ cp ../../var/lib/postgresql/9.3/main_old_2/recovery.conf ../../var/lib/postgresql/9.3/main/recovery.conf
Cleaning up
After you've ãnished the PostgreSQL tutorial, you can clean up the resources you created on Google Cloud Platform so you won't be billed for them in the
future. The following sections describe how to delete or turn off these resources.
If you used an existing project, you'll also delete any other work you've done in the project.
You can't reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside
the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL remain available. If you used the automatically generated
project ID, this might not be a concern.
To delete the project, in the Cloud Platform Console, use the Projects page. Click the trash can icon to the right of the project name.
Deleting instances
You can use the Cloud Platform Console to stop or delete Compute Engine instances. Select the check boxes and then click Stop or Delete.
Next steps
Explore the PostgreSQL documentation.
Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under
the Apache 2.0 License. For details, see our Site Policies.
https://cloud.google.com/solutions/setuppostgreshotstandby 6/6