{"id":967,"date":"2026-01-04T10:34:05","date_gmt":"2026-01-04T10:34:05","guid":{"rendered":"https:\/\/tensorzen.blog\/?p=967"},"modified":"2026-01-07T09:00:53","modified_gmt":"2026-01-07T09:00:53","slug":"persisting-postgresql-data-on-host","status":"publish","type":"post","link":"https:\/\/tensorzen.blog\/?p=967","title":{"rendered":"Persisting PostgreSQL data on host"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Option A, host bind mount<\/h2>\n\n\n\n<p>best for direct host access and backups<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mount a host directory to the container&#8217;s data dir (usually \/var\/lib\/postgresql\/data)<\/li>\n\n\n\n<li>Pros: you can back up\/edit files directly on the host; easy to inspect.<\/li>\n\n\n\n<li>Cons: host permission must align with the postgre user inside the container; slightly more OS-specific concerns.<\/li>\n<\/ul>\n\n\n\n<p>Example(Linxu):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prepare host dir: mkdir -p \/host\/postgre-data; chwon 999:999 \/host\/postgre-data<\/li>\n\n\n\n<li>Run: docker run &#8211;name my-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=mydb -v \/host\/postgres-data:\/var\/lib\/postgresql\/data -p 5432:5432 -d postgres:15<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Option B, Docker named volume<\/h2>\n\n\n\n<p>best for Docker lifecycle management<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Use a named volume to store data (still persists beyond container life; data lives on the host, but managed by Docker)<\/li>\n\n\n\n<li>Pros: easier to manage, fewer permission issues, better protability.<\/li>\n\n\n\n<li>Cons: you don&#8217;t directly browse host files unless you inspect the volume.<\/li>\n<\/ol>\n\n\n\n<p>Example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create and run: \n<ul class=\"wp-block-list\">\n<li>docker volume create pgdata<\/li>\n\n\n\n<li>docker run &#8211;name my-postgres -e POSTGRES_PASSWORD=secret -e  -v pgdata:\/var\/lib\/postgresql -p 5432:5432 -d postgres:15<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Where data is stored on host:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker volume inspect pgdata<\/code><\/pre>\n\n\n\n<p>Create a database:<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Option A, host bind mount best for direct host access and backups Example(Linxu): Option B, Docker named volume best for Docker lifecycle management Example: Where data is stored [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[36],"class_list":["post-967","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux"],"_links":{"self":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=967"}],"version-history":[{"count":3,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":969,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/967\/revisions\/969"}],"wp:attachment":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}