{"id":454,"date":"2025-07-03T18:49:31","date_gmt":"2025-07-03T18:49:31","guid":{"rendered":"http:\/\/77interactive.com\/?p=454"},"modified":"2025-07-03T18:49:40","modified_gmt":"2025-07-03T18:49:40","slug":"move-postgresql-data-directory-and-config-files","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=454","title":{"rendered":"Move PostgreSQL Data Directory and Config Files"},"content":{"rendered":"\n<p>The following code is used to move the PostgreSQL data directory to a new directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Move the data directory for PostgreSQL 15\n# This script moves the data directory for PostgreSQL 15 to a new location.\n\n# Stop PostgreSQL 15 service\nsudo systemctl stop postgresql-15\n\n# Create the new data directory for PostgreSQL 15\nsudo mkdir -p \/var\/lib\/postgresql\/15\/main\nsudo mkdir -p \/etc\/postgresql\/15\/main\n\n# Copy the data directory from PostgreSQL 15 to PostgreSQL 15\nsudo rsync -av --progress \/var\/lib\/pgsql\/15\/data\/ \/var\/lib\/postgresql\/15\/main\/\n\n# Move Configuration files to the new location\nsudo cp \/var\/lib\/pgsql\/15\/data\/postgresql.conf \/etc\/postgresql\/15\/main\/\nsudo cp \/var\/lib\/pgsql\/15\/data\/pg_hba.conf \/etc\/postgresql\/15\/main\/\nsudo cp \/var\/lib\/pgsql\/15\/data\/pg_ident.conf \/etc\/postgresql\/15\/main\/\n\n# Change ownership of the new data directory\nsudo chown -R postgres:postgres \/var\/lib\/postgresql\/15\/main\nsudo chown -R postgres:postgres \/etc\/postgresql\/15\/main\n\n# Edit the PostgreSQL 15 configuration file\n# Replace the data_directory and hba_file paths in the configuration file\n# with the new paths to the moved data directory and configuration files.\nsudo nano \/etc\/postgresql\/15\/main\/postgresql.conf\n\n# data_directory = '\/var\/lib\/postgresql\/15\/main'          # use data in another directory\n#                                                         # (change requires restart)\n# hba_file = '\/etc\/postgresql\/15\/main\/pg_hba.conf'        # host-based authentication file\n#                                                         # (change requires restart)   \n# ident_file = '\/etc\/postgresql\/15\/main\/pg_ident.conf'    # ident configuration file\n#                                                         # (change requires restart)\n# # If external_pid_file is not explicitly set, no extra PID file is written.\n# external_pid_file = '\/var\/run\/postgresql\/15-main.pid'   # write an extra PID file\n#                                                         # (change requires restart)\n\n# Edit the systemd service file for PostgreSQL 15\nsudo systemctl edit postgresql-15.service\n# If the service file does not exist, create it:\nsudo nano \/etc\/systemd\/system\/postgresql-15.service\n\n# Add the following content to the service file:\n# &#91;Service]\n# Environment=PGDATA=\/var\/lib\/postgresql\/15\/main\n# ExecStart=\n# ExecStart=\/usr\/pgsql-15\/bin\/postgres -D ${PGDATA} -c config_file=\/etc\/postgresql\/15\/main\/postgresql.conf\n\n# Use the correct path to the PostgreSQL 15 binary in the ExecStart line.\n# ExecStart=\/usr\/pgsql-17\/bin\/postgres -D ${PGDATA} -c config_file=\/etc\/postgresql\/15\/main\/postgresql.conf\n\n\n# Reload the systemd daemon to apply the changes\nsudo systemctl daemon-reload\n\n# Start PostgreSQL 15 service\nsudo systemctl start postgresql-15\n\n\n# Compare directory sizes\nsudo du -sh \/var\/lib\/pgsql\/15\/data\nsudo du -sh \/var\/lib\/postgresql\/15\/main\n\n# Remove the old data directory if everything is working fine\nsudo rm -rf \/var\/lib\/pgsql\/15<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following code is used to move the PostgreSQL data directory to a new directory.<\/p>\n","protected":false},"author":1,"featured_media":455,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[17,18],"class_list":["post-454","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql","tag-postgres","tag-postgresql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Move PostgreSQL Data Directory and Config Files - 77 Interactive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/77interactive.com\/?p=454\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Move PostgreSQL Data Directory and Config Files - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"The following code is used to move the PostgreSQL data directory to a new directory.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=454\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-03T18:49:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-03T18:49:40+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2350\" \/>\n\t<meta property=\"og:image:height\" content=\"1280\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Rudy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=454#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=454\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Move PostgreSQL Data Directory and Config Files\",\"datePublished\":\"2025-07-03T18:49:31+00:00\",\"dateModified\":\"2025-07-03T18:49:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=454\"},\"wordCount\":22,\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=454#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png\",\"keywords\":[\"postgres\",\"postgresql\"],\"articleSection\":[\"PostgreSQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=454\",\"url\":\"http:\/\/77interactive.com\/?p=454\",\"name\":\"Move PostgreSQL Data Directory and Config Files - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=454#primaryimage\"},\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=454#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png\",\"datePublished\":\"2025-07-03T18:49:31+00:00\",\"dateModified\":\"2025-07-03T18:49:40+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=454#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=454\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/?p=454#primaryimage\",\"url\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png\",\"contentUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png\",\"width\":2350,\"height\":1280},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=454#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Move PostgreSQL Data Directory and Config Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/77interactive.com\/#website\",\"url\":\"http:\/\/77interactive.com\/\",\"name\":\"77 Interactive\",\"description\":\"Rudy&#039;s Code snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/77interactive.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\",\"name\":\"Rudy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"caption\":\"Rudy\"},\"url\":\"http:\/\/77interactive.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Move PostgreSQL Data Directory and Config Files - 77 Interactive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/77interactive.com\/?p=454","og_locale":"en_US","og_type":"article","og_title":"Move PostgreSQL Data Directory and Config Files - 77 Interactive","og_description":"The following code is used to move the PostgreSQL data directory to a new directory.","og_url":"http:\/\/77interactive.com\/?p=454","og_site_name":"77 Interactive","article_published_time":"2025-07-03T18:49:31+00:00","article_modified_time":"2025-07-03T18:49:40+00:00","og_image":[{"width":2350,"height":1280,"url":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png","type":"image\/png"}],"author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=454#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=454"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Move PostgreSQL Data Directory and Config Files","datePublished":"2025-07-03T18:49:31+00:00","dateModified":"2025-07-03T18:49:40+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=454"},"wordCount":22,"image":{"@id":"http:\/\/77interactive.com\/?p=454#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png","keywords":["postgres","postgresql"],"articleSection":["PostgreSQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=454","url":"http:\/\/77interactive.com\/?p=454","name":"Move PostgreSQL Data Directory and Config Files - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"primaryImageOfPage":{"@id":"http:\/\/77interactive.com\/?p=454#primaryimage"},"image":{"@id":"http:\/\/77interactive.com\/?p=454#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png","datePublished":"2025-07-03T18:49:31+00:00","dateModified":"2025-07-03T18:49:40+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=454#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=454"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/?p=454#primaryimage","url":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png","contentUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2025\/07\/two_file_cabinets_once_file_cabinet_is-01.png","width":2350,"height":1280},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=454#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Move PostgreSQL Data Directory and Config Files"}]},{"@type":"WebSite","@id":"http:\/\/77interactive.com\/#website","url":"http:\/\/77interactive.com\/","name":"77 Interactive","description":"Rudy&#039;s Code snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/77interactive.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9","name":"Rudy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","caption":"Rudy"},"url":"http:\/\/77interactive.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=454"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/media\/455"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}