<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ali Dadmand</title>
    <description>The latest articles on DEV Community by Ali Dadmand (@dadmand).</description>
    <link>https://dev.to/dadmand</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3401345%2F9252c4f6-a9b9-4744-8422-ee87042009af.jpg</url>
      <title>DEV Community: Ali Dadmand</title>
      <link>https://dev.to/dadmand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dadmand"/>
    <language>en</language>
    <item>
      <title>🐘 One-Click Production-Ready PostgreSQL HA Cluster with Docker, Patroni, and HAProxy</title>
      <dc:creator>Ali Dadmand</dc:creator>
      <pubDate>Wed, 30 Jul 2025 21:43:50 +0000</pubDate>
      <link>https://dev.to/dadmand/one-click-production-ready-postgresql-ha-cluster-with-docker-patroni-and-haproxy-4mfj</link>
      <guid>https://dev.to/dadmand/one-click-production-ready-postgresql-ha-cluster-with-docker-patroni-and-haproxy-4mfj</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;What if you could spin up a full-featured, highly available PostgreSQL cluster — with failover, monitoring, backups, and load balancing — in a single command?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In modern infrastructure, high availability is no longer a luxury. Whether you're running SaaS, fintech, analytics, or internal platforms, &lt;strong&gt;PostgreSQL must stay online, fast, and observable&lt;/strong&gt; — without manual recovery scripts or ops nightmares.&lt;/p&gt;

&lt;p&gt;That’s why I built this:&lt;br&gt;
A &lt;strong&gt;one-click, production-grade PostgreSQL HA cluster&lt;/strong&gt; using &lt;strong&gt;Patroni, HAProxy, Prometheus, and Docker&lt;/strong&gt; — fully optimized for scale, observability, and operational simplicity.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ What You Get — In One Command
&lt;/h2&gt;

&lt;p&gt;With just one script, you'll launch a cluster that includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔁 &lt;strong&gt;Automatic failover&lt;/strong&gt; with Patroni&lt;/li&gt;
&lt;li&gt;⚖️ &lt;strong&gt;Load balancing&lt;/strong&gt; via HAProxy&lt;/li&gt;
&lt;li&gt;📈 &lt;strong&gt;Monitoring dashboards&lt;/strong&gt; (Grafana + Prometheus)&lt;/li&gt;
&lt;li&gt;💾 &lt;strong&gt;Automated backups&lt;/strong&gt; and performance tuning scripts&lt;/li&gt;
&lt;li&gt;🔐 &lt;strong&gt;Secure endpoints&lt;/strong&gt; ready for production hardening&lt;/li&gt;
&lt;li&gt;🐳 &lt;strong&gt;Docker-based&lt;/strong&gt;, no system install required&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 Why I Built This
&lt;/h2&gt;

&lt;p&gt;Most PostgreSQL HA setups are complex, manual, and poorly documented. I needed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A setup I could run in &lt;strong&gt;15 minutes or less&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;With all the &lt;strong&gt;hard parts automated&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Something I could trust in &lt;strong&gt;real production&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;And extensible enough for future growth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This project solves that — clean, documented, and &lt;strong&gt;ready for 1M+ users with minimal resources&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚙️ Technology Stack
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Core database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Patroni&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;HA manager and failover controller&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;etcd&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Distributed consensus store&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;HAProxy&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Load balancer for read/write routing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Prometheus&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Time-series metrics collection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Grafana&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Dashboards and alerting&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🚀 How to Launch It (One Click Setup)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Docker + Docker Compose&lt;/li&gt;
&lt;li&gt;At least 4GB RAM&lt;/li&gt;
&lt;li&gt;No conflicting services on ports: &lt;code&gt;5432&lt;/code&gt;, &lt;code&gt;5433&lt;/code&gt;, &lt;code&gt;3000&lt;/code&gt;, &lt;code&gt;7001&lt;/code&gt;, &lt;code&gt;9090&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setup Command
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/dadmand/postgres-ha-cluster.git
&lt;span class="nb"&gt;cd &lt;/span&gt;postgres-ha-cluster
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x setup_with_monitoring.sh
./setup_with_monitoring.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within minutes, you'll have a fully running HA PostgreSQL stack on your local machine or cloud instance.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔐 Secure Endpoints
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;URL/Port&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL RW&lt;/td&gt;
&lt;td&gt;&lt;code&gt;localhost:5432&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;via HAProxy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL Read&lt;/td&gt;
&lt;td&gt;&lt;code&gt;localhost:5433&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;direct read replica access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Grafana&lt;/td&gt;
&lt;td&gt;&lt;code&gt;localhost:3000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Dashboards (user: admin)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prometheus&lt;/td&gt;
&lt;td&gt;&lt;code&gt;localhost:9090&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Metrics browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HAProxy Stats&lt;/td&gt;
&lt;td&gt;&lt;code&gt;localhost:7001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Load balancer monitoring&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ⚡ Optimized for Production
&lt;/h2&gt;

&lt;h3&gt;
  
  
  PostgreSQL Tuning
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;max_connections&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2000&lt;/span&gt;
&lt;span class="na"&gt;shared_buffers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;1GB&lt;/span&gt;
&lt;span class="na"&gt;work_mem&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;4MB&lt;/span&gt;
&lt;span class="na"&gt;checkpoint_completion_target&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.9&lt;/span&gt;
&lt;span class="na"&gt;autovacuum_max_workers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lightweight Resource Allocation
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Service&lt;/th&gt;
&lt;th&gt;RAM&lt;/th&gt;
&lt;th&gt;CPU&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PostgreSQL&lt;/td&gt;
&lt;td&gt;1GB&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;etcd&lt;/td&gt;
&lt;td&gt;256MB&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HAProxy&lt;/td&gt;
&lt;td&gt;128MB&lt;/td&gt;
&lt;td&gt;0.25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prometheus&lt;/td&gt;
&lt;td&gt;512MB&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Grafana&lt;/td&gt;
&lt;td&gt;256MB&lt;/td&gt;
&lt;td&gt;0.5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Total:&lt;/strong&gt; ~4GB RAM and 8 CPU cores&lt;br&gt;
Tested on macOS, Linux, and cloud VMs.&lt;/p&gt;




&lt;h2&gt;
  
  
  📈 Observability &amp;amp; Alerting
&lt;/h2&gt;

&lt;p&gt;Grafana comes pre-configured with dashboards for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🧠 PostgreSQL performance (connections, slow queries, cache hit ratio)&lt;/li&gt;
&lt;li&gt;⚙️ Replication status and lag&lt;/li&gt;
&lt;li&gt;📊 HAProxy traffic metrics&lt;/li&gt;
&lt;li&gt;💻 System resource usage (CPU, RAM, Disk)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pre-defined alerting thresholds (e.g., replication lag &amp;gt; 10s, disk usage &amp;gt; 85%) are easy to modify in Grafana.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 Backups and Recovery
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Manual Backup:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./config/backup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Simulate Failover:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose stop patroni1
docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;patroni2 patronictl list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reset Cluster:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose down &lt;span class="nt"&gt;-v&lt;/span&gt;
./setup_with_monitoring.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔒 Secure by Default — Hardening Checklist
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Before going live in production:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Change default passwords&lt;/li&gt;
&lt;li&gt;[ ] Configure SSL/TLS for DB and dashboards&lt;/li&gt;
&lt;li&gt;[ ] Set up firewall rules for ports 5432/3000/7001&lt;/li&gt;
&lt;li&gt;[ ] Encrypt backup storage&lt;/li&gt;
&lt;li&gt;[ ] Enable alert channels in Grafana (Slack, email, etc.)&lt;/li&gt;
&lt;li&gt;[ ] Integrate with your CI/CD pipeline&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔚 Why This Matters
&lt;/h2&gt;

&lt;p&gt;You shouldn’t need a DevOps team just to run PostgreSQL with high availability.&lt;/p&gt;

&lt;p&gt;This project gives you a &lt;strong&gt;one-click entry point into resilient, observable PostgreSQL&lt;/strong&gt; — whether you're a solo developer, startup founder, or architect rolling out infrastructure at scale.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 Get the Code
&lt;/h2&gt;

&lt;p&gt;🔗 &lt;strong&gt;GitHub Repo:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/dadmand/postgres-ha-cluster" rel="noopener noreferrer"&gt;github.com/dadmand/postgres-ha-cluster&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📜 License: Apache 2.0&lt;br&gt;
🧠 Designed &amp;amp; maintained by &lt;strong&gt;Ali Dadmand&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🙌 Like This?
&lt;/h2&gt;

&lt;p&gt;Give it a ⭐ on GitHub&lt;br&gt;
Share it with your team&lt;br&gt;
Or fork it for your own production rollout&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>docker</category>
      <category>cluster</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
