Easy setup replication on PostgreSQL

image
The need arose to quickly and simply as possible to organize the data replication from database server to the backup server. A simple and intuitive way on the Internet and was not found, this had parts to assemble information, which became this article.

task. The source data


So, we have a database server that provides clients and the backup server on which it is necessary to configure replication from the primary database.
In my case, a PostgreSQL 9.2.1, which is installed on both servers and supports streaming replication. Assume that the database on the primary server deployed and running on only backup installed but not configured PostgreSQL. For example, take the IP address 192.168.1.1 for the address of the server, IP address 192.168.1.2 is the address of the backup.

Custom main database server


In the section "the Role of input" using PgAdmin create user (role) repl rights "Can create streaming replication and backups". Add in the pg_hba or through a "Configuration server" in PgAdmin create a string that allows the user repl connection to the database.

the host replication repl 192.168.1.2/32 trust

In postgresql.conf made the following changes:
the the the the the
Original value Change Description
#max_wal_senders = 1 max_wal_senders = 2 the Number of standby servers that can connect to the main server
#wal_keep_segments = 32 wal_keep_segments = 32 (you can put a 256) How to store segments. The number should be chosen such that the backup server had time to pick up and handle. I put 256 to the day wal files was not erased
#wal_level = hot_standby wal_level = hot_standby hot_standby adds information required to run read-only queries on a standby server
#checkpoint_segments = 3 checkpoint_segments = 16 you Can increase the number of segments in WAL-log

Custom backup server


Stop service Postgresql 9.2 on the backup server and clean up the data folder that is created when you install PostgreSQL, for example D:\database. After this, run backup from the command line:

"C:\PostgreSQL\bin\pg_basebackup.exe" --host=192.168.1.1 --port=5432 --username=repl -D "D:\database"

Custom postgresql.conf D:\database
the the
Original value Change Description
#hot_standby = off hot_standby = on Allow read-only queries to the database during the restore process

A custom recovery.conf D:\database
the the the the
Value Description
standby_mode = 'on' Enable recovery mode and to act as a backup server (slave)
primary_conninfo = 'host=192.168.1.1 port=5432 user=repl' the Parameters to connect to the main server
trigger_file = 'D:\\database\\end_trig' If you create a file with the name end_trig in the specified folder, the server will exit and replication will become a regular server

Start the Postgresql service on the backup server. Should start without errors. Check the replication job: changes in a table on the primary server, and check — if they reflected on the backup.

configuring the archive logs (if needed)


In postgresql.conf on the master server, include the following:
the
archive_mode = on archive_command = 'copy "%p" "e:\\Backup\\%f"'

Folder e:\Backup will fall the archives of the logs. (Note, they can score all the disk space, you need to configure the cleanup at the overflow)

To use the archives logs, copy logs to a folder from which you will recover and in recovery.conf to add the line

restore_command = 'copy "e:\\Backup\\%f" "%p"'

Actions in case of failures


If the failed backup server, then stop it the Postgres service and performed all actions as "Custom backup server".
If the failed primary server, it is necessary to translate the backup server to normal operation: it is necessary to create a file end_trig in the folder as specified in the recovery.conf trigger_file = 'D:\\database\\end_trig' b do vacuum and reindex the database.

notes


Replication can be configured as from Windows XP to Windows 7, and Vice versa.
For Windows 7 and Wndows XP in pg_hba different settings, because in XP there is no Protocol for ip6, then the line should be commented out.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Why I left Google Zurich

2000 3000 icons ready — become a sponsor! (the table of orders)

New web-interface for statistics and listen to the calls for IP PBX Asterisk