<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>database Archives - ServerStadium</title>
	<atom:link href="https://serverstadium.com/knowledge-base/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>https://serverstadium.com/knowledge-base/tag/database/</link>
	<description>Public Cloud Solution by ServerStadium</description>
	<lastBuildDate>Fri, 24 May 2024 18:06:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://serverstadium.com/wp-content/uploads/2023/04/cropped-favicon-1-32x32.png</url>
	<title>database Archives - ServerStadium</title>
	<link>https://serverstadium.com/knowledge-base/tag/database/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Setting Up Redis on Server Stadium Cloud VMs or Dedicated Servers</title>
		<link>https://serverstadium.com/knowledge-base/setting-up-redis-on-server-stadium-cloud-vm-or-dedicated-server/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Fri, 24 May 2024 18:06:58 +0000</pubDate>
				<guid isPermaLink="false">https://serverstadium.com/?post_type=epkb_post_type_1&#038;p=11951</guid>

					<description><![CDATA[<p>Introduction Redis is renowned for its high performance and flexibility, making it a great choice for applications requiring rapid data access. This guide will walk you through the process of installing and configuring Redis on a Server Stadium cloud VM or dedicated server. Prerequisites A Server Stadium cloud VM or dedicated server running Ubuntu 20.04&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/setting-up-redis-on-server-stadium-cloud-vm-or-dedicated-server/">Setting Up Redis on Server Stadium Cloud VMs or Dedicated Servers</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h3>Introduction</h3>
<p>Redis is renowned for its high performance and flexibility, making it a great choice for applications requiring rapid data access. This guide will walk you through the process of installing and configuring Redis on a Server Stadium cloud VM or dedicated server.</p>
<h3>Prerequisites</h3>
<ul>
<li>A Server Stadium <a href="https://serverstadium.com/pricing/" target="_new" rel="noreferrer noopener">cloud VM</a> or <a href="https://serverstadium.com/dedicated-server/" target="_new" rel="noreferrer noopener">dedicated server</a> running Ubuntu 20.04 LTS or 22.04 LTS. If you do not have an account, <a href="https://cp.serverstadium.com/regist-user" target="_new" rel="noreferrer noopener">sign up here</a>.</li>
<li>Administrative or sudo privileges on your server.</li>
<li>Basic knowledge of Linux command line.</li>
</ul>
<h3>Step 1: Install Redis</h3>
<ol>
<li><strong>Update Your Server&#8217;s Package Index</strong><br />
Start by ensuring your server&#8217;s package list is up-to-date:<br />
<div class="shortcode-code">
<p> sudo apt update<br />
sudo apt upgrade<br />
</p></div></li>
<li><strong>Install Redis</strong><br />
Install Redis directly from the Ubuntu repository:<br />
<div class="shortcode-code">
<p> sudo apt install redis-server<br />
</p></div></li>
<li><strong>Configure Redis</strong><br />
By default, Redis is not configured to start automatically at boot. Open the Redis configuration file to adjust this:<br />
<div class="shortcode-code">
<p> sudo nano /etc/redis/redis.conf<br />
</p></div> Change the <code>supervised</code> directive from <code>no</code> to <code>systemd</code>.</li>
<li><strong>Restart Redis Service</strong><br />
Apply the changes and restart the Redis service:<br />
<div class="shortcode-code">
<p> sudo systemctl restart redis.service<br />
</p></div></li>
</ol>
<h3>Step 2: Secure Redis</h3>
<ol>
<li><strong>Set a Redis Password</strong><br />
Open the Redis configuration file to set up a password:<br />
<div class="shortcode-code">
<p> sudo nano /etc/redis/redis.conf<br />
</p></div> Locate the <code># requirepass foobared</code> line, uncomment it, and change <code>foobared</code> to a strong password.</li>
<li><strong>Restrict Redis Connectivity</strong><br />
By default, Redis listens on all interfaces. Modify it to accept connections only from local applications unless you require external accessibility:<br />
<div class="shortcode-code">
<p> bind 127.0.0.1<br />
</p></div></li>
</ol>
<h3>Step 3: Test Redis Installation</h3>
<ol>
<li><strong>Check Redis Functioning</strong><br />
Verify that Redis is running properly:<br />
<div class="shortcode-code">
<p> redis-cli ping<br />
</p></div> If Redis is configured correctly, it should return <code>PONG</code>.</li>
<li><strong>Test Redis with Password</strong><br />
Connect to Redis with authentication:<br />
<div class="shortcode-code">
<p> redis-cli -a yourpassword<br />
</p></div></li>
</ol>
<h3>Conclusion</h3>
<p>Redis is now set up and secured on your Server Stadium cloud VM or dedicated server. It’s ready to be used as a high-performance cache and message broker for your applications, enhancing speed and efficiency.</p>
<p>For further customization and advanced Redis usage scenarios, visit our <a href="https://serverstadium.com/knowledge-base/" target="_new" rel="noreferrer noopener">knowledge base</a> or consult the <a href="https://serverstadium.com/" target="_new" rel="noreferrer noopener">Server Stadium website</a>.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/setting-up-redis-on-server-stadium-cloud-vm-or-dedicated-server/">Setting Up Redis on Server Stadium Cloud VMs or Dedicated Servers</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Setting Up a MongoDB Database on ServerStadium Cloud Instances</title>
		<link>https://serverstadium.com/knowledge-base/setting-up-a-mongodb-database-on-serverstadium-cloud-instances/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Fri, 26 May 2023 03:39:18 +0000</pubDate>
				<guid isPermaLink="false">https://serverstadium.com/?post_type=epkb_post_type_1&#038;p=11677</guid>

					<description><![CDATA[<p>MongoDB is a popular NoSQL database widely used in modern web applications. This tutorial will guide you through setting up a MongoDB database on your ServerStadium cloud instance. For more tutorials and guides, make sure to visit our Knowledge Base. Step 1: Register Your Account If you haven&#8217;t already, you&#8217;ll need to create an account&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/setting-up-a-mongodb-database-on-serverstadium-cloud-instances/">Setting Up a MongoDB Database on ServerStadium Cloud Instances</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>MongoDB is a popular NoSQL database widely used in modern web applications. This tutorial will guide you through setting up a MongoDB database on your ServerStadium cloud instance.</p>
<p>For more tutorials and guides, make sure to visit our <a href="https://serverstadium.com/knowledge-base/" target="_new" rel="noopener">Knowledge Base</a>.</p>
<h2>Step 1: Register Your Account</h2>
<p>If you haven&#8217;t already, you&#8217;ll need to create an account on our <a href="https://cp.serverstadium.com/regist-user" target="_new" rel="noopener">Registration Page</a>. Once registered, you&#8217;ll have access to your personal <a href="https://cp.serverstadium.com/" target="_new" rel="noopener">Cloud Dashboard</a>.</p>
<h2>Step 2: Add Your Cloud Instance</h2>
<p>From your <a href="https://cp.serverstadium.com/" target="_new" rel="noopener">Cloud Dashboard</a>, click the &#8216;Add VM&#8217; button. Choose a cloud instance that suits your needs and budget. For more detailed information about our offerings, you can visit our <a href="https://serverstadium.com/pricing/" target="_new" rel="noopener">Pricing Page</a>.</p>
<h2>Step 3: Install MongoDB</h2>
<p>Log into your ServerStadium cloud instance and install MongoDB with the following commands:</p>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash">wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -<br />
</code></div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse"</span> | sudo <span class="hljs-built_in">tee</span> /etc/apt/sources.list.d/mongodb-org-4.4.list</code></div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash">sudo apt-get update</code></div>
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash">sudo apt-get install -y mongodb-org<br />
</code></div>
</div>
<p>These commands will add the MongoDB repository to your sources list, update your package list, and install MongoDB.</p>
<h2>Step 4: Start MongoDB Service</h2>
<p>Once MongoDB is installed, start the MongoDB service with the following command:</p>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash">sudo systemctl start mongod<br />
</code></div>
</div>
<p>You can also set MongoDB to start automatically at boot with this command:</p>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash">sudo systemctl <span class="hljs-built_in">enable</span> mongod<br />
</code></div>
</div>
<h2>Step 5: Verify MongoDB Installation</h2>
<p>Verify that MongoDB is running correctly with the following command:</p>
<div class="bg-black rounded-md mb-4">
<div class="p-4 overflow-y-auto"><code class="!whitespace-pre hljs language-bash">mongo --<span class="hljs-built_in">eval</span> <span class="hljs-string">'db.runCommand({ connectionStatus: 1 })'</span><br />
</code></div>
</div>
<p>This command will return a status document that contains various information about the MongoDB connection.</p>
<h2>Conclusion</h2>
<p>Congratulations! You&#8217;ve successfully set up a MongoDB database on your ServerStadium cloud instance. You&#8217;re now ready to start building your MongoDB-based application.</p>
<p>Visit our <a href="https://serverstadium.com/knowledge-base/" target="_new" rel="noopener">Knowledge Base</a> for more tutorials and guides.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/setting-up-a-mongodb-database-on-serverstadium-cloud-instances/">Setting Up a MongoDB Database on ServerStadium Cloud Instances</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Set Up High Availability PostgreSQL Cluster Using Patroni on ServerStadium</title>
		<link>https://serverstadium.com/knowledge-base/set-up-high-availability-postgresql-cluster-using-patroni-on-serverstadium/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Wed, 06 Jul 2022 07:40:20 +0000</pubDate>
				<guid isPermaLink="false">https://cloudraya.com/knowledge-base/?p=2096</guid>

					<description><![CDATA[<p>PostgreSQL is a powerful OpenSource, an object-relational database management system that extends the SQL language with many features. Meanwhile, Patroni is an OpenSource python package that we can use to manage and automate the deployment of PostgreSQL HA Clusters. Little information, Patroni was developed by Zalando. Prerequisite To follow this tutorial, we need three VM&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/set-up-high-availability-postgresql-cluster-using-patroni-on-serverstadium/">Set Up High Availability PostgreSQL Cluster Using Patroni on ServerStadium</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">PostgreSQL is a powerful OpenSource, an object-relational database management system that extends the SQL language with many features.</p>



<p class="wp-block-paragraph">Meanwhile, Patroni is an OpenSource python package that we can use to manage and automate the deployment of PostgreSQL HA Clusters. Little information, Patroni was developed by Zalando.</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Prerequisite</strong></h2>



<p class="wp-block-paragraph">To follow this tutorial, we need three VM for PostgreSQL and one VM for HAproxy and etcd.<br>Below is four VMs that we will deploy in ServerStadium.</p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<figure class="wp-block-table"><table><tbody><tr><td class="has-text-align-center" data-align="center"><strong>Hostname</strong></td><td class="has-text-align-center" data-align="center"><strong>IP Address</strong></td><td class="has-text-align-left" data-align="left"><strong>Applications</strong></td></tr><tr><td class="has-text-align-center" data-align="center">haetcd</td><td class="has-text-align-center" data-align="center">199.180.130.99</td><td class="has-text-align-left" data-align="left">HAproxy</td></tr><tr><td class="has-text-align-center" data-align="center"></td><td class="has-text-align-center" data-align="center">10.3.3.166</td><td class="has-text-align-left" data-align="left">etcd</td></tr><tr><td class="has-text-align-center" data-align="center">pgsql1</td><td class="has-text-align-center" data-align="center">10.3.3.12</td><td class="has-text-align-left" data-align="left">patroni</td></tr><tr><td class="has-text-align-center" data-align="center">pgsql2</td><td class="has-text-align-center" data-align="center">10.3.3.71</td><td class="has-text-align-left" data-align="left">patroni</td></tr><tr><td class="has-text-align-center" data-align="center">pgsql3</td><td class="has-text-align-center" data-align="center">10.3.3.70</td><td class="has-text-align-left" data-align="left">patroni</td></tr></tbody></table></figure>
</div>
</div>



<p class="wp-block-paragraph">We will use haetcd as jumpbox server since the VM has Public IP Address that we can access from the outside network. In /etc/host, insert the hostname of the servers to make us easy to access the server without remembering the IP Address.</p>



<p class="wp-block-paragraph">/etc/hosts</p>



<figure class="wp-block-image size-full"><img decoding="async" width="284" height="144" src="https://serverstadium.com/wp-content/uploads/2022/05/image.png" alt="" class="wp-image-2098"/></figure>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install PostgreSQL 14</strong></h2>



<p class="wp-block-paragraph">In this step, we will use PostgreSQL14 for pgsql1, pgsql2, and pgsql3. Please the command below on each server.</p>



<pre class="wp-block-preformatted"><code>sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" &gt; /etc/apt/sources.list.d/pgdg.list'</code>
<code>
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

apt update</code></pre>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1157" height="260" src="https://serverstadium.com/wp-content/uploads/2022/05/image-2.png" alt="" class="wp-image-2100" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-2.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-2-300x67.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-2-1024x230.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-2-768x173.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<pre class="wp-block-preformatted">apt -y install postgresql-14 postgresql-server-dev-14</pre>



<figure class="wp-block-image size-full"><img decoding="async" width="1157" height="131" src="https://serverstadium.com/wp-content/uploads/2022/05/image-3.png" alt="" class="wp-image-2101" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-3.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-3-300x34.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-3-1024x116.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-3-768x87.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">After the postgresql installation is completed, we need to stop and disable PostgreSQL service. Then we will continue to install patroni.</p>



<p class="wp-block-paragraph"> <code>systemctl stop postgresql &amp;&amp; systemctl disable postgresql</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install Patroni</strong></h2>



<p class="wp-block-paragraph">Patroni uses the utilities of PostgreSQL that we have installed previously which is located at <strong>/usr/lib/postgresql/versin/bin </strong>directory.</p>



<p class="wp-block-paragraph">In this step, we create a symbolic link to PostgreSQL utilities on psql1, psql2, and psql3 </p>



<p class="wp-block-paragraph"><code>ln -s /usr/lib/postgresql/14/bin/* /usr/sbin/ </code></p>



<p class="wp-block-paragraph">Next, we will install patroni on each server</p>



<p class="wp-block-paragraph"><code>apt -y install python3 python3-pip <br>pip install --upgrade setuptools <br>pip install psycopg2 <br>pip install patroni <br>pip install python-etcd</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="340" src="https://serverstadium.com/wp-content/uploads/2022/05/image-4.png" alt="" class="wp-image-2102" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-4.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-4-300x88.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-4-1024x301.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-4-768x226.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph"></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Configure Patroni on Pgsql1, Pgsql2, and Pgsql3</strong></h2>



<p class="wp-block-paragraph">Patroni uses yaml files to store the configuration. We will place the yaml file on /etc directory. </p>



<pre class="wp-block-preformatted">sudo nano /etc/patroni.yml

<strong>pgsql1:</strong>
scope: postgres
namespace: /db/
name: <strong>pgsql1</strong>

restapi:
    listen: <strong>10.3.3.12</strong>:8008
    connect_address: <strong>10.3.3.12</strong>:8008

etcd:
    host:  <strong>10.3.3.166</strong>:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator 10.3.3.12/0 md5
    - host replication replicator 10.3.3.71/0 md5
    - host replication replicator 10.3.3.70/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: <strong>10.3.3.12</strong>:5432
    connect_address: <strong>10.3.3.12</strong>:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replicator
            password: replogin321
        superuser:
            username: postgres
            password: secretlogin321
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

<strong>pgsql2</strong>
scope: postgres
namespace: /db/
name: <strong>pgsql2</strong>

restapi:
    listen: <strong>10.3.3.71</strong>:8008
    connect_address: <strong>10.3.3.71</strong>:8008

etcd:
    host:  <strong>10.3.3.166</strong>:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator 10.3.3.12/0 md5
    - host replication replicator 10.3.3.71/0 md5
    - host replication replicator 10.3.3.70/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: <strong>10.3.3.71</strong>:5432
    connect_address: <strong>10.3.3.71</strong>:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replicator
            password: replogin321
        superuser:
            username: postgres
            password: secretlogin321
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

<strong>pgsql3
</strong>scope: postgres
namespace: /db/
name: <strong>pgsql3</strong>

restapi:
    listen: <strong>10.3.3.70</strong>:8008
    connect_address: <strong>10.3.3.70</strong>:8008

etcd:
    host:  10.3.3.166:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator 10.3.3.12/0 md5
    - host replication replicator 10.3.3.71/0 md5
    - host replication replicator 10.3.3.70/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: <strong>10.3.3.70</strong>:5432
    connect_address: <strong>10.3.3.70</strong>:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replicator
            password: replogin321
        superuser:
            username: postgres
            password: secretlogin321
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false
</pre>



<p class="wp-block-paragraph">According to the yml file, the data of PostgreSQL will be stored at /data/patroni. Make sure the ownership of that directory is postgres.</p>



<p class="wp-block-paragraph"><code>mkdir -p /data/patroni</code><br>chown postgres:postgres /data/patroni<br><code>chmod 700 /data/patroni</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Create a Service</strong></h2>



<p class="wp-block-paragraph">Next, we will create patroni service that allow us to start and stop the service on each server.</p>



<p class="wp-block-paragraph"><code>sudo nano /etc/systemd/system/patroni.service</code></p>



<p class="wp-block-paragraph">Add the following parameter in the patroni.service<code><br>[Unit]<br>Description=High availability PostgreSQL Cluster<br>After=syslog.target network.target<br>[Service]<br>Type=simple<br>User=postgres<br>Group=postgres<br>ExecStart=/usr/local/bin/patroni /etc/patroni.yml<br>KillMode=process<br>TimeoutSec=30<br>Restart=no<br><br>[Install]<br>WantedBy=multi-user.target</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Create and Enable Patroni Service</strong></h2>



<p class="wp-block-paragraph">After the patroni service has been created then we enable it to make it autostart.</p>



<p class="wp-block-paragraph"><code>sudo systemctl daemon-reload<br>sudo systemctl enable patroni</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install Etcd and Haproxy</strong> <strong>on Haetcd VM</strong></h2>



<p class="wp-block-paragraph">etcd is a fault-tolerant, a distributed key value store that can be used to store the state of PostgreSQL cluster.  With etcd, it will keep the PostgreSQL cluster up and running.</p>



<p class="wp-block-paragraph">Run the command below to install etcd</p>



<pre class="wp-block-preformatted">sudo apt -y install etcd</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="156" src="https://serverstadium.com/wp-content/uploads/2022/05/image-5.png" alt="" class="wp-image-2104" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-5.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-5-300x40.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-5-1024x138.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-5-768x104.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">Next step, we will configure the etcd file. Add the below parameter in the etcd file:</p>



<p class="wp-block-paragraph"><code>sudo nano /etc/default/etcd</code></p>



<p class="wp-block-paragraph"><code>ETCD_LISTEN_PEER_URLS="http://<strong>10.3.3.166</strong>:2380"<br>ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://<strong>10.3.3.166</strong>:2379"<br>ETCD_INITIAL_ADVERTISE_PEER_URLS="http://<strong>10.3.3.166</strong>:2380"<br>ETCD_INITIAL_CLUSTER="default=http://<strong>10.3.3.166</strong>:2380"<br>ETCD_ADVERTISE_CLIENT_URLS="http://<strong>10.3.3.166</strong>:2379"<br>ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"<br>ETCD_INITIAL_CLUSTER_STATE="new"</code><br></p>



<p class="wp-block-paragraph">Restart the etcd service to make a change effect</p>



<p class="wp-block-paragraph"><code>sudo systemctl restart etcd<br></code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Start Patroni on Each Pgsql Server</strong></h2>



<p class="wp-block-paragraph">Before we start the patroni service, we need to activate the watchdog support to prevent split-brain on the PostgreSQL<br><br>https://patroni.readthedocs.io/en/latest/watchdog.html<br><br><code>modprobe softdog<br>chown postgres /dev/watchdog</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="705" height="56" src="https://serverstadium.com/wp-content/uploads/2022/05/image-6.png" alt="" class="wp-image-2105" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-6.png 705w, https://serverstadium.com/wp-content/uploads/2022/05/image-6-300x24.png 300w" sizes="(max-width: 705px) 100vw, 705px" /></figure>



<p class="wp-block-paragraph">After the watchdog is activated, now we can start the patroni service again on each server.</p>



<p class="wp-block-paragraph"><code>systemctl start patroni</code><br><code>systemctl status patroni</code></p>



<p class="wp-block-paragraph">pgsql1 as master. <br>We can see, the pgsql1 will act as the PostgreSQL master</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="372" src="https://serverstadium.com/wp-content/uploads/2022/05/image-7.png" alt="" class="wp-image-2106" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-7.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-7-300x96.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-7-1024x329.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-7-768x247.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">pgsql2 and pgsql3 as slave</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="366" src="https://serverstadium.com/wp-content/uploads/2022/05/image-8.png" alt="" class="wp-image-2107" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-8.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-8-300x95.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-8-1024x324.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-8-768x243.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="353" src="https://serverstadium.com/wp-content/uploads/2022/05/image-9.png" alt="" class="wp-image-2108" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-9.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-9-300x92.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-9-1024x312.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-9-768x234.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install Haproxy</strong></h2>



<p class="wp-block-paragraph">Our last step install HAproxy service to give us an endpoint to which the application can connect to the database.</p>



<p class="wp-block-paragraph"><code>sudo apt -y install haproxy</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="146" src="https://serverstadium.com/wp-content/uploads/2022/05/image-10.png" alt="" class="wp-image-2109" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-10.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-10-300x38.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-10-1024x129.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-10-768x97.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph" id="block-afeeab82-1a19-4d69-a234-c7d500db5846">Edit the haproxy file configuration as follow</p>



<pre class="wp-block-preformatted"><code>nano /etc/haproxy/haproxy.cfg</code>
global
    maxconn 100

defaults
    log global
    mode tcp
    retries 2
    timeout client 30m
    timeout connect 4s
    timeout server 30m
    timeout check 5s

listen stats
    mode http
    bind *:7000
    stats enable
    stats uri /

listen postgres
    bind *:5000
    option httpchk
    http-check expect status 200
    default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
    server pgsql1 <strong>10.3.3.12</strong>:5432 maxconn 100 check port 8008
    server pgsql2 <strong>10.3.3.71</strong>:5432 maxconn 100 check port 8008
    server pgsql3 <strong>10.3.3.70</strong>:5432 maxconn 100 check port 8008
</pre>



<p class="wp-block-paragraph">Check our haproxy configuration using the following command:</p>



<pre class="wp-block-preformatted">sudo /usr/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg&nbsp;</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1000" height="54" src="https://serverstadium.com/wp-content/uploads/2022/05/image-11.png" alt="" class="wp-image-2110" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-11.png 1000w, https://serverstadium.com/wp-content/uploads/2022/05/image-11-300x16.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-11-768x41.png 768w" sizes="(max-width: 1000px) 100vw, 1000px" /></figure>



<p class="wp-block-paragraph">If the configuration is valid, we can restart our haproxy service</p>



<p class="wp-block-paragraph"><code>sudo systemctl restart haproxy</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="307" src="https://serverstadium.com/wp-content/uploads/2022/05/image-12.png" alt="" class="wp-image-2111" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-12.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-12-300x80.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-12-1024x272.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-12-768x204.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In the final step, we can monitor the PostgreSQL cluster using the browser as shown in the screenshot below:</p>



<p class="wp-block-paragraph"><a href="http://199.180.130.99:7000/">http://199.180.130.99:7000/</a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="380" src="https://serverstadium.com/wp-content/uploads/2022/05/image-13.png" alt="" class="wp-image-2112" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-13.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-13-300x99.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-13-1024x336.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-13-768x252.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">As we can see, psql1 is the leader of the PostgreSQL cluster with the green line row.<br>Now we will kill patroni service on psql1, to see which VM will be acting as the leader.</p>



<p class="wp-block-paragraph"><code>systemctl stop patroni</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="374" src="https://serverstadium.com/wp-content/uploads/2022/05/image-14.png" alt="" class="wp-image-2113" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-14.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-14-300x97.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-14-1024x331.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-14-768x248.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">After we stop service patroni service in psql1, the psql2 will be elected as the leader node.</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><br><strong>Test PostgresSQL Cluster</strong></h2>



<p class="wp-block-paragraph">If we want to make a connection to PostgreSQL, we use the IP Address of HAProxy and port 5000.</p>



<p class="wp-block-paragraph"><code>su - postgres<br>psql -U postgres -h 199.180.130.99 -p 5000</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="550" height="96" src="https://serverstadium.com/wp-content/uploads/2022/05/image-15.png" alt="" class="wp-image-2120" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-15.png 550w, https://serverstadium.com/wp-content/uploads/2022/05/image-15-300x52.png 300w" sizes="(max-width: 550px) 100vw, 550px" /></figure>



<p class="wp-block-paragraph"><br>You now have a high availability PostgreSQL Cluster in ServerStadium that is ready to use.<br>Please don&#8217;t hesitate to <a href="https://serverstadium.com/id/contact/" target="_blank" rel="noreferrer noopener">contact ServerStadium support</a> if you need further assistance.</p>



<p class="wp-block-paragraph">Find more tutorials articles about ServerStadium in our Knowledge Base, or if you want to get more general knowledge about technology feel free to visit our <a href="https://serverstadium.com/blog/" target="_blank" rel="noreferrer noopener">Blog.</a> </p>



<p class="wp-block-paragraph">Curious to know more about our products and panel? register <a href="https://cp.serverstadium.com/regist-user" target="_blank" rel="noreferrer noopener">here</a> for free!.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/set-up-high-availability-postgresql-cluster-using-patroni-on-serverstadium/">Set Up High Availability PostgreSQL Cluster Using Patroni on ServerStadium</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
