{"id":354,"date":"2024-05-16T16:16:52","date_gmt":"2024-05-16T16:16:52","guid":{"rendered":"http:\/\/77interactive.com\/?p=354"},"modified":"2024-05-20T17:04:19","modified_gmt":"2024-05-20T17:04:19","slug":"install-single-instance-of-postgresql-with-ansible","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=354","title":{"rendered":"Install single instance of PostgreSQL with Ansible"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>The following Ansible script installs prerequisites. This includes packages for Python, etc. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- name: Update all packages and install prereqs on AWS Linux\n  hosts: all\n  become: yes  # Required for installing system packages\n\n  tasks:\n    - name: Update all current packages\n      ansible.builtin.yum:\n        name: '*'\n        state: latest\n\n    - name: Ensure dnf-plugins-core is installed (for dnf config-manager)\n      ansible.builtin.dnf:\n        name: dnf-plugins-core\n        state: present\n\n    - name: Enable the CRB (CodeReady Builder) repository on Rocky Linux\n      ansible.builtin.shell: \n        cmd: dnf config-manager --set-enabled crb\n      when: ansible_distribution == \"Rocky\" and ansible_distribution_major_version == \"9\"\n\n    - name: Install pip, python3-devel, nano\n      ansible.builtin.yum:\n        name:\n          - nano\n          - python3-pip  # Ensures pip is installed\n          - python3-devel  # Python development files necessary for psycopg2\n          - git\n          - htop\n          - python3-dnf\n          - python3-libselinux\n        state: present\n\n    - name: Install system packages for psycopg2\n      ansible.builtin.yum:\n        name:\n          - perl\n          - perl-App-cpanminus\n          - perl-devel\n          - postgresql-devel  # PostgreSQL development files necessary for psycopg2\n        state: present\n    - name: Install psycopg2 using pip\n      ansible.builtin.pip:\n        name: psycopg2-binary\n        state: present\n...\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PostgreSQL<\/h2>\n\n\n\n<p>The following Ansible script installs a single instance of PostgreSQL via ansible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- name: Install and configure PostgreSQL 16 on Rocky Linux 9.1\n  hosts: all\n  become: yes\n\n  vars:\n    postgres_version: 16\n    db_name: \"postgres\"\n    user1:\n      name: \"dbaadmin\"\n      password: \"dbaadmin1234\"\n      role: \"superuser\"\n    user2:\n      name: \"repluser\"\n      password: \"repluser1234\"\n      role: \"replication\"\n\n  tasks:\n    - name: Ensure all packages are up to date\n      yum:\n        name: \"*\"\n        state: latest\n\n    - name: Add PostgreSQL repository\n      yum_repository:\n        name: \"postgresql\"\n        description: \"PostgreSQL Repository\"\n        baseurl: \"https:\/\/download.postgresql.org\/pub\/repos\/yum\/{{ postgres_version }}\/redhat\/rhel-9-x86_64\"\n        gpgkey: \"https:\/\/download.postgresql.org\/pub\/repos\/yum\/keys\/PGDG-RPM-GPG-KEY-RHEL\"\n        enabled: yes\n        state: present\n\n    - name: Install PostgreSQL and contrib package\n      yum:\n        name: \"postgresql{{ postgres_version }}, postgresql{{ postgres_version }}-server, postgresql{{ postgres_version }}-contrib\"\n        state: present\n\n    - name: Initialize the PostgreSQL database\n      command: \"\/usr\/pgsql-{{ postgres_version }}\/bin\/postgresql-{{ postgres_version }}-setup initdb\"\n      args:\n        creates: \"\/var\/lib\/pgsql\/{{ postgres_version }}\/data\/PG_VERSION\"\n\n    - name: Start and enable PostgreSQL service\n      systemd:\n        name: \"postgresql-{{ postgres_version }}\"\n        enabled: yes\n        state: started\n\n    - name: Install dependencies for psycopg2\n      yum:\n        name:\n          - python3\n          - python3-devel\n          - gcc\n          - postgresql{{ postgres_version }}-devel\n        state: present\n\n    - name: Install psycopg2 using pip\n      pip:\n        name: psycopg2\n        state: present\n\n    - name: Set up PostgreSQL user accounts\n      postgresql_user:\n        name: \"{{ item.name }}\"\n        password: \"{{ item.password }}\"\n        role_attr_flags: \"{{ item.role }}\"\n      loop:\n        - \"{{ user1 }}\"\n        - \"{{ user2 }}\"\n\n    - name: Allow replication connections\n      lineinfile:\n        path: \"\/var\/lib\/pgsql\/{{ postgres_version }}\/data\/pg_hba.conf\"\n        line: \"host replication {{ user2.name }} 0.0.0.0\/0 md5\"\n        create: yes\n\n    - name: Configure PostgreSQL for replication and enable pg_stat_statements\n      blockinfile:\n        path: \"\/var\/lib\/pgsql\/{{ postgres_version }}\/data\/postgresql.conf\"\n        block: |\n          listen_addresses = '*'\n          wal_level = replica\n          max_wal_senders = 10\n          archive_mode = on\n          archive_command = 'cp %p \/var\/lib\/pgsql\/{{ postgres_version }}\/data\/archive\/%f'\n          shared_preload_libraries = 'pg_stat_statements'\n\n    - name: Restart PostgreSQL to apply changes\n      systemd:\n        name: \"postgresql-{{ postgres_version }}\"\n        state: restarted\n\n    - name: Enable pg_stat_statements extension\n      postgresql_ext:\n        name: pg_stat_statements\n        db: \"{{ db_name }}\"\n        state: present\n...<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites The following Ansible script installs prerequisites. This includes packages for Python, etc. PostgreSQL The following Ansible script installs a single instance of PostgreSQL via ansible.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[50,18],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-ansible","tag-postgresql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Install single instance of PostgreSQL with Ansible - 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=354\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Install single instance of PostgreSQL with Ansible - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"Prerequisites The following Ansible script installs prerequisites. This includes packages for Python, etc. PostgreSQL The following Ansible script installs a single instance of PostgreSQL via ansible.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=354\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-16T16:16:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-20T17:04:19+00:00\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=354#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=354\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Install single instance of PostgreSQL with Ansible\",\"datePublished\":\"2024-05-16T16:16:52+00:00\",\"dateModified\":\"2024-05-20T17:04:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=354\"},\"wordCount\":33,\"keywords\":[\"ansible\",\"postgresql\"],\"articleSection\":[\"PostgreSQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=354\",\"url\":\"http:\/\/77interactive.com\/?p=354\",\"name\":\"Install single instance of PostgreSQL with Ansible - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"datePublished\":\"2024-05-16T16:16:52+00:00\",\"dateModified\":\"2024-05-20T17:04:19+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=354#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=354\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=354#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Install single instance of PostgreSQL with Ansible\"}]},{\"@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":"Install single instance of PostgreSQL with Ansible - 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=354","og_locale":"en_US","og_type":"article","og_title":"Install single instance of PostgreSQL with Ansible - 77 Interactive","og_description":"Prerequisites The following Ansible script installs prerequisites. This includes packages for Python, etc. PostgreSQL The following Ansible script installs a single instance of PostgreSQL via ansible.","og_url":"http:\/\/77interactive.com\/?p=354","og_site_name":"77 Interactive","article_published_time":"2024-05-16T16:16:52+00:00","article_modified_time":"2024-05-20T17:04:19+00:00","author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=354#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=354"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Install single instance of PostgreSQL with Ansible","datePublished":"2024-05-16T16:16:52+00:00","dateModified":"2024-05-20T17:04:19+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=354"},"wordCount":33,"keywords":["ansible","postgresql"],"articleSection":["PostgreSQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=354","url":"http:\/\/77interactive.com\/?p=354","name":"Install single instance of PostgreSQL with Ansible - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"datePublished":"2024-05-16T16:16:52+00:00","dateModified":"2024-05-20T17:04:19+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=354#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=354"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=354#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Install single instance of PostgreSQL with Ansible"}]},{"@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\/354","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=354"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}