{"id":2741,"date":"2020-02-06T10:53:40","date_gmt":"2020-02-06T07:53:40","guid":{"rendered":"https:\/\/www.fatihacar.com\/blog\/?p=2741"},"modified":"2020-08-05T18:36:30","modified_gmt":"2020-08-05T15:36:30","slug":"postgresql-12-streaming-replication-installation-and-failover-operations-on-centos-7","status":"publish","type":"post","link":"http:\/\/www.fatihacar.com\/blog\/postgresql-12-streaming-replication-installation-and-failover-operations-on-centos-7\/","title":{"rendered":"PostgreSQL 12 Streaming Replication Installation and Failover Operations on CentOS 7"},"content":{"rendered":"<p>PostgreSQL has few feature for standby technology. I generally use streaming replication. This is very easy and confident. Streaming replication use a replication user for replication operations.Streaming replication allows a standby server to stay more up-to-date than is possible with file-based log shipping. The standby connects to the primary, which streams WAL records to the standby as they&#8217;re generated, without waiting for the WAL file to be filled.<\/p>\n<p>Streaming replication is asynchronous by default, in which case there is a small delay between committing a transaction in the primary and the changes becoming visible in the standby.<\/p>\n<p>If you use streaming replication without file-based continuous archiving, the server might recycle old WAL segments before the standby has received them. If this occurs, the standby will need to be reinitialized from a new base backup. You can avoid this by setting wal_keep_segments to a value large enough to ensure that WAL segments are not recycled too early, or by configuring a replication slot for the standby. If you set up a WAL archive that&#8217;s accessible from the standby, these solutions are not required, since the standby can always use the archive to catch up provided it retains enough segments.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2742\" src=\"https:\/\/www.fatihacar.com\/blog\/resimler\/postgresql-12-streaming-replication.jpg\" alt=\"\" width=\"824\" height=\"347\" srcset=\"http:\/\/www.fatihacar.com\/blog\/resimler\/postgresql-12-streaming-replication.jpg 824w, http:\/\/www.fatihacar.com\/blog\/resimler\/postgresql-12-streaming-replication-300x126.jpg 300w, http:\/\/www.fatihacar.com\/blog\/resimler\/postgresql-12-streaming-replication-768x323.jpg 768w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><\/p>\n<p><strong>PostgreSQL 12 New Standby Structure<\/strong><\/p>\n<p>recovery.conf file is no longer used. Parameters will write in postgresql.conf file and postgresql.auto.conf file. Some parameter names changed. trigger_file parameter has been changed to <strong>promote_trigger_file<\/strong>. standby_mode parameter is no longer used. <strong>standby.signal<\/strong> file will use to determine standby database.<\/p>\n<p>Also, you can change parameters with <strong>ALTER SYSTEM<\/strong> command. Reload is adequate to apply changes for archive_cleanup_command, promote_trigger_file, recovery_end_command, recovery_min_apply_delay parameters. You can reload database with psql -c &#8220;select pg_reload_conf()&#8221; or PSQL&gt; select pg_reload_conf(); or systemctl reload postgresql-12.service commands.<\/p>\n<p><strong>Test Systems Features<\/strong><\/p>\n<blockquote><p><strong>PG Version :<\/strong> 12.1<br \/>\n<strong>Primary DB IP :<\/strong> 192.168.10.11<br \/>\n<strong>Pimary DB Hostname :<\/strong> pg01<br \/>\n<strong>Hot Standby DB IP :<\/strong> 192.168.10.12<br \/>\n<strong>Host Standby DB Hostname :<\/strong> pg02<br \/>\n<strong>Virtual Service IP :<\/strong> 192.168.10.10<br \/>\n<strong>OS :<\/strong> Centos 7 x64<br \/>\n<strong>Data directory :<\/strong> \/pgdata\/data<br \/>\n<strong>Archive direcytory :<\/strong> \/pgdata\/ARCHIVELOG<\/p><\/blockquote>\n<p><strong>1- Keygen Generate And Copy<\/strong><\/p>\n<blockquote><p>[postgres@pg01 ~] ssh-keygen -t rsa<br \/>\n[postgres@pg02 ~] ssh-keygen -t rsa<br \/>\n[postgres@pg01 ~] ssh-copy-id -i .ssh\/id_rsa.pub postgres@pg02<br \/>\n[postgres@pg02 ~] ssh-copy-id -i .ssh\/id_rsa.pub postgres@pg01<\/p><\/blockquote>\n<p><strong>2- Create Streaming Replication User on Primary DB<\/strong><\/p>\n<blockquote><p>psql# createuser -U postgres -p 5432 repuser -P &#8211;replication<\/p><\/blockquote>\n<p><strong>3- Primary DB pg_hba.conf configuratio<\/strong>n<\/p>\n<blockquote><p>[root@pg01 ~] vi \/pgdata\/data\/pg_hba.conf<\/p>\n<p>Add below line<\/p>\n<p>host replication repuser 192.168.10.12\/32 md5<\/p>\n<p>[root@pg01 ~] systemctl reload postgresql-12.service<\/p><\/blockquote>\n<p><strong>4- Create Wal Archive Directory on Standby Server<\/strong><\/p>\n<blockquote><p>[root@pg02 ~] mkdir -p \/pgdata\/ARCHIVELOG<\/p><\/blockquote>\n<p><strong>5- Configure postgresql.conf on Primary DB<\/strong><\/p>\n<blockquote><p>[root@pg01 ~] vi \/pgdata\/data\/postgresql.conf<\/p>\n<p>Parameters change like below<\/p>\n<p>listen_addresses = &#8216;192.168.10.10&#8217;<\/p>\n<p>archive_mode = on # enables archiving; off, on, or always<br \/>\narchive_command = &#8216;rsync -a %p postgres@192.168.10.12:\/pgdata\/ARCHIVELOG\/%f&#8217;<\/p>\n<p>[root@pg01 ~] systemctl restart postgresql-12.service<\/p><\/blockquote>\n<p><strong>6- Take basebackup<\/strong><\/p>\n<blockquote><p>You can use pg_basebackup while primary db is start or stop. Stop status is more confident.<\/p>\n<p>\/pgdata\/data directory has to be empty on standby server. pg_basebackup command will fill up that directory and will create <strong>postgresql.auto.conf<\/strong> and <strong>standby.signal<\/strong> file.<\/p>\n<p>[postgres@pg02 ~] \/usr\/pgsql-12\/bin\/pg_basebackup -h 192.168.10.10 -D \/pgdata\/data -U repuser -p 5432 -v -P &#8211;wal-method=stream &#8211;write-recovery-conf<\/p><\/blockquote>\n<p><strong>7- Postgresql.conf configuration on Standby Server<\/strong><\/p>\n<blockquote><p>Primary DB configuration has to be disabled with # mark.<\/p>\n<p>listen_address = &#8216;*&#8217;<\/p>\n<p>#archive_mode = on<br \/>\n#archive_command = &#8216;rsync -a %p postgres@192.168.10.12:\/pgdata\/ARCHIVELOG\/%f&#8217;<\/p><\/blockquote>\n<blockquote><p>restore_command = &#8216;cp \/pgdata\/ARCHIVELOG\/%f %p&#8217; # e.g. &#8216;cp \/mnt\/server\/archivedir\/%f %p&#8217;<br \/>\narchive_cleanup_command = &#8216;\/usr\/pgsql-12\/bin\/pg_archivecleanup \/pgdata\/ARCHIVELOG %r<\/p><\/blockquote>\n<blockquote><p>If you want to delay to apply committed values. You can use recovery_min_apply_delay parameter.<\/p>\n<p>recovery_min_apply_delay = 5min<\/p>\n<p>This parameter provides 5 minutes delay. When you commit a transaction at primary side, hot standby will apply this transaction 5 minutes later.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>8- Check postgresql.auto.conf file on Standby Server<\/strong><\/p>\n<blockquote><p>[root@pg02 ~] more \/pgdata\/data\/postgresql.auto.conf<\/p>\n<p>primary_conninfo = &#8216;user=repuser password=yourpass host=192.168.10.10 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any&#8217;<\/p><\/blockquote>\n<p><strong>9- Start Hot Standby PostgreSQL Service<\/strong><\/p>\n<blockquote><p>[root@pg02 ~] systemctl start postgresql-12.service<\/p><\/blockquote>\n<p><strong>10- Test<\/strong><\/p>\n<blockquote><p><strong>Primary DB<\/strong><br \/>\nReplication check<br \/>\npsql# select * from pg_stat_replication ;<\/p>\n<p>psql# create table test (name text);<br \/>\npsql# insert into test values(&#8216;Fatih Acar&#8217;);<\/p>\n<p><strong>Hot Standby DB<\/strong><br \/>\nReceiver check<br \/>\npsql# select * from pg_stat_wal_receiver;<\/p>\n<p>Receiver process check<br \/>\nps -ef | grep receiver<br \/>\npostgres 20936 20919 0 17:04 ? 00:00:00 postgres: walreceiver\u00a0 streaming 0\/9000268<\/p>\n<p>psql# select * from test;<\/p>\n<p>name<br \/>\n&#8212;&#8212;&#8212;&#8211;<br \/>\nFatih Acar<\/p><\/blockquote>\n<p><strong>Failover Operations<\/strong><\/p>\n<p>We have 3 methods to do failover operation.<\/p>\n<blockquote>\n<p><strong>Step 1<\/strong><\/p>\n<p><strong>Method 1<\/strong><\/p>\n<p>Create \/pgdata\/data\/failover.uygula file that is wrote in postgresql.conf file promote_trigger_file parameter.<\/p>\n<p>Standby DB will be primary db (read-write mode) when promote_trigger_file exist.<\/p>\n<p>Also, you can see standby.signal file disappear.<\/p>\n<p><strong>Method 2<\/strong><\/p>\n<p>If you want to use <strong>pg_ctl promote<\/strong> or to call <strong>pg_promote<\/strong> to failover operation, promote_trigger_file is not required.<\/p>\n<p>[postgres@pg02 ~] pg_ctl promote -D \/pgdata\/data\/<\/p>\n<p>waiting for server to promote&#8230;. done<\/p>\n<p>server promoted<\/p>\n<p><strong>Method 3<\/strong><\/p>\n<p>psql# select pg_promote();<\/p>\n<p>pg_promote<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nt<br \/>\n(1 row)<\/p><\/blockquote>\n<blockquote><p><strong>Step 2<\/strong><\/p>\n<p>If you use virtual service IP address for primary database, you can take virtual service IP on Standby Server<\/p>\n<p>ifconfig eth0:0 192.168.10.10 netmask 255.255.255.0<\/p><\/blockquote>\n<p>Source : www.postgresql.org<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL has few feature for standby technology. I generally use streaming replication. This is very easy and confident. Streaming replication use a replication user for replication operations.Streaming replication allows a standby server to stay more up-to-date than is possible with file-based log shipping. The standby connects to the primary, which streams WAL records to the&#8230;<\/p>\n","protected":false},"author":37,"featured_media":2742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[14,88,108,109,127],"class_list":["post-2741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administration-postgresql","tag-database-administration","tag-postgresql-administration","tag-postgresql-high-availability","tag-postgresql-hot-standby","tag-postgresql-streaming-replication"],"jetpack_featured_media_url":"http:\/\/www.fatihacar.com\/blog\/resimler\/postgresql-12-streaming-replication.jpg","jetpack_shortlink":"https:\/\/wp.me\/p39NFI-Id","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/comments?post=2741"}],"version-history":[{"count":17,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2741\/revisions"}],"predecessor-version":[{"id":2779,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2741\/revisions\/2779"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media\/2742"}],"wp:attachment":[{"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media?parent=2741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/categories?post=2741"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/tags?post=2741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}