SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Hot Stand By
Setup, Failover and Rebuilding the Master
3/6/2014
Confidentialinformation,forinternaluseonly
Prepare the Instance
 Install Postgres on Servers which are going to hold Primary and Secondary
database
 Setup and configure the database cluster on Primary servers
 In this example:
 Primary DB Server:
 dbserver1
 192.168.160.147
 Data directory: /opt/PostgresPlus/9.2AS/data
 Port: 5444
 Stand by DB Server
 dbserver2
 192.168.160.150
 Data Directory: /opt/PostgresPlus/9.2AS/data2
 Port: 5222
2
Confidentialinformation,forinternaluseonly
Edit postgresql.conf and pg_hba.conf on
master
 wal_level = hot_standby (mandatory)
 max_wal_senders = 3 (mandatory to be set to a positive integer)
 wal_keep_segments = 128 (optional/depending on load)
 replication_timeout = 5 sec (optional)
 hot_standby = on (effective only for hot stand by server)
Add entry in pg_hba.conf
• host replication all 192.168.0.0/16 trust
3
Confidentialinformation,forinternaluseonly
Take a Backup and restore on Secondary
 Take a cold backup of Primary DB Instance/Cluster
 Restore the same for creating the DB cluster/instance on
Secondary server
4
Confidentialinformation,forinternaluseonly
Create recovery.conf in Secondary Server
 standby_mode = 'on'
 primary_conninfo = 'host=192.168.160.147 port=5444 user=enterprisedb
password=ashnik'
 restore_command = 'scp enterprisedb@192.168.160.147:/mnt/arch/%f %p'
# optional
# needs archiving command to be enabled on primary
 recovery_target_timeline = 'latest' #optional
 trigger_file = '/opt/PostgresPlus/9.2AS/data2/recover.trigger'
5
Confidentialinformation,forinternaluseonly
Start the servers
 Start the secondary server
 There will be a warning in log files that primary server is not
available, ignore that
 Start the primary server
6
Confidentialinformation,forinternaluseonly
Test Replication
On Primary:
edb=# insert into replication_test values (2);
INSERT 0 1
On Secondary:
edb=# select * from replication_test;
test_column
-------------
1
2
(2 rows)
Secondary server is read-only:
edb=# insert into replication_test values (3);
ERROR: cannot execute INSERT in a read-only transaction
7
Confidentialinformation,forinternaluseonly
Triggering the Failover
 To, Trigger a failure on Primary and create the recovery trigger file (manually, but can be scripted too)
touch opt/PostgresPlus/9.2AS/data2/recover.trigger
 Logic to script the above step:
while( pg_ctl -h 192.168.160.147 –p 5444 -c "select 1 “)
{
; #do nothing
}
touch opt/PostgresPlus/9.2AS/data2/recover.trigger
 Once completed, the recovery.conf will change to recover.done
 Connect to secondary db and execute insert to confirm the failover
edb=# insert into replication_test values (4);
INSERT 0 1
Or execute select pg_is_in_recovery(); (output must be “f”) to confirm recovery is completed
 Point the database/Virtual IP to new database server 8
Confidentialinformation,forinternaluseonly
Triggering the Switchover
 Disconnect all the application from Primary Node
 Shutdown the primary database
 To, Trigger a failure on Primary and create the recovery trigger file
touch opt/PostgresPlus/9.2AS/data2/recover.trigger
 Once completed, the recovery.conf will change to recover.done
 Connect to secondary db and execute insert
edb=# insert into replication_test values (4);
INSERT 0 1
Or execute select pg_is_in_recovery(); (output must be “f”) to confirm recovery is completed
 Point the database/Virtual IP to new database server
9
Confidentialinformation,forinternaluseonly
Rebuilding the Master
 Let’s remove the data directory on Old Primary (dbserver1)
rm -Rf /opt/PostgresPlus/9.2AS/data
 Restore the primary server from Secondary server (Switched to Master
now: dbserver2):
pg_basebackup -D /opt/PostgresPlus/9.2AS/data -h 192.168.160.150 -W -p 5222 -Fp --xlog-method=stream
#This step does not need down time
 Change the port number in postgresql.conf on restored data directory
 Change the recovery.done to recovery.conf in data directory copied just
now and make changes in configuration to point to new master
 Start the old primary instance as new hot standby
10
Confidentialinformation,forinternaluseonly
Monitoring the Replication
Check if the current node is master or slave:
• SELECT pg_is_in_recovery();
See the current snapshot on master and slave:
• SELECT txid_current_snapshot();
11
12
Sameer Kumar
Ashnik Pte Ltd, Singapore
www.ashnik.com | sameer.kumar@ashnik.com

Weitere ähnliche Inhalte

Was ist angesagt?

Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
Denish Patel
 
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
Wei Shan Ang
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
elliando dias
 
plProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancerplProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancer
elliando dias
 

Was ist angesagt? (19)

PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Replication in 10  Minutes - SCALEPostgreSQL Replication in 10  Minutes - SCALE
PostgreSQL Replication in 10 Minutes - SCALE
 
Advanced Postgres Monitoring
Advanced Postgres MonitoringAdvanced Postgres Monitoring
Advanced Postgres Monitoring
 
PostgreSQL Replication High Availability Methods
PostgreSQL Replication High Availability MethodsPostgreSQL Replication High Availability Methods
PostgreSQL Replication High Availability Methods
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
 
Logical replication with pglogical
Logical replication with pglogicalLogical replication with pglogical
Logical replication with pglogical
 
What is new in PostgreSQL 14?
What is new in PostgreSQL 14?What is new in PostgreSQL 14?
What is new in PostgreSQL 14?
 
Evolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best PracticesEvolution of MongoDB Replicaset and Its Best Practices
Evolution of MongoDB Replicaset and Its Best Practices
 
Logical Replication in PostgreSQL - FLOSSUK 2016
Logical Replication in PostgreSQL - FLOSSUK 2016Logical Replication in PostgreSQL - FLOSSUK 2016
Logical Replication in PostgreSQL - FLOSSUK 2016
 
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
 
Basics of Logical Replication,Streaming replication vs Logical Replication ,U...
Basics of Logical Replication,Streaming replication vs Logical Replication ,U...Basics of Logical Replication,Streaming replication vs Logical Replication ,U...
Basics of Logical Replication,Streaming replication vs Logical Replication ,U...
 
Case Studies on PostgreSQL
Case Studies on PostgreSQLCase Studies on PostgreSQL
Case Studies on PostgreSQL
 
plProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancerplProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancer
 
GitLab PostgresMortem: Lessons Learned
GitLab PostgresMortem: Lessons LearnedGitLab PostgresMortem: Lessons Learned
GitLab PostgresMortem: Lessons Learned
 
GOTO 2013: Why Zalando trusts in PostgreSQL
GOTO 2013: Why Zalando trusts in PostgreSQLGOTO 2013: Why Zalando trusts in PostgreSQL
GOTO 2013: Why Zalando trusts in PostgreSQL
 
Building Spark as Service in Cloud
Building Spark as Service in CloudBuilding Spark as Service in Cloud
Building Spark as Service in Cloud
 
PostgreSQL and RAM usage
PostgreSQL and RAM usagePostgreSQL and RAM usage
PostgreSQL and RAM usage
 
PostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized WorldPostgreSQL High Availability in a Containerized World
PostgreSQL High Availability in a Containerized World
 
SQL Server vs Postgres
SQL Server vs PostgresSQL Server vs Postgres
SQL Server vs Postgres
 

Ähnlich wie Streaming replication in PostgreSQL

oracle upgradation
oracle upgradationoracle upgradation
oracle upgradation
influxbob
 
Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)
Denish Patel
 
The Magic of Hot Streaming Replication, Bruce Momjian
The Magic of Hot Streaming Replication, Bruce MomjianThe Magic of Hot Streaming Replication, Bruce Momjian
The Magic of Hot Streaming Replication, Bruce Momjian
Fuenteovejuna
 
Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)
Denish Patel
 
Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)
Denish Patel
 

Ähnlich wie Streaming replication in PostgreSQL (20)

Best Practices: Migrating a Postgres Production Database to the Cloud
Best Practices: Migrating a Postgres Production Database to the CloudBest Practices: Migrating a Postgres Production Database to the Cloud
Best Practices: Migrating a Postgres Production Database to the Cloud
 
How to Replicate PostgreSQL Database
How to Replicate PostgreSQL DatabaseHow to Replicate PostgreSQL Database
How to Replicate PostgreSQL Database
 
oracle upgradation
oracle upgradationoracle upgradation
oracle upgradation
 
Document Management: Opendocman and LAMP installation on Cent OS
Document Management: Opendocman and LAMP installation on Cent OSDocument Management: Opendocman and LAMP installation on Cent OS
Document Management: Opendocman and LAMP installation on Cent OS
 
Creating a physical standby database 11g on windows
Creating a physical standby database 11g on windowsCreating a physical standby database 11g on windows
Creating a physical standby database 11g on windows
 
Database upgradation
Database upgradationDatabase upgradation
Database upgradation
 
PuppetConf 2016: An Introduction to Measuring and Tuning PE Performance – Cha...
PuppetConf 2016: An Introduction to Measuring and Tuning PE Performance – Cha...PuppetConf 2016: An Introduction to Measuring and Tuning PE Performance – Cha...
PuppetConf 2016: An Introduction to Measuring and Tuning PE Performance – Cha...
 
Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)Out of the Box Replication in Postgres 9.4(PgConfUS)
Out of the Box Replication in Postgres 9.4(PgConfUS)
 
Automating Disaster Recovery PostgreSQL
Automating Disaster Recovery PostgreSQLAutomating Disaster Recovery PostgreSQL
Automating Disaster Recovery PostgreSQL
 
Upgrading 11i E-business Suite to R12 E-business Suite
Upgrading 11i E-business Suite to R12 E-business SuiteUpgrading 11i E-business Suite to R12 E-business Suite
Upgrading 11i E-business Suite to R12 E-business Suite
 
Cake php
Cake phpCake php
Cake php
 
Cloning Oracle EBS R12: A Step by Step Procedure
Cloning Oracle EBS R12: A Step by Step ProcedureCloning Oracle EBS R12: A Step by Step Procedure
Cloning Oracle EBS R12: A Step by Step Procedure
 
The Magic of Hot Streaming Replication, Bruce Momjian
The Magic of Hot Streaming Replication, Bruce MomjianThe Magic of Hot Streaming Replication, Bruce Momjian
The Magic of Hot Streaming Replication, Bruce Momjian
 
Install Oracle 12c Golden Gate On Oracle Linux
Install Oracle 12c Golden Gate On Oracle LinuxInstall Oracle 12c Golden Gate On Oracle Linux
Install Oracle 12c Golden Gate On Oracle Linux
 
Oracle 11g Installation With ASM and Data Guard Setup
Oracle 11g Installation With ASM and Data Guard SetupOracle 11g Installation With ASM and Data Guard Setup
Oracle 11g Installation With ASM and Data Guard Setup
 
Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)
 
Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)Out of the Box Replication in Postgres 9.4(PgCon)
Out of the Box Replication in Postgres 9.4(PgCon)
 
Upgrading hadoop
Upgrading hadoopUpgrading hadoop
Upgrading hadoop
 
Oracle Data Guard
Oracle Data GuardOracle Data Guard
Oracle Data Guard
 
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRestPGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
PGDay.Amsterdam 2018 - Stefan Fercot - Save your data with pgBackRest
 

Mehr von Ashnikbiz

Mehr von Ashnikbiz (20)

CloudOps_tool.pptx
CloudOps_tool.pptxCloudOps_tool.pptx
CloudOps_tool.pptx
 
Webinar_CloudOps final.pptx
Webinar_CloudOps final.pptxWebinar_CloudOps final.pptx
Webinar_CloudOps final.pptx
 
Autoscaling in Kubernetes (K8s)
Autoscaling in Kubernetes (K8s)Autoscaling in Kubernetes (K8s)
Autoscaling in Kubernetes (K8s)
 
Why and how to use Kubernetes for scaling of your multi-tier (n-tier) appli...
Why and how to use Kubernetes  for scaling of your  multi-tier (n-tier) appli...Why and how to use Kubernetes  for scaling of your  multi-tier (n-tier) appli...
Why and how to use Kubernetes for scaling of your multi-tier (n-tier) appli...
 
Zero trust in a multi tenant environment
Zero trust in a multi tenant environment  Zero trust in a multi tenant environment
Zero trust in a multi tenant environment
 
Deploy and automate ‘Secrets Management’ for a multi-cloud environment
Deploy and automate ‘Secrets Management’ for a multi-cloud environmentDeploy and automate ‘Secrets Management’ for a multi-cloud environment
Deploy and automate ‘Secrets Management’ for a multi-cloud environment
 
Deploy, move and manage Postgres across cloud platforms
Deploy, move and manage Postgres across cloud platformsDeploy, move and manage Postgres across cloud platforms
Deploy, move and manage Postgres across cloud platforms
 
Deploy, move and manage Postgres across cloud platforms
Deploy, move and manage Postgres across cloud platformsDeploy, move and manage Postgres across cloud platforms
Deploy, move and manage Postgres across cloud platforms
 
The Best Approach For Multi-cloud Infrastructure Provisioning-2
The Best Approach For Multi-cloud Infrastructure Provisioning-2The Best Approach For Multi-cloud Infrastructure Provisioning-2
The Best Approach For Multi-cloud Infrastructure Provisioning-2
 
The Best Approach For Multi-cloud Infrastructure Provisioning
The Best Approach For Multi-cloud Infrastructure ProvisioningThe Best Approach For Multi-cloud Infrastructure Provisioning
The Best Approach For Multi-cloud Infrastructure Provisioning
 
Which PostgreSQL is right for your multi cloud strategy? P2
Which PostgreSQL is right for your multi cloud strategy? P2Which PostgreSQL is right for your multi cloud strategy? P2
Which PostgreSQL is right for your multi cloud strategy? P2
 
Which PostgreSQL is right for your multi cloud strategy? P1
Which PostgreSQL is right for your multi cloud strategy? P1Which PostgreSQL is right for your multi cloud strategy? P1
Which PostgreSQL is right for your multi cloud strategy? P1
 
Reduce the complexities of managing Kubernetes clusters anywhere 2
Reduce the complexities of managing Kubernetes clusters anywhere 2Reduce the complexities of managing Kubernetes clusters anywhere 2
Reduce the complexities of managing Kubernetes clusters anywhere 2
 
Reduce the complexities of managing Kubernetes clusters anywhere
Reduce the complexities of managing Kubernetes clusters anywhereReduce the complexities of managing Kubernetes clusters anywhere
Reduce the complexities of managing Kubernetes clusters anywhere
 
Enhance your multi-cloud application performance using Redis Enterprise P2
Enhance your multi-cloud application performance using Redis Enterprise P2Enhance your multi-cloud application performance using Redis Enterprise P2
Enhance your multi-cloud application performance using Redis Enterprise P2
 
Enhance your multi-cloud application performance using Redis Enterprise P1
Enhance your multi-cloud application performance using Redis Enterprise P1Enhance your multi-cloud application performance using Redis Enterprise P1
Enhance your multi-cloud application performance using Redis Enterprise P1
 
Gain multi-cloud versatility with software load balancing designed for cloud-...
Gain multi-cloud versatility with software load balancing designed for cloud-...Gain multi-cloud versatility with software load balancing designed for cloud-...
Gain multi-cloud versatility with software load balancing designed for cloud-...
 
Gain multi-cloud versatility with software load balancing designed for cloud-...
Gain multi-cloud versatility with software load balancing designed for cloud-...Gain multi-cloud versatility with software load balancing designed for cloud-...
Gain multi-cloud versatility with software load balancing designed for cloud-...
 
Enterprise-class security with PostgreSQL - 1
Enterprise-class security with PostgreSQL - 1Enterprise-class security with PostgreSQL - 1
Enterprise-class security with PostgreSQL - 1
 
Enterprise-class security with PostgreSQL - 2
Enterprise-class security with PostgreSQL - 2Enterprise-class security with PostgreSQL - 2
Enterprise-class security with PostgreSQL - 2
 

Streaming replication in PostgreSQL

  • 1. Hot Stand By Setup, Failover and Rebuilding the Master 3/6/2014
  • 2. Confidentialinformation,forinternaluseonly Prepare the Instance  Install Postgres on Servers which are going to hold Primary and Secondary database  Setup and configure the database cluster on Primary servers  In this example:  Primary DB Server:  dbserver1  192.168.160.147  Data directory: /opt/PostgresPlus/9.2AS/data  Port: 5444  Stand by DB Server  dbserver2  192.168.160.150  Data Directory: /opt/PostgresPlus/9.2AS/data2  Port: 5222 2
  • 3. Confidentialinformation,forinternaluseonly Edit postgresql.conf and pg_hba.conf on master  wal_level = hot_standby (mandatory)  max_wal_senders = 3 (mandatory to be set to a positive integer)  wal_keep_segments = 128 (optional/depending on load)  replication_timeout = 5 sec (optional)  hot_standby = on (effective only for hot stand by server) Add entry in pg_hba.conf • host replication all 192.168.0.0/16 trust 3
  • 4. Confidentialinformation,forinternaluseonly Take a Backup and restore on Secondary  Take a cold backup of Primary DB Instance/Cluster  Restore the same for creating the DB cluster/instance on Secondary server 4
  • 5. Confidentialinformation,forinternaluseonly Create recovery.conf in Secondary Server  standby_mode = 'on'  primary_conninfo = 'host=192.168.160.147 port=5444 user=enterprisedb password=ashnik'  restore_command = 'scp enterprisedb@192.168.160.147:/mnt/arch/%f %p' # optional # needs archiving command to be enabled on primary  recovery_target_timeline = 'latest' #optional  trigger_file = '/opt/PostgresPlus/9.2AS/data2/recover.trigger' 5
  • 6. Confidentialinformation,forinternaluseonly Start the servers  Start the secondary server  There will be a warning in log files that primary server is not available, ignore that  Start the primary server 6
  • 7. Confidentialinformation,forinternaluseonly Test Replication On Primary: edb=# insert into replication_test values (2); INSERT 0 1 On Secondary: edb=# select * from replication_test; test_column ------------- 1 2 (2 rows) Secondary server is read-only: edb=# insert into replication_test values (3); ERROR: cannot execute INSERT in a read-only transaction 7
  • 8. Confidentialinformation,forinternaluseonly Triggering the Failover  To, Trigger a failure on Primary and create the recovery trigger file (manually, but can be scripted too) touch opt/PostgresPlus/9.2AS/data2/recover.trigger  Logic to script the above step: while( pg_ctl -h 192.168.160.147 –p 5444 -c "select 1 “) { ; #do nothing } touch opt/PostgresPlus/9.2AS/data2/recover.trigger  Once completed, the recovery.conf will change to recover.done  Connect to secondary db and execute insert to confirm the failover edb=# insert into replication_test values (4); INSERT 0 1 Or execute select pg_is_in_recovery(); (output must be “f”) to confirm recovery is completed  Point the database/Virtual IP to new database server 8
  • 9. Confidentialinformation,forinternaluseonly Triggering the Switchover  Disconnect all the application from Primary Node  Shutdown the primary database  To, Trigger a failure on Primary and create the recovery trigger file touch opt/PostgresPlus/9.2AS/data2/recover.trigger  Once completed, the recovery.conf will change to recover.done  Connect to secondary db and execute insert edb=# insert into replication_test values (4); INSERT 0 1 Or execute select pg_is_in_recovery(); (output must be “f”) to confirm recovery is completed  Point the database/Virtual IP to new database server 9
  • 10. Confidentialinformation,forinternaluseonly Rebuilding the Master  Let’s remove the data directory on Old Primary (dbserver1) rm -Rf /opt/PostgresPlus/9.2AS/data  Restore the primary server from Secondary server (Switched to Master now: dbserver2): pg_basebackup -D /opt/PostgresPlus/9.2AS/data -h 192.168.160.150 -W -p 5222 -Fp --xlog-method=stream #This step does not need down time  Change the port number in postgresql.conf on restored data directory  Change the recovery.done to recovery.conf in data directory copied just now and make changes in configuration to point to new master  Start the old primary instance as new hot standby 10
  • 11. Confidentialinformation,forinternaluseonly Monitoring the Replication Check if the current node is master or slave: • SELECT pg_is_in_recovery(); See the current snapshot on master and slave: • SELECT txid_current_snapshot(); 11
  • 12. 12 Sameer Kumar Ashnik Pte Ltd, Singapore www.ashnik.com | sameer.kumar@ashnik.com