<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vina Maghdalena A. on Medium]]></title>
        <description><![CDATA[Stories by Vina Maghdalena A. on Medium]]></description>
        <link>https://medium.com/@vinaghda?source=rss-cf6c95dc2bb6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uE6alcStpa7SXyYmF5n5PA@2x.jpeg</url>
            <title>Stories by Vina Maghdalena A. on Medium</title>
            <link>https://medium.com/@vinaghda?source=rss-cf6c95dc2bb6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:49:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vinaghda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Backup and Restore PostgreSQL using PgBackRest with S3 Repository]]></title>
            <link>https://medium.com/@vinaghda/back-up-and-restore-postgresql-using-pgbackrest-with-s3-repository-e4a330cda3ac?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4a330cda3ac</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[backup]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Fri, 07 Nov 2025 08:08:31 GMT</pubDate>
            <atom:updated>2026-04-01T08:40:42.882Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ybmatYWCx3ld3wQ4oiwKVQ.png" /></figure><p>In this guide, I’ll walk you through setting up pgBackRest with AWS S3 as your backup repository, a configuration I’ve deployed across multiple staging and production environments. This setup gives you the reliability of enterprise backups with the scalability and cost effectiveness of cloud storage.</p><h3>Database Backups Are Non-Negotiable</h3><p>Database failures happen, hardware crashes, human errors, corrupted data, ransomware attacks and when they do, your backup strategy becomes the difference between a minor incident and a career defining disaster.</p><p>For PostgreSQL administrators, having a robust, tested backup solution isn’t just best practice, it’s essential infrastructure. But not all backup tools are created equal, especially when you’re managing databases that power critical applications.</p><h3>Enter pgBackRest: Enterprise Grade Backups for PostgreSQL</h3><p>While pg_dump and pg_basebackup are fine for small databases or development environments, they fall short when you need:</p><ul><li><strong>Parallel backup and restore</strong> for massive databases</li><li><strong>Incremental and differential backups</strong> to save storage and time</li><li><strong>Point in Time Recovery (PITR)</strong> to restore to any moment in time</li><li><strong>Multiple repositories</strong> including cloud storage like AWS S3</li><li><strong>Built in compression and encryption</strong></li><li><strong>Retention policies</strong> for automatic backup management</li></ul><p>That’s where <strong>pgBackRest</strong> shines. It’s a modern, feature-rich backup and restore solution specifically designed for PostgreSQL. Think of it as the industrial-strength backup tool that scales with your database, whether you’re managing a 10GB development database or a multi terabyte production cluster.</p><h3>Prerequisites</h3><p>Before we dive in, make sure you have:</p><ul><li><strong>Ubuntu 20.04 or later</strong> (the examples use Ubuntu, but pgBackRest works on most Linux distributions)</li><li><strong>PostgreSQL 12+</strong> installed and running</li><li><strong>AWS S3 bucket</strong> created with appropriate IAM permissions</li><li><strong>Root or sudo access</strong> on your PostgreSQL server</li><li><strong>AWS credentials</strong> (Access Key ID and Secret Access Key)</li></ul><h3>Step 1: Install pgBackRest</h3><p>First, let’s install pgBackRest. On Ubuntu, you can install it directly from the PostgreSQL APT repository:</p><pre># Add PostgreSQL repository if not already added<br>sudo sh -c &#39;echo &quot;deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main&quot; &gt; /etc/apt/sources.list.d/pgdg.list&#39;<br>wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -<br><br># Update package list<br>sudo apt update<br><br># Update package list<br>sudo apt update<br><br># Install pgBackRest<br>sudo apt install pgbackrest -y<br><br># Verify installation<br>pgbackrest version</pre><p>You should see output like pgBackRest 2.48 or similar.</p><h3>Step 2: Configure PostgreSQL for pgBackRest</h3><p>pgBackRest needs PostgreSQL to be configured for Write Ahead Log (WAL) archiving. Edit your PostgreSQL configuration file (typically /etc/postgresql/14/main/postgresql.conf):</p><pre>sudo nano /etc/postgresql/14/main/postgresql.conf</pre><p>Add or modify these settings:</p><pre># WAL archiving settings<br>archive_mode = on<br>archive_command = &#39;pgbackrest --stanza=main-db archive-push %p&#39;<br>max_wal_senders = 3<br>wal_level = replica</pre><p>Restart PostgreSQL to apply changes:</p><pre>sudo systemctl restart postgresql</pre><h3>Step 3: Configure S3 as the Backup Repository</h3><p>Now comes the crucial part, configuring pgBackRest to use S3 and encrypt your backup.</p><pre>sudo openssl rand -base64 48<br><br>#You will see something like this, copy and put on pgBackRest config file<br>kK6wvERtjflskajladfien@$4i9023ArNHZ3X+zuxw4csw6FAawPq7Y7wYdescd</pre><p>Create or edit the pgBackRest configuration file:</p><pre>sudo nano /etc/pgbackrest/pgbackrest.conf</pre><p>Here’s a sample configuration:</p><pre>[global]<br>repo1-type=s3<br>repo1-s3-bucket=my-pgbackrest-backups<br>repo1-s3-region=us-east-1<br>repo1-s3-key=YOUR_AWS_ACCESS_KEY_ID<br>repo1-s3-key-secret=YOUR_AWS_SECRET_ACCESS_KEY<br>repo1-s3-endpoint=s3.amazonaws.com<br>repo1-retention-full=2<br>repo1-retention-diff=4<br>repo1-cipher-type=aes-256-cbc<br>repo1-cipher-pass=your-encryption-passphrase<br>process-max=4<br>log-level-console=info<br>log-level-file=debug<br><br>[main-db]<br>pg1-path=/var/lib/postgresql/14/main<br>pg1-port=5432<br>pg1-socket-path=/var/run/postgresql</pre><p><strong>Configuration breakdown:</strong></p><ul><li>repo1-type=s3: Use S3 as the repository</li><li>repo1-s3-bucket: Your S3 bucket name</li><li>repo1-retention-full=2: Keep 2 full backups</li><li>repo1-retention-diff=4: Keep 4 differential backups</li><li>repo1-cipher-type: Enable encryption (highly recommended!)</li><li>process-max=4: Use 4 parallel processes for faster backups</li><li>pg1-path: Path to your PostgreSQL data directory</li></ul><p><strong>Security note:</strong> For production, use IAM roles instead of hardcoding AWS credentials, or store them in a secure configuration file with restricted permissions:</p><pre>sudo chmod 640 /etc/pgbackrest/pgbackrest.conf<br>sudo chown postgres:postgres /etc/pgbackrest/pgbackrest.conf</pre><h3>Step 4: Create the Stanza</h3><p>A “stanza” is pgBackRest’s term for a backup configuration for a specific PostgreSQL cluster. Create one:</p><pre>sudo -u postgres pgbackrest --stanza=main-db stanza-create</pre><p>Verify the stanza was created successfully:</p><pre>sudo -u postgres pgbackrest --stanza=main-db check</pre><p>You should see output indicating that WAL archiving is working and the repository is accessible.</p><h3>Step 5: Run Your First Full Backup</h3><p>Now for the moment of truth — your first backup:</p><pre>sudo -u postgres pgbackrest --stanza=main-db --type=full backup</pre><p>This will create a full backup of your PostgreSQL database and upload it to S3. Depending on your database size and network speed, this could take anywhere from minutes to hours.</p><p>Monitor the progress with:</p><pre>sudo -u postgres pgbackrest info</pre><p>You should see output like:</p><pre>stanza: main-db<br>    status: ok<br>    cipher: aes-256-cbc<br>db (current)<br>        wal archive min/max (14): 000000010000000000000001/000000010000000000000003<br>        full backup: 20250107-150000F<br>            timestamp start/stop: 2025-01-07 15:00:00 / 2025-01-07 15:15:00<br>            wal start/stop: 000000010000000000000002 / 000000010000000000000003<br>            database size: 245.3MB, database backup size: 245.3MB<br>            repo1: backup size: 32.1MB</pre><h3>Step 6: Run Incremental Backups</h3><p>Incremental backups only capture changes since the last backup (full or incremental), making them fast and storage-efficient:</p><pre>sudo -u postgres pgbackrest --stanza=main-db --type=incr backup</pre><p>For daily operations, you might run:</p><ul><li><strong>Full backup</strong>: Weekly (e.g., Sunday night)</li><li><strong>Differential backup</strong>: Daily (captures changes since last full)</li><li><strong>Incremental backup</strong>: Hourly or as needed</li></ul><h3>Restoring Your Database</h3><h3>Scenario 1: Restore to the Same Server (be careful)</h3><p>If you need to restore on the same server (after data corruption, for example):</p><pre># Stop PostgreSQL<br>sudo systemctl stop postgresql<br><br># Remove the corrupted data directory<br>sudo rm -rf /var/lib/postgresql/14/main/*<br><br># Restore from latest backup<br>sudo -u postgres pgbackrest --stanza=main-db restore<br><br># Start PostgreSQL<br>sudo systemctl start postgresql</pre><h3>Scenario 2: Restore to a New Server</h3><p>Setting up a new PostgreSQL server from backup:</p><pre># On the new server, install PostgreSQL and pgBackRest<br>sudo apt install postgresql-14 pgbackrest<br><br># Stop PostgreSQL<br>sudo systemctl stop postgresql<br><br># Copy pgBackRest configuration from old server<br>sudo scp old-server:/etc/pgbackrest/pgbackrest.conf /etc/pgbackrest/<br><br># Empty the data directory<br>sudo rm -rf /var/lib/postgresql/14/main/*<br><br># Restore from S3<br>sudo -u postgres pgbackrest --stanza=main-db restore<br><br># Start PostgreSQL<br>sudo systemctl start postgresql</pre><h3>Point-in-Time Recovery (PITR)</h3><p>One of pgBackRest’s most powerful features is the ability to restore to any point in time (as long as you have the WAL archives):</p><pre># Restore to a specific timestamp<br>sudo -u postgres pgbackrest --stanza=main-db --type=time \<br>  --target=&quot;2025-01-07 14:30:00&quot; restore<br><br># Or restore to a specific transaction ID<br>sudo -u postgres pgbackrest --stanza=main-db --type=xid \<br>  --target=&quot;1234567&quot; restore</pre><p>This is invaluable when you need to recover from a bad migration, accidental deletion, or other time-specific issues.</p><h3>Conclusion</h3><p>pgBackRest with S3 provides a robust, scalable, and cost effective backup solution for PostgreSQL databases of any size. By following this guide, you’ve learned how to:</p><ul><li>Configure pgBackRest with S3 as a cloud backup repository</li><li>Run full, differential, and incremental backups</li><li>Restore databases to the same or different servers</li><li>Perform point in time recovery for granular restoration</li><li>Troubleshoot common issues</li><li>Implement production ready best practices</li></ul><p>Remember: <strong>Backups are only as good as your ability to restore from them.</strong> Test your restore process regularly, automate your backup schedule, and monitor your backup infrastructure as carefully as you monitor your databases.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4a330cda3ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Configure BARMAN Postgresql]]></title>
            <link>https://medium.com/@vinaghda/configure-barman-postgresql-54658b43cf6d?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/54658b43cf6d</guid>
            <category><![CDATA[barman-backup]]></category>
            <category><![CDATA[postgresql-backup]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Tue, 01 Oct 2024 07:34:05 GMT</pubDate>
            <atom:updated>2025-08-08T08:37:36.612Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o-mRWTw_fuRtRjlYO5UjSg.png" /></figure><p>Backup Server (barman installed): 192.168.100.31<br>Postgres Server: 192.168.100.10</p><p>Install barman</p><pre>apt install barman</pre><p>Create passwordless access between barman and postgres server, then try to ssh across server, makesure connect without prompt password</p><p>On barman server using user barman</p><pre>barman@vinaghda:~$ ssh-keygen<br>barman@vinaghda:~$ ssh-copy-id postgres@192.168.100.10<br>barman@vinaghda:~$ ssh postgres@192.168.100.10</pre><p>On server postgres using user postgres</p><pre>-bash-4.2$ ssh-keygen<br>-bash-4.2$ ssh-copy-id barman@192.168.100.31<br>-bash-4.2$ ssh barman@192.168.100.31</pre><p>On barman server using user barman</p><p>Create .pgpass file to passthrough the connection psql become passwordless</p><pre># sudo su - barman<br>$ mkdir .pgpass<br>$ vi .pgpass<br>*:5432:*:barman:password123<br>$ sudo chmod 0600 .pgpass</pre><p>Configure barman /etc/barman.conf on server barman</p><pre>$ vi /etc/barman.conf<br>[barman]<br>barman_user = barman<br>configuration_files_directory = /etc/barman.d<br>barman_home = /var/lib/barman<br>log_file = /var/log/barman/barman.log<br>log_level = INFO<br>compression = gzip<br>immediate_checkpoint = true<br>basebackup_retry_times = 3<br>basebackup_retry_sleep = 30<br>last_backup_maximum_age = 1 DAYS<br>minimum_redundancy = 2<br>[main-db-server]<br>description = &quot;Main DB Server&quot;<br>ssh_command = ssh postgres@&lt;ip postgres&gt;<br>conninfo = host=&lt;ip postgres&gt; user=barman dbname=postgres passfile=&#39;/var/lib/barman/.pgpass&#39; port=5432<br>retention_policy_mode = auto<br>retention_policy = RECOVERY WINDOW OF 7 days<br>wal_retention_policy = main<br>archiver = on</pre><p>Create new directory under /var/lib/barman</p><pre># mkdir /var/lib/barman/main-db-server/</pre><p>Configure postgresql, add following lines to postgresql.conf</p><pre>wal_level=archive<br>archive_mode=on<br>archive_command=&#39;rsync -avp %p barman@192.168.100.31:/var/lib/barman/main-db-server/incoming/%f&#39;</pre><p>Restart postgres</p><pre>$ /usr/pgsql-11/bin/pg_ctl -D /data/postgres/data/ start</pre><p>Create user barman on postgres server 192.168.100.10</p><pre>postgres=# create role barman with password &#39;password123&#39; ; <br>alter role barman with login</pre><p>On barman server using user barman, check barman configuration and connection to postgres</p><pre>barman@vinaghda:~$ barman check main-db-server</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54658b43cf6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setup percona agent on ARM based architecture]]></title>
            <link>https://medium.com/@vinaghda/setup-percona-agent-on-arm-based-architecture-a6bef4718b19?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6bef4718b19</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[percona-monitoring]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Tue, 01 Oct 2024 04:09:51 GMT</pubDate>
            <atom:updated>2024-11-18T04:57:18.476Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MV9uMjsy1qcrUVDoP7eu2Q.png" /></figure><p>Monitoring your database’s performance is crucial to ensure optimal performance and availability. While tools like Percona Monitoring and Management (PMM) are often used in x86 environments, ARM-based for percona has not been released yet. So in this guide, I will walk you through installing Percona Monitoring on an ARM architecture, specifically on Ubuntu 22.04.4 LTS, with PostgreSQL v15.</p><p><strong>System Information:</strong></p><ul><li><strong>OS</strong>: Ubuntu 22.04.4 LTS</li><li><strong>Database</strong>: PostgreSQL v15</li><li><strong>Architecture</strong>: ARM</li></ul><p><strong>Preparing the Environment</strong></p><p>Install the required dependencies for Percona on ARM architecture. You’ll need to have wget, gnupg, and other utilities installed:</p><pre>apt update &amp;&amp; sudo apt upgrade -y<br>apt install vim wget git<br>apt install gcc-aarch64-linux-gnu libc6-dev-arm64-cross<br>apt install libc6-dev</pre><h3>Cloning the Percona Repository</h3><p>Percona provides detailed instructions on how to compile their tools for ARM-based systems, as referenced in their <a href="https://www.percona.com/blog/compiling-percona-xtrabackup-for-arm/">official blog post</a>. We’ll follow a similar approach to set up Percona Monitoring.</p><p>Start by cloning the Percona repository, which contains the necessary components:</p><pre>mkdir -p ~/go/src/github.com/percona/<br>cd $_<br>git clone https://github.com/percona/pmm-admin.git<br>git clone https://github.com/percona/pmm-agent.git<br>git clone https://github.com/percona/node_exporter.git<br>git clone https://github.com/percona/postgres_exporter.git</pre><h3>Compiling Percona</h3><p>Before compiling we need to install golang in our instance</p><pre>wget https://go.dev/dl/go1.22.7.linux-arm64.tar.gz<br>tar -C /usr/local -xzf go1.22.7.linux-arm64.tar.gz<br>export PATH=$PATH:/usr/local/go/bin<br>go version</pre><p>Compile pmm client and exporter</p><pre>cd ~/go/src/github.com/percona/pmm-admin<br>make release<br><br>cd ~/go/src/github.com/percona/pmm-agent<br>make release<br><br>cd ~/go/src/github.com/percona/node_exporter/<br>make build<br><br>cd ~/go/src/github.com/percona/postgres_exporter/<br>make build</pre><p>Download other exporter and tools for percona: vmagent, pt-summary (show instance detail) and pt-pg-summary (show postgres detail)</p><pre>cd /tmp/<br>curl -LO https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.59.0/vmutils-arm-v1.59.0.tar.gz<br>tar xzf vmutils-arm-v1.59.0.tar.gz<br>wget http://percona.com/get/pt-summary<br>wget http://percona.com/get/pt-pg-summary</pre><p>move to pmm binary to percona directory</p><pre>cd ~/go/src/github.com/percona/<br>cp pmm-admin/bin/pmm-admin /usr/local/bin<br>cp pmm-agent/bin/pmm-agent /usr/local/bin<br>cp pmm-admin/bin/pmm-admin /usr/local/percona/pmm2/bin<br>cp pmm-agent/bin/pmm-agent /usr/local/percona/pmm2/bin</pre><p>move exporter and tools to percona directory</p><pre>mkdir -p /usr/local/percona/pmm2/exporters/<br>mkdir -p /usr/local/percona/pmm2/tools/<br>cp node_exporter/node_exporter /usr/local/percona/pmm2/exporters/<br>cp postgres_exporter/postgres_exporter /usr/local/percona/pmm2/exporters/<br>cp /tmp/vmagent-prod /usr/local/percona/pmm2/exporters/vmagent<br>cp /tmp/pt-summary /usr/local/percona/pmm2/tools/<br>cp /tmp/pt-pg-summary /usr/local/percona/pmm2/tools/<br>ls</pre><p>to avoid error, we need to create these files:</p><pre>cd /usr/local/percona/pmm2/<br>mkdir -p collectors/textfile-collector/high-resolution<br>mkdir -p collectors/textfile-collector/medium-resolution<br>mkdir -p collectors/textfile-collector/low-resolution</pre><h3>Starting the PMM Client</h3><pre>mkdir -p /usr/local/percona/pmm2/config/<br><br>PMM_CLIENT_ADDR=&#39;172.30.0.17&#39;<br>PMM_SERVER_ADDR=&#39;percona_dashboard.test.com:443&#39;<br>NODE_NAME=&#39;aws_arm_1&#39;<br>USER_NAME_PMM=&#39;percona&#39;<br>PASSWORD_PMM=&#39;password&#39;<br>USER_NAME_DB=&#39;postgres&#39;<br>PASSWORD_DB=&#39;password&#39;<br><br><br>pmm-agent setup ${PMM_CLIENT_ADDR} generic ${NODE_NAME} \<br>  --config-file=/usr/local/percona/pmm2/config/pmm-agent.yaml \<br>  --server-address=${PMM_SERVER_ADDR} --server-insecure-tls \<br>  --server-username=${USER_NAME_PMM} --server-password=${PASSWORD_PMM}<br><br>pmm-agent --config-file=/usr/local/percona/pmm2/config/pmm-agent.yaml &gt; /tmp/pmm_agent.log &amp;<br><br>pmm-admin add postgresql --username=${USER_NAME_DB} --password=${PASSWORD_DB} \<br>  --server-address=${PMM_SERVER_ADDR} --server-insecure-tls \<br>  --server-username=${USER_NAME_PMM} --server-password=${PASSWORD_PMM}<br>  --comments-parsing=&quot;off&quot; --host=${PMM_CLIENT_ADDR}</pre><p>References:<br><a href="https://www.percona.com/blog/compiling-a-percona-monitoring-and-management-v2-client-in-arm-architecture/">https://www.percona.com/blog/compiling-a-percona-monitoring-and-management-v2-client-in-arm-architecture/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6bef4718b19" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monitoring PostgreSQL: Automating Alerts for Unauthorized GRANT Commands via Slack]]></title>
            <link>https://medium.com/@vinaghda/monitoring-postgresql-automating-alerts-for-unauthorized-grant-commands-via-slack-15e49c0317f4?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/15e49c0317f4</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[database-administration]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[slack]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Mon, 02 Sep 2024 08:06:31 GMT</pubDate>
            <atom:updated>2025-08-08T07:40:51.738Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u1nyJ8NdRGWullFuN5238Q.png" /></figure><p>In my case in need to monitor permission on my database, if someone invoke grant command in my db, slack notification will be send alert.</p><p>This script will read your postgresql log, put this script on your crontab.</p><pre>#!/bin/bash<br><br># PostgreSQL log directory and file name pattern<br>PG_LOG_DIR=&quot;/data/pg_log&quot;<br>LOG_FILE_PATTERN=&quot;postgresql-$(date +%d).log&quot;<br><br># Slack webhook URL<br>SLACK_WEBHOOK_URL=&quot;https://hooks.slack.com/your_webhook&quot;<br><br># Temporary file to store last checked position<br>POS_FILE=&quot;$PG_LOG_DIR/pg_log_check_position_$(date +%d)&quot;<br><br># Get the full path to the current log file<br>PG_LOG_FILE=&quot;$PG_LOG_DIR/$LOG_FILE_PATTERN&quot;<br><br># Get the last checked position<br>if [ -f &quot;$POS_FILE&quot; ]; then<br>    LAST_POS=$(cat &quot;$POS_FILE&quot;)<br>else<br>    LAST_POS=0<br>fi<br><br># Ensure LAST_POS is an integer<br>if ! [[ &quot;$LAST_POS&quot; =~ ^[0-9]+$ ]]; then<br>    LAST_POS=0<br>fi<br><br># Get the current file size<br>CURRENT_SIZE=$(stat -c%s &quot;$PG_LOG_FILE&quot;)<br><br># Ensure CURRENT_SIZE is an integer<br>if ! [[ &quot;$CURRENT_SIZE&quot; =~ ^[0-9]+$ ]]; then<br>    echo &quot;Error: Unable to get the size of the log file.&quot;<br>    exit 1<br>fi<br><br># If the log file has been rotated, reset the last position<br>if [ &quot;$LAST_POS&quot; -gt &quot;$CURRENT_SIZE&quot; ]; then<br>    LAST_POS=0<br>fi<br><br># Read the log file from the last position<br>LOG_ENTRIES=$(tail -c +$((LAST_POS + 1)) &quot;$PG_LOG_FILE&quot;)<br><br># Update the position file with the current file size<br>echo &quot;$CURRENT_SIZE&quot; &gt; &quot;$POS_FILE&quot;<br><br># Function to extract details from log entry<br>extract_details() {<br>    local log_entry=&quot;$1&quot;<br>    local timestamp=$(echo &quot;$log_entry&quot; | awk &#39;{print $1}&#39;)<br>    local pid=$(echo &quot;$log_entry&quot; | awk &#39;{print $4}&#39; | tr -d &#39;[]&#39;)<br>    local user_db=$(echo &quot;$log_entry&quot; | awk &#39;{print $5}&#39;)<br>    local host=$(echo &quot;$log_entry&quot; | awk &#39;{print $6}&#39;)<br>    local statement=$(echo &quot;$log_entry&quot; | sed &#39;s/.*LOG:  statement: //&#39;)<br>    echo &quot;$timestamp&quot; &quot;$pid&quot; &quot;$user_db&quot; &quot;$host&quot; &quot;$statement&quot;<br>}<br><br># Check for GRANT commands in the log entries<br>GRANT_ENTRIES=$(echo &quot;$LOG_ENTRIES&quot; | grep -i &quot;grant&quot;)<br><br>if [ -n &quot;$GRANT_ENTRIES&quot; ]; then<br>    # Process each grant entry<br>    while IFS= read -r entry; do<br>        read timestamp pid user_db host statement &lt;&lt;&lt; $(extract_details &quot;$entry&quot;)<br><br>        # Format the message for Slack<br>        FORMATTED_ENTRY=&quot;• Timestamp: $timestamp\n• Process ID: $pid\n• User@DB: $user_db\n• Host: $host\n\n*Detected GRANT command:* \`\`\`$statement\`\`\`\n\nThis activity is being monitored as per your request.&quot;<br><br>        # Create a styled Slack message payload using Block Kit<br>        PAYLOAD=$(cat &lt;&lt;EOF<br>{<br>    &quot;attachments&quot;: [<br>        {<br>            &quot;color&quot;: &quot;#FFA500&quot;,<br>            &quot;blocks&quot;: [<br>                {<br>                    &quot;type&quot;: &quot;section&quot;,<br>                    &quot;text&quot;: {<br>                        &quot;type&quot;: &quot;mrkdwn&quot;,<br>                        &quot;text&quot;: &quot;*:warning: GRANT command detected:*&quot;<br>                    }<br>                },<br>                {<br>                    &quot;type&quot;: &quot;section&quot;,<br>                    &quot;text&quot;: {<br>                        &quot;type&quot;: &quot;mrkdwn&quot;,<br>                        &quot;text&quot;: &quot;$FORMATTED_ENTRY&quot;<br>                    }<br>                }<br>            ]<br>        }<br>    ]<br>}<br>EOF<br>)<br><br>        # Send notification to Slack<br>        curl -X POST -H &#39;Content-type: application/json&#39; --data &quot;$PAYLOAD&quot; $SLACK_WEBHOOK_URL<br><br>    done &lt;&lt;&lt; &quot;$GRANT_ENTRIES&quot;<br>fi</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15e49c0317f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Efficient Backup Management: Setting Up Multiple Stanzas in pgBackRest]]></title>
            <link>https://medium.com/@vinaghda/efficient-backup-management-setting-up-multiple-stanzas-in-pgbackrest-6cc7a76b1707?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cc7a76b1707</guid>
            <category><![CDATA[pgbackrest]]></category>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Tue, 06 Aug 2024 04:25:31 GMT</pubDate>
            <atom:updated>2024-08-06T04:25:31.015Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*LTYD-jrbbFLKqCPWnUGbJg.png" /></figure><p>Setting up multiple stanzas in one repository using pgBackRest allows you to manage backups for multiple PostgreSQL clusters in a single repository. Here’s a step-by-step guide on how to set up and configure multiple stanzas:</p><h3>Prerequisites</h3><ol><li><strong>Install pgBackRest</strong>: Ensure pgBackRest is installed on your system with same version</li><li><strong>Configure PostgreSQL</strong>: Ensure your PostgreSQL clusters are up and running.</li><li><strong>Repository Directory</strong>: Choose a directory to serve as your backup repository.</li></ol><h3>Installation Steps</h3><ol><li>In this case, our repository using pgbackrest version 2.46<br>First thing first download version 2.46 of <strong>pgBackRest</strong> to /build path</li></ol><pre>sudo mkdir -p /build<br>sudo wget -q -O - \<br>       https://github.com/pgbackrest/pgbackrest/archive/release/2.46.tar.gz | \<br>       tar zx -C /build</pre><p>2. Install build dependencies</p><pre>sudo apt-get install make gcc libpq-dev libssl-dev libxml2-dev pkg-config \<br>        liblz4-dev libzstd-dev libbz2-dev libz-dev libyaml-dev libssh2-1-dev</pre><p><strong>3. </strong>Configure and compile <strong>pgBackRest</strong></p><pre>sudo cd /build/pgbackrest-release-2.46/src &amp;&amp; ./configure &amp;&amp; make</pre><p>4. Install dependencies</p><pre>sudo apt-get install postgresql-client libxml2 libssh2-1</pre><p>5. Copy <strong>pgBackRest</strong> binary</p><pre>sudo cp pgbackrest /usr/bin <br>sudo chmod 755 /usr/bin/pgbackrest</pre><p>6. Verify the instalation</p><pre>pgbackrest version pgBackRest 2.46</pre><h3>Configuration Steps</h3><ol><li><strong>Create a Configuration File</strong></li></ol><p>Create or edit the pgbackrest.conf file, typically located in /etc/pgbackrest/pgbackrest.conf. You will define separate stanzas for each PostgreSQL cluster.</p><p><strong>2. Define Multiple Stanzas<br></strong>Each stanza corresponds to a PostgreSQL cluster. Below is an example configuration for three clusters:</p><pre>vi /etc/pgbackrest/pgbackrest.conf<br><br>[db-skate]<br>pg1-path=/data/postgres/datadir<br>pg1-port=5432<br>pg1-host=192.168.56.1<br>pg1-socket-path=/var/run/postgresql<br><br>[db-car]<br>pg1-path=/data/postgres/datadir<br>pg1-port=5432<br>pg1-host=192.168.56.2<br>pg1-socket-path=/var/run/postgresql<br><br>[db-bike]<br>pg1-path=/data/postgres/datadir<br>pg1-port=5432<br>pg1-host=192.168.56.3<br>pg1-socket-path=/var/run/postgresql<br><br>[global]<br>repo1-path=/archive/pgbackrest<br>start-fast=y<br>repo1-retention-full=2</pre><ul><li><strong>[global]</strong>: This section defines global settings applicable to all stanzas.</li><li><strong>[db-skate], [db-car] </strong>and <strong>[db-bike]</strong>: These are separate stanzas for different PostgreSQL clusters. Adjust paths and ports as needed.</li></ul><p><strong>3. Initialize the Stanzas<br></strong>Initialize each stanza to verify that the configuration is correct and pgBackRest can communicate with the PostgreSQL clusters:</p><pre>pgbackrest --stanza=db-skate --log-level-console=info stanza-create<br>pgbackrest --stanza=db-car --log-level-console=info stanza-create<br>pgbackrest --stanza=db-bike --log-level-console=info stanza-create</pre><p><strong>4. Perform Backups<br></strong>Once the stanzas are initialized, you can perform backups for each cluster. This will start a backup for the specified cluster and store it in the configured repository.</p><pre>pgbackrest --stanza=db-skate --log-level-console=info backup<br>pgbackrest --stanza=db-car --log-level-console=info backup<br>pgbackrest --stanza=db-bike --log-level-console=info backup</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cc7a76b1707" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rename Your Cluster with pg_renamecluster]]></title>
            <link>https://medium.com/@vinaghda/effortlessly-rename-your-cluster-with-pg-renamecluster-242f79d223d3?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/242f79d223d3</guid>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Thu, 25 Jul 2024 09:10:44 GMT</pubDate>
            <atom:updated>2024-08-07T02:21:14.481Z</atom:updated>
            <content:encoded><![CDATA[<p>The pg_renamecluster command simplifies the process of renaming a PostgreSQL cluster. This command handles stopping the cluster, renaming the data directory and configuration files, and starting the cluster with the new name.</p><p>Here’s how to use pg_renamecluster:</p><ol><li><strong>Run </strong><strong>pg_renamecluster</strong>: You can use the following command format:</li></ol><p>For example, if you have a cluster named old_cluster for PostgreSQL version 13 and you want to rename it to new_cluster, you would run:</p><pre>pg_renamecluster &lt;version&gt; &lt;old_cluster_name&gt; &lt;new_cluster_name&gt;<br><br>pg_renamecluster 13 old_cluster new_cluster</pre><p>This command performs the following actions automatically:</p><ul><li>Stops the cluster.</li><li>Renames the data directory.</li><li>Renames the configuration files.</li><li>Starts the cluster with the new name.</li></ul><p>pg_renamecluster makes the renaming process straightforward and less error-prone without restarting db</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=242f79d223d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting Up Alert Notifications for pgpool Failover in Your Postgresql]]></title>
            <link>https://medium.com/@vinaghda/ensuring-high-availability-setting-up-alert-notifications-for-pgpool-failover-in-your-postgresql-0f48120ff215?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/0f48120ff215</guid>
            <category><![CDATA[pgpool]]></category>
            <category><![CDATA[failover]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[alerts]]></category>
            <category><![CDATA[slack]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Fri, 19 Jul 2024 07:03:17 GMT</pubDate>
            <atom:updated>2024-08-07T02:20:14.319Z</atom:updated>
            <content:encoded><![CDATA[<p>Maintaining high availability for your database is crucial for minimizing downtime and ensuring smooth operations. pgpool is a powerful tool that helps manage PostgreSQL servers, providing connection pooling, load balancing, and replication. However, it’s important to monitor and receive alerts for failovers to ensure prompt action.</p><h4>Setting Up Slack Webhook</h4><p>To receive notifications in Slack, you need to set up an incoming webhook. Follow these steps:</p><ol><li>Go to your Slack workspace and navigate to Apps &gt; Manage Apps.</li><li>Search for Incoming WebHooks and add it to your workspace.</li><li>Click Add to Slack, select a channel, and click Add Incoming WebHooks integration.</li><li>Copy the provided webhook URL and replace YOUR/SLACK/WEBHOOK/URL in the failover script.</li></ol><h4>Modify failover script</h4><p>The failover script (failover_script.sh) will be triggered by pgpool during a failover. This script will include commands to send notifications.</p><pre>#add parameter<br>WEBHOOK_URL=&quot;https://hooks.slack.com/services/your_web_hook&quot;<br>CHANNEL=&quot;#bbbb&quot;<br>CLUSTER_NAME=&quot;bbbb&quot;<br>TIMESTAMP=$(date +%s)<br>MONITORING_TOOL_URL=&quot;your dashboard link&quot;<br>MENTION_USER=&quot;user_ID_slack&quot;<br>MUTE_ALERT=${MUTE_ALERT:-false} # Default to false if not set<br><br>#####<br>if [ $? -ne 0 ]; then<br>    echo ERROR: failover.sh: end: failover failed<br>    exit 1<br>fi<br>##### <br><br># Put this script before last line<br># Check if alerts are muted<br>if [ &quot;$MUTE_ALERT&quot; = true ]; then<br>  echo &quot;Alerts are muted. Exiting without sending alert.&quot;<br>  exit 0<br>fi<br><br># Create the JSON payload<br>read -r -d &#39;&#39; PAYLOAD &lt;&lt;EOF<br>{<br>  &quot;channel&quot;: &quot;${CHANNEL}&quot;,<br>  &quot;blocks&quot;: [<br>    {<br>      &quot;type&quot;: &quot;section&quot;,<br>      &quot;text&quot;: {<br>        &quot;type&quot;: &quot;mrkdwn&quot;,<br>        &quot;text&quot;: &quot;:ninuninu: *PostgreSQL Failover Alert* :ninuninu:&quot;<br>      }<br>    },<br>    {<br>      &quot;type&quot;: &quot;section&quot;,<br>      &quot;fields&quot;: [<br>        {<br>          &quot;type&quot;: &quot;mrkdwn&quot;,<br>          &quot;text&quot;: &quot;*Event:* PostgreSQL Failover&quot;<br>        },<br>        {<br>          &quot;type&quot;: &quot;mrkdwn&quot;,<br>          &quot;text&quot;: &quot;*Severity:* High&quot;<br>        },<br>        {<br>          &quot;type&quot;: &quot;mrkdwn&quot;,<br>          &quot;text&quot;: &quot;*Timestamp:* &lt;!date^${TIMESTAMP}^ {date_num} {time_secs}|Timestamp&gt;&quot;<br>        },<br>        {<br>          &quot;type&quot;: &quot;mrkdwn&quot;,<br>          &quot;text&quot;: &quot;*Cluster:* \`${CLUSTER_NAME}\`&quot;<br>        },<br>        {<br>          &quot;type&quot;: &quot;mrkdwn&quot;,<br>          &quot;text&quot;: &quot;*Old Master:* \`${OLD_PRIMARY_NODE_HOST}\`&quot;<br>        },<br>        {<br>          &quot;type&quot;: &quot;mrkdwn&quot;,<br>          &quot;text&quot;: &quot;*New Master:* \`${NEW_MAIN_NODE_HOST}\`&quot;<br>        }<br>      ]<br>    },<br>    {<br>      &quot;type&quot;: &quot;section&quot;,<br>      &quot;text&quot;: {<br>        &quot;type&quot;: &quot;mrkdwn&quot;,<br>        &quot;text&quot;: &quot;Please check the cluster status and ensure the failover completed successfully. \n&lt;${MONITORING_TOOL_URL}|View Monitoring Dashboard&gt;\n\n${MENTION_USER}&quot;<br>      }<br>    }<br>  ]<br>}<br>EOF<br><br># Send the payload to Slack<br>curl -X POST -H &#39;Content-type: application/json&#39; --data &quot;${PAYLOAD}&quot; ${WEBHOOK_URL}</pre><h4>Testing the Configuration</h4><p>To ensure everything is set up correctly, you can manually trigger a failover. This can be done by stopping the primary server or using pgpool’s administrative commands. Check your Slack channel for the failover notification.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0f48120ff215" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monitor Progress of your Postgresql Dump]]></title>
            <link>https://medium.com/@vinaghda/monitor-progress-of-your-postgresql-dump-9de936781baa?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9de936781baa</guid>
            <category><![CDATA[dump-database]]></category>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Mon, 01 Jul 2024 04:14:27 GMT</pubDate>
            <atom:updated>2024-07-24T08:18:06.960Z</atom:updated>
            <content:encoded><![CDATA[<p>To dump a PostgreSQL database with a progress bar in the terminal on Ubuntu, you can use the pg_dump command along with tools like pv to display the progress. Here&#39;s a step-by-step guide:</p><ol><li>Install <strong>pv:</strong> <br>pv is a tool for monitoring the progress of data through a pipeline. You can install it using apt<br>sudo apt-get install pv</li><li>Create the PostgreSQL dump with a progress bar<br>pg_dump testdb|pv -c -s $(psql -tc &quot;SELECT pg_database_size(&#39;testdb&#39;)&quot;) -N dump|gzip &gt; testdb.sql.gz</li><li>Monitor progress using pg_dumpall<br>pg_dumpall|pv -s $(psql -tc &quot;SELECT SUM(1.1*pg_database_size(datname))) FROM pg_database&quot;|gzip &gt; dumpall.sql.gz</li></ol><p>source: <a href="https://gist.github.com/kmatt/2783910">https://gist.github.com/kmatt/2783910</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9de936781baa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use pg-auto-failover]]></title>
            <link>https://medium.com/@vinaghda/how-to-use-pg-auto-failover-d36b60a922d9?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d36b60a922d9</guid>
            <category><![CDATA[citusdata]]></category>
            <category><![CDATA[high-availability]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Mon, 29 Jan 2024 06:51:52 GMT</pubDate>
            <atom:updated>2024-08-06T04:32:19.746Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>pg_autoctl create monitor</strong></h3><p>Initialize a pg_auto_failover monitor node</p><pre>$ pg_autoctl create monitor --pgctl path_to_pgctl --pgdata data_dir --pgport port_postgres</pre><p><strong>pg_autoctl create coordinator</strong></p><p>Initializes a pg_auto_failover Coordinator node for a Citus formation. The coordinator is special in a Citus formation: that’s where the client application connects to either to manage the formation and the sharding of the tables, or for its normal SQL traffic.</p><pre>pg_autoctl create coordinator --pgctl path_to_pgctl --pgdata data_dir --pgport port_postgres --monitor endpoint monitor</pre><p><strong>pg_autoctl create postgres</strong></p><p>This command will create postgres node and the monitor is handling auto-failover for this Postgres node (as soon as a secondary has been registered too, and is known to be healthy). The create the Postgres nodes use the following command on each node you want to create:</p><pre>pg_autoctl create postgres --pgctl path_to_pgctl --pgdata data_dir --pgport port_postgres</pre><p><strong>pg_autoctl create worker</strong></p><p>The worker is special in a Citus formation: that’s where the client application connects to either to manage the formation and the sharding of the tables, or for its normal SQL traffic. To create the Postgres nodes with citus extension installed use this command on each node:</p><pre>pg_autoctl create worker--pgctl path_to_pgctl --pgdata data_dir --pgport port_postgres --monitor endpoint monitor</pre><p><strong>pg_autoctl run</strong></p><p>While those <em>create</em> commands initialize your nodes, now you have to actually run the Postgres service that are expected to be running on background. For that you can manually run the following command on every node:</p><pre>pg_autoctl run --pgdata data_dir --pgport port_postgres &amp;</pre><p><strong>pg_autoctl show state</strong></p><p>Once your Postgres nodes have been created, and once each <strong>pg_autoctl</strong> service is running, it is possible to inspect the current state of the formation with the following command:</p><pre>pg_autoctl show state --monitor monitor_endpoint</pre><p><strong>pg_autoctl</strong> <strong>watch</strong></p><p>The <strong>pg_autoctl show state</strong> commands outputs the current state of the system only once. Sometimes it would be nice to have an auto-updated display such as provided by common tools such as <a href="https://www.mankier.com/1/watch">watch(1)</a> or <a href="https://www.mankier.com/1/top">top(1)</a> and the like. For that, the following commands are available (see also <em>pg_autoctl watch</em>):</p><pre>pg_autoctl watch<br>pg_autoctl show state --watch</pre><p><strong>pg_autoctl show events</strong></p><p>To analyze what’s been happening to get to the current state, it is possible to review the past events generated by the pg_auto_failover monitor with the following command:</p><pre>pg_autoctl show events --monitor monitor_endpoint</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d36b60a922d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Distributed Database using Citus with High Availability pg_autofailover part 1]]></title>
            <link>https://medium.com/@vinaghda/distributed-database-using-citus-with-high-availability-pg-autofailover-d9b010acaf14?source=rss-cf6c95dc2bb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9b010acaf14</guid>
            <dc:creator><![CDATA[Vina Maghdalena A.]]></dc:creator>
            <pubDate>Mon, 29 Jan 2024 02:53:36 GMT</pubDate>
            <atom:updated>2024-07-24T07:57:50.662Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Citus</h3><p>Citus is an open source distributed database engine for PostgreSQL that is implemented as an extension. Citus gives users the ability to distribute data, queries, and transactions in PostgreSQL across a cluster of PostgreSQL nodes to handle the needs of data-intensive applications.</p><h3>Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dl11T8BmqedPQ9MO2UklpQ.png" /></figure><ul><li>one monitor node,</li><li>one primary Citus coordinator node and one secondary Citus coordinator node,</li><li>and two Citus worker nodes, each worker with both a primary Postgres node and a secondary Postgres node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F8lKuI-ooOPOrG6C3OIPyw.png" /></figure><p>HA in Citus is handled primarily at the node level using existing PostgreSQL replication. In an HA setup, each node in the cluster has one or more hot standby nodes and replicates its write-ahead log (WAL). When a node fails, the cluster orchestrator promotes a standby and updates the Citus metadata. The whole failover process takes 20–30 seconds, during which distributed transactions involving the node roll back. To handled autofailover we will use pg_autofailover.</p><h3>Setting-up Citus Cluster</h3><h3>Installation of pg_auto_failover</h3><p>Pg_auto_failover is available in the package managers for Linux distributions. The following steps for installation Debian / Ubuntu.</p><h4>Add repository for pg-auto-failover</h4><p>Binary packages for debian and derivatives (ubuntu) are available from <a href="https://wiki.postgresql.org/wiki/Apt">apt.postgresql.org</a> repository, install by following the linked documentation and then:</p><pre>#Add postgres repository for pg-auto-failover<br>[all nodes]# apt-get install curl ca-certificates gnupg<br>[all nodes]# curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add<br>[all nodes]# sh -c &#39;echo &quot;deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main&quot; &gt; /etc/apt/sources.list.d/pgdg.list&#39;<br>[all nodes]# apt-get update</pre><p>The Postgres extension named “pgautofailover” is only necessary on the monitor node. To install that extension, you can install the postgresql-14-auto-failover package</p><pre>[monitor nodes]# apt-get install pg-auto-failover-cli <br>[monitor nodes]# apt-get install postgresql-14-auto-failover</pre><p>Confirm the installation</p><pre>[monitor nodes]# pg_autoctl --version <br>postgres@dba-coordinator:~$ pg_autoctl --version <br>pg_autoctl version 2.0 <br>pg_autoctl extension version 2.0 <br>compiled with PostgreSQL 15.0 (Ubuntu 15.0-2.pgdg22.04+1) on x86_64-pc-linux-gnu, <br>compiled by gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, 64-bit <br>compatible with Postgres 10, 11, 12, 13, and 14</pre><h3>Installation of Citus</h3><h4>Add repository for citus</h4><pre># Add Citus repository for package manager [all nodes]<br>curl https://install.citusdata.com/community/deb.sh | sudo bash</pre><h4>Install citus and set libraries</h4><p>This installs centralized configuration in /etc/postgresql/14/main, and creates a database in /var/lib/postgresql/14/main.</p><pre># install the node and initialize db [all nodes]<br>apt-get -y install postgresql-14-citus-11.2  <br># preload citus extension [all nodes]<br>pg_conftool 14 main set shared_preload_libraries citus</pre><h3>Configuration for Monitor node</h3><p>We need to first configure the monitor as it will periodically monitor the DB nodes and watches their health. For complete configuration, we will be using pg_autoctl commands.</p><p>Create data and tmp directories(if not exist) and change the ownership to postgres user.</p><pre>[all nodes]$ mkdir -p /tmp/pg_autoctl <br>[all nodes]$ chown -R postgres.postgres  /tmp/pg_autoctl</pre><h4>pg_autoctl create monitor</h4><p>Execute the below command to configure the monitor</p><pre>[monitor node]$ pg_autoctl create monitor --auth trust --ssl-self-signed  --pgport 5431 --pgdata /data/postgres/data/ --pgctl /usr/lib/postgresql/14/bin/pg_ctl  <br>pgdata —&gt; path of the data directory <br>pgctl  —&gt; path to the pg_ctl binary</pre><p>The above successful execution of the command will print the following message</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nMJDTaSXpoZdOmntsiCe9g.png" /></figure><p>It also installs the pgautofailover extension and grants access to the new user autoctl_node in the local PostgreSQL database.</p><p>Execute the following command to start the monitor node by postgres user.</p><pre>[monitor node]$ pg_autoctl run --pgdata /data/postgres/monitor/ --pgport 5431 &amp;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zHiDJBUMhMCKq_dg-ULGng.png" /></figure><p>pg_autofailover will create new db and new user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGIXJiZg-LYMu48DSp03LA.png" /></figure><p>After post successful configuration of the monitor node, it is time to move to set up the coordinator node.</p><h3>Configuration for Coordinator Node</h3><p>This creates a Citus coordinator, that is to say a Postgres node with the Citus extension loaded and ready to act as a coordinator.</p><h4>Create coordinator primary node</h4><pre>[coordinator node1]$ pg_autoctl create coordinator --pgport 5433  --pgdata /data/postgres/coor/ --auth trust --ssl-self-signed --username vina  --dbname mydbops --hostname 192.168.56.2 --pgctl /usr/lib/postgresql/14/bin/pg_ctl  --monitor &#39;postgres://autoctl_node@192.168.56.2:5431/pg_auto_failover?sslmode=require&#39;  <br>pgdata   —&gt; Path of the data directory <br>username —&gt; User to be created in the database <br>dbname   —&gt; Database to be created in the database  <br>hostname —&gt; Hostname of the primary database (Current node) <br>pg_ctl   —&gt; Absolute path of the pg_ctl binary monitor: <br>autoctl_node —&gt; Default user to connect to the monitor node.  <br>192.168.56.2:5431 —&gt; Hostname or IP of the monitor node along with the port. <br>pg_auto_failover —&gt; Database created in the monitor node at the time of configuration.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nmOmO5fQ9XZhbBHDwShRlg.png" /></figure><h4>Run coordinator primary node</h4><p>Run the pg_autoctl service as coordinator</p><pre>[coordinator node]$ pg_autoctl run --pgdata /data/postgres/coor/ --pgport 5433</pre><p>And confirm the configuration by checking status on monitor node</p><pre>[monitor node]$ pg_autoctl show state --monitor &#39;postgres://autoctl_node@192.168.56.2:5431</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7izBCTX45QsA28yVB3wKWw.png" /></figure><h4>Create coordinator secondary node</h4><p>pg_auto_failover performs the standby setup as well by performing pg_basebackup for the initial sync. Before creating secondary coordinator, do checkpoint on primary coordinator</p><pre>[coordinator node1]$ psql -p 5433 <br>psql (14.7 (Ubuntu 14.7-1.pgdg22.04+1)) <br>Type &quot;help&quot; for help. <br>postgres=# checkpoint;<br></pre><p>Move to coordinator node secondary and invoke command pg_autoctl create coordinator</p><pre>[coordinator node2]$ pg_autoctl create coordinator --pgport 5433 <br>--pgdata /data/postgres/coor/ <br>--auth trust --ssl-self-signed --username vina --dbname mydbops <br>--hostname 192.168.56.3 --pgctl /usr/lib/postgresql/14/bin/pg_ctl <br>--monitor &#39;postgres://autoctl_node@192.168.56.2:5431/pg_auto_failover?sslmode=require&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MrbJ-vocEH-0fjoSWJX46g.png" /></figure><h4>Run coordinator secondary node</h4><p>After basebackup complete, we need to start coordinator</p><p>Run the pg_autoctl service as coordinator</p><p>[coordinator node]$ pg_autoctl run --pgdata /data/postgres/coor/ --pgport 5433</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nD2o7Kku-OwnutLe-olk9A.png" /></figure><p>And confirm the configuration by checking status on monitor node, currently 2 nodes of coordinator successfully configured</p><p>[monitor node]$ pg_autoctl show state --monitor &#39;postgres://autoctl_node@192.168.56.2:5431/pg_auto_failover?sslmode=require&#39;</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9b010acaf14" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>