{"id":266,"date":"2023-09-19T21:52:53","date_gmt":"2023-09-19T21:52:53","guid":{"rendered":"http:\/\/77interactive.com\/?p=266"},"modified":"2023-09-25T22:04:46","modified_gmt":"2023-09-25T22:04:46","slug":"read-postgres-configuration-file","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=266","title":{"rendered":"Read Postgres Configuration File"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Read and Display Postgres configuration information<\/h2>\n\n\n\n<p>The following script reads the Postgresql configuration file and displays key settings. Results are sent to the console and an outfile. You can also specify the postresql.conf file location via parameter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\nimport sys\nimport subprocess\nimport json\n\ndef get_node_name():\n    # Try to get the cluster name using the `pg_lsclusters` command\n    try:\n        output = subprocess.check_output(&#91;'pg_lsclusters', '-h'], universal_newlines=True)\n        # Extract the cluster name (which is typically \"main\" in default installations)\n        cluster_name = output.splitlines()&#91;1].split()&#91;-1]\n        return cluster_name\n    except:\n        return \"Unknown\"\n\ndef read_postgres_conf(file_path):\n    # Define the settings we're interested in\n    settings = &#91;\r\n        \"listen_addresses\", \"port\", \"max_connections\", \"shared_buffers\", \r\n        \"work_mem\", \"maintenance_work_mem\", \"wal_level\", \"max_wal_size\", \"min_wal_size\"\r\n        \"archive_mode\", \"log_statement\", \"log_duration\", \"hot_standby\",\"synchronous_commit\",\r\n        \"autovacuum\", \"log_timezone\", \"timezone\", \"ssl\", \"Password_encryption\",\r\n        \"effective_cache_size\",\"random_page_cost\", \"seq_page_cost\"\r\n    ]\n    \n    # Initialize a dictionary with \"Not Set\" as default for all settings and empty comments\n    values = {setting: {\"value\": \"Not Set\", \"comment\": \"\"} for setting in settings}\n\n    try:\n        with open(file_path, 'r') as file:\n            for line in file:\n                # Remove leading and trailing whitespaces\n                line = line.strip()\n\n                # Skip empty lines\n                if not line:\n                    continue\n\n                for setting in settings:\n                    if line.startswith(setting):\n                        # Extract the value and possible comment using a regular expression\n                        match = re.search(r'^\\s*{}\\s*=\\s*(&#91;^#\\s]*)\\s*(?:#\\s*(.*))?'.format(setting), line)\n                        if match:\n                            values&#91;setting]&#91;\"value\"] = match.group(1).strip(\"'\\\"\")\n                            if match.group(2):\n                                values&#91;setting]&#91;\"comment\"] = match.group(2).strip()\n    except FileNotFoundError:\n        print(f\"Error: The file '{file_path}' was not found.\")\n        return\n\n    return values\n\nif __name__ == \"__main__\":\n    default_path = \"\/etc\/postgresql\/15\/main\/postgresql.conf\"\n    output_file = \"\/tmp\/linuxverification.json\"\n    \n    if len(sys.argv) == 2 and sys.argv&#91;1] in &#91;\"-h\", \"--help\"]:\n        print(\"Usage: python postgresverification.py &#91;path_to_postgresql.conf]\")\n        print(f\"Default path if not specified: {default_path}\")\n        print(\"Example: python postgresverification.py \/path\/to\/your\/postgresql.conf\")\n    else:\n        file_path = sys.argv&#91;1] if len(sys.argv) > 1 else default_path\n        values = read_postgres_conf(file_path)\n\n        if values:\n            # Get the node name\n            node_name = get_node_name()\n            output_data = {\n                \"Postgres Node Name\": node_name,\n                \"Settings\": values\n            }\n\n           # Output to the console\n            print(f\"Postgres Node Name: {node_name}\\n\")\n            print(\"{:&lt;20} | {:&lt;15} | {}\".format(\"Setting\", \"Current Value\", \"Comment\"))\n            print(\"-\" * 65)\n            for setting, data in values.items():\n                print(\"{:&lt;20} | {:&lt;15} | {}\".format(setting, data&#91;\"value\"], data&#91;\"comment\"]))\n       \n\n            # Append the results to the file in JSON format\n            with open(output_file, 'a') as json_file:\n                json_file.write(json.dumps(output_data, indent=4))\n                json_file.write('\\n')  # Separate entries by a newline\n\n            print(f\"Results appended to: {output_file}\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Read and Display Postgres configuration information The following script reads the Postgresql configuration file and displays key settings. Results are sent to the console and an outfile. You can also specify the postresql.conf file location via parameter.<\/p>\n","protected":false},"author":1,"featured_media":268,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,2],"tags":[17,18,3],"class_list":["post-266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgres","category-python","tag-postgres","tag-postgresql","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Read Postgres Configuration File - 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=266\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Read Postgres Configuration File - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"Read and Display Postgres configuration information The following script reads the Postgresql configuration file and displays key settings. Results are sent to the console and an outfile. You can also specify the postresql.conf file location via parameter.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=266\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-19T21:52:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-25T22:04:46+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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=266#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=266\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Read Postgres Configuration File\",\"datePublished\":\"2023-09-19T21:52:53+00:00\",\"dateModified\":\"2023-09-25T22:04:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=266\"},\"wordCount\":42,\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=266#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg\",\"keywords\":[\"postgres\",\"postgresql\",\"python\"],\"articleSection\":[\"postgres\",\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=266\",\"url\":\"http:\/\/77interactive.com\/?p=266\",\"name\":\"Read Postgres Configuration File - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=266#primaryimage\"},\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=266#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg\",\"datePublished\":\"2023-09-19T21:52:53+00:00\",\"dateModified\":\"2023-09-25T22:04:46+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=266#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=266\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/?p=266#primaryimage\",\"url\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg\",\"contentUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg\",\"width\":512,\"height\":512,\"caption\":\"Elephant wearing a VR helmet\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=266#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Read Postgres Configuration File\"}]},{\"@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":"Read Postgres Configuration File - 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=266","og_locale":"en_US","og_type":"article","og_title":"Read Postgres Configuration File - 77 Interactive","og_description":"Read and Display Postgres configuration information The following script reads the Postgresql configuration file and displays key settings. Results are sent to the console and an outfile. You can also specify the postresql.conf file location via parameter.","og_url":"http:\/\/77interactive.com\/?p=266","og_site_name":"77 Interactive","article_published_time":"2023-09-19T21:52:53+00:00","article_modified_time":"2023-09-25T22:04:46+00:00","og_image":[{"width":512,"height":512,"url":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg","type":"image\/jpeg"}],"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=266#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=266"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Read Postgres Configuration File","datePublished":"2023-09-19T21:52:53+00:00","dateModified":"2023-09-25T22:04:46+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=266"},"wordCount":42,"image":{"@id":"http:\/\/77interactive.com\/?p=266#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg","keywords":["postgres","postgresql","python"],"articleSection":["postgres","Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=266","url":"http:\/\/77interactive.com\/?p=266","name":"Read Postgres Configuration File - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"primaryImageOfPage":{"@id":"http:\/\/77interactive.com\/?p=266#primaryimage"},"image":{"@id":"http:\/\/77interactive.com\/?p=266#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg","datePublished":"2023-09-19T21:52:53+00:00","dateModified":"2023-09-25T22:04:46+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=266#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=266"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/?p=266#primaryimage","url":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg","contentUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/09\/elephant-vr.jpg","width":512,"height":512,"caption":"Elephant wearing a VR helmet"},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=266#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Read Postgres Configuration File"}]},{"@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\/266","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=266"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/media\/268"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}