<?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 Jenin on Medium]]></title>
        <description><![CDATA[Stories by Jenin on Medium]]></description>
        <link>https://medium.com/@jenin8282?source=rss-29c128ffa609------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WyA-FKGjQ3bQ-pb7</url>
            <title>Stories by Jenin on Medium</title>
            <link>https://medium.com/@jenin8282?source=rss-29c128ffa609------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 11:57:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jenin8282/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[Reviving a Crashed PostgreSQL Container in Docker Swarm — Step-by-Step Recovery from WAL Corruption]]></title>
            <link>https://medium.com/@jenin8282/reviving-a-crashed-postgresql-container-in-docker-swarm-step-by-step-recovery-from-wal-corruption-9c5f681f976c?source=rss-29c128ffa609------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c5f681f976c</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[sysadmin]]></category>
            <category><![CDATA[databaserecovery]]></category>
            <dc:creator><![CDATA[Jenin]]></dc:creator>
            <pubDate>Thu, 30 Oct 2025 05:27:01 GMT</pubDate>
            <atom:updated>2025-10-30T05:28:57.305Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reviving a Crashed PostgreSQL Container in Docker Swarm — Step-by-Step Recovery from WAL Corruption</h3><p>If your PostgreSQL service in <strong>Docker Swarm</strong> refuses to start and keeps spitting errors like</p><blockquote><em>PANIC: could not locate a valid checkpoint record</em></blockquote><p>…you’ve likely hit <strong>WAL (Write-Ahead Log) corruption</strong> — usually from a full /run tmpfs, low disk space, or a forced shutdown.</p><p>This guide walks you through safely <strong>resetting the WAL</strong>, verifying your data, and bringing PostgreSQL back online without panic. 🐳💾</p><h3>⚠️ The Symptoms</h3><pre>PANIC: could not locate a valid checkpoint record<br>invalid resource manager ID 24 at 0/B2A8030<br>database system is shut down</pre><p>Your container restarts repeatedly and your service never stabilizes. Let’s fix that.</p><h3>🧱 Step 1 — Stop the Swarm Service</h3><p>Stop the affected service to prevent further restarts while you repair the database:</p><pre>docker service scale &lt;your_postgres_service&gt;=0</pre><h3>💾 Step 2 — Back Up Your Data Volume</h3><p>Always take a snapshot before modifying data. Replace &lt;your_volume&gt; with your actual PostgreSQL volume name:</p><pre>docker run --rm \<br>  -v &lt;your_volume&gt;:/var/lib/postgresql/data \<br>  -v &quot;$PWD&quot;:/backup \<br>  alpine sh -c &#39;cd /var/lib/postgresql &amp;&amp; tar czf /backup/pgdata-pre-reset-$(date +%F-%H%M).tgz data&#39;</pre><p>This creates a .tgz backup of your database volume inside the current host directory.</p><h3>🔍 Step 3 — (Optional) Inspect the WAL File</h3><p>To confirm where corruption starts:</p><pre>docker run --rm -it \<br>  -v &lt;your_volume&gt;:/var/lib/postgresql/data \<br>  --entrypoint bash postgis/postgis:latest</pre><pre>export PGDATA=/var/lib/postgresql/data<br>pg_waldump -p &quot;$PGDATA/pg_wal&quot; 00000001000000000000000B | head -n 40<br>exit</pre><p>This shows the last few valid WAL records.</p><h3>⚙️ Step 4 — Dry-Run a WAL Reset</h3><p>PostgreSQL provides a low-level recovery tool: pg_resetwal.<br> Run a dry-run first (nothing will be changed):</p><pre>docker run --rm -it \<br>  -v &lt;your_volume&gt;:/var/lib/postgresql/data \<br>  --user postgres \<br>  --entrypoint bash postgis/postgis:latest \<br>  -lc &#39;/usr/lib/postgresql/*/bin/pg_resetwal -n -D /var/lib/postgresql/data&#39;</pre><h3>🔧 Step 5 — Force the WAL Reset (Last Resort)</h3><p>If the dry-run looks good, perform the actual reset:</p><pre>docker run --rm -it \<br>  -v &lt;your_volume&gt;:/var/lib/postgresql/data \<br>  --user postgres \<br>  --entrypoint bash postgis/postgis:latest \<br>  -lc &#39;/usr/lib/postgresql/*/bin/pg_resetwal -f -D /var/lib/postgresql/data&#39;</pre><blockquote><em>🧠 </em><strong><em>Note:</em></strong><em> This recreates a clean checkpoint.<br> You might lose the very last committed transactions, but the cluster should become startable again.</em></blockquote><h3>🧩 Step 6 — Verify PostgreSQL Starts Manually</h3><p>Before bringing Swarm back online, verify the database runs:</p><pre>docker run --rm -it \<br>  -v &lt;your_volume&gt;:/var/lib/postgresql/data \<br>  --entrypoint bash postgis/postgis:latest \<br>  -lc &#39;chown -R postgres:postgres /var/lib/postgresql/data &amp;&amp; \<br>       su -s /bin/bash postgres -c &quot;/usr/lib/postgresql/*/bin/postgres -D /var/lib/postgresql/data -c logging_collector=off -c log_min_messages=info&quot;&#39;</pre><p>If it stays up and you see:</p><pre>LOG: listening on IPv4 address &quot;0.0.0.0&quot;, port 5432</pre><p>🎉 You’re good! Press Ctrl+C to stop it.</p><h3>🐳 Step 7 — Bring the Service Back Online</h3><p>Now start the service again in Swarm:</p><pre>docker service scale &lt;your_postgres_service&gt;=1<br>watch -n 1 &#39;docker service ps --no-trunc &lt;your_postgres_service&gt;&#39;<br>CID=$(docker ps --filter name=&lt;your_postgres_service&gt; --format &#39;{{.ID}}&#39;); echo &quot;$CID&quot;<br>docker logs --tail=200 &quot;$CID&quot;</pre><p>You should see the PostgreSQL container running and healthy. <strong><em>You can stop here if everything is working as expected.</em></strong></p><h3>🧠 Step 8 — Check Database Integrity</h3><h3>Check Index Consistency:</h3><pre>CID=$(docker ps --filter name=&lt;your_postgres_service&gt; --format &#39;{{.ID}}&#39;)<br>docker exec -it &quot;$CID&quot; psql -U postgres -d postgres -c &quot;CREATE EXTENSION IF NOT EXISTS amcheck;&quot;</pre><pre>docker exec -it &quot;$CID&quot; psql -U postgres -d &lt;your_database_name&gt; -c &quot;<br>WITH idx AS (<br>  SELECT indexrelid<br>  FROM pg_index i<br>  JOIN pg_class c ON c.oid = i.indexrelid<br>  WHERE i.indisvalid IS DISTINCT FROM false<br>    AND pg_get_indexdef(indexrelid) LIKE &#39;CREATE INDEX%USING btree%&#39;<br>)<br>SELECT bt_index_check(indexrelid, heapallindexed := true) FROM idx;&quot;</pre><p>If you see any issues, rebuild indexes:</p><pre>docker exec -it &quot;$CID&quot; psql -U postgres -d &lt;your_database_name&gt; -c &quot;REINDEX DATABASE CONCURRENTLY &lt;your_database_name&gt;;&quot;</pre><p>And then refresh statistics:</p><pre>docker exec -it &quot;$CID&quot; vacuumdb -U postgres --all --analyze-in-stages</pre><h3>🔒 Step 9 — Prevent Future Corruption</h3><p>✅ <strong>Pin the Docker image</strong><br> Use a fixed major tag instead of :latest:</p><pre>image: postgis/postgis:17</pre><p>✅ <strong>Expand </strong><strong>/run tmpfs</strong><br> Ensure /run has at least 256 MB and add it to /etc/fstab:</p><pre>tmpfs /run tmpfs size=256M,mode=0755 0 0</pre><p>✅ <strong>Enable regular backups and WAL archiving</strong><br> That way, next time you can restore cleanly instead of resetting WAL manually.</p><h3>🎉 Conclusion</h3><p>Recovering from PostgreSQL WAL corruption inside Docker Swarm isn’t fun — but with pg_resetwal and careful backup discipline, you can bring your database back from the dead 🧙‍♂️.</p><p>💪 <em>You’ve just performed real-world database triage — welcome to DBA hero status!</em> 🦸‍♀️💽</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c5f681f976c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>