Automation

How to Use Ansible to Install and Configure Postgres 14 on Ubuntu 20.04

Pinterest LinkedIn Tumblr

In this guide we are going to install and configure Postgresql 14 in Ubuntu 20.04 using Ansible.

Postgresql is an open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance. Postgres, is a free and open-source relational database management system emphasizing extensibility and SQL compliance.

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Check also:

Prerequisites

To follow along, ensure that you have the following:

  • An up to date Ubuntu 20.04 server
  • SSH access to the server with user having sudo access
  • Latest version of Ansible installed locally

Table of Content

  1. Ensuring that the server is up to date
  2. Installing some packages necessary for the set up
  3. Setting up Postgres 14 repo
  4. Installing Postgres 14
  5. Configuring postgres 14
  6. Creating a super user in the database server
  7. Setting up ansible handlers
  8. Creating hosts file
  9. The whole playbook
  10. Runing the ansible playbook

1. Ensuring that the server is up to date

Before proceeding, ensure that the server is up to date using these tasks. First we refresh the repos then we upgrade all packages.

- name: Update apt repo and cache on Ubuntu box
  apt:
    update_cache: yes
    force_apt_get: yes
    cache_valid_time: 3600

- name: Upgrade all packages on servers
  apt:
    upgrade: dist
    force_apt_get: yes

2. Installing necessary packages

Next, let us install some common packages that we will need in our playbook.

- name: Install required packages
  apt:
    name:
      - wget
      - python3-psycopg2
      - acl
    state: latest

3. Setting up Postgres 14 Repo

Next, let us set up the repo for Postgres 14. The default Ubuntu 20.04 repos contains an outdated version of Postgres. Let us set up these repos that will allow us to install Postgres 14.

- name: Set up Postgres 14 repo
  shell: |
    echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  args:
    warn: no

4. Installing Postgres 14 in Ubuntu

Now that we have set up the repos, we can install Postgres. Use this task to achieve that

- name: Install postgresql
  apt:
    name: postgresql-14
    update_cache: yes
  notify:
    - Enable Postgresql

In the above, we are installing postgres but doing a cache update (equivalent to apt-get update) before that.

5. Configuring Postgres 14

With Postresql install, we would want to do some more configurations.

Let us update the listen address so we can access the service from a remote machine and then change identification to trust:

- name: Ensure PostgreSQL is listening on *
  lineinfile:
    dest: /etc/postgresql/14/main/postgresql.conf
    regexp: '^listen_addresses\s*='
    line: "listen_addresses='*'"
    state: present
  notify: Restart Postgresql

- name: Add new configuration to "pg_hba.conf"
  blockinfile:
    dest: /etc/postgresql/14/main/pg_hba.conf
    block: |
      host    all             all             0.0.0.0/0                md5
      host    all             all             ::/0                     md5
  notify: Restart Postgresql

- name: Change peer identification to trust
  shell: /bin/sed -i '/^local/s/peer/trust/' /etc/postgresql/14/main/pg_hba.conf
  args:
    warn: no
  notify: Restart Postgresql

6. Creating a Postgres Super User

Let us also create a user that will allow us to manage database operations. This is like a root user for our DB:

- name: Create a Superuser PostgreSQL database user
  become: yes
  become_user: postgres
  postgresql_user:
    name: '{{ postgres_root_user }}'
    password: '{{ postgres_root_pass }}'
    role_attr_flags: CREATEDB,SUPERUSER,CREATEROLE
    encrypted: yes
    state: present

7. Set up handlers

Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something. 

Let us set up handlers for out Postgres installation and set up:

- name: Restart Postgresql
  systemd:
    name: postgresql
    state: restarted

- name: Start Postgresql
  systemd:
    name: postgresql
    state: started

- name: Stop Postgresql
  systemd:
    name: postgresql
    state: stopped

- name: Enable Postgresql
  systemd:
    name: postgresql
    enabled: yes

8. Creating the hosts file

Ansible will execute the tasks against some inventory. The server inventory will be added as a hosts.yaml file that defines how the servers will be reached. This is my hosts file:

all:
  hosts
    ubuntusrv:
      ansible_ssh_host: 10.2.11.9
      ansible_ssh_user: ubuntu

9. The whole playbook

This is the whole playbook. Save it as postgres.yaml

---
- name: Install and set up Postgres 14 in Ubuntu 20.04
  hosts: ubuntusrv
  become: yes
  gather_facts: False
  vars:
    postgres_root_user: root
    postgres_root_pass: GQt5MTyVPuf9vsVWoWDT9YCn
    allow_world_readable_tmpfiles: true
  tasks:
    - name: Update apt repo and cache on all Ubuntu boxes
      apt:
        update_cache: yes
        force_apt_get: yes
        cache_valid_time: 3600

    - name: Upgrade all packages on servers
      apt:
        upgrade: dist
        force_apt_get: yes

    - name: Install required packages
      apt:
        name:
          - wget
          - python3-psycopg2
          - acl
        state: latest

    - name: Set up Postgres 14 repo
      shell: |
        echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
        wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
      args:
        warn: no

    - name: Install postgresql
      apt:
        name: postgresql-14
        update_cache: yes
      notify:
        - Enable Postgresql

    - name: Ensure PostgreSQL is listening on *
      lineinfile:
        dest: /etc/postgresql/14/main/postgresql.conf
        regexp: '^listen_addresses\s*='
        line: "listen_addresses='*'"
        state: present
      notify: Restart Postgresql

    - name: Add new configuration to "pg_hba.conf"
      blockinfile:
        dest: /etc/postgresql/14/main/pg_hba.conf
        block: |
          host    all             all             0.0.0.0/0                md5
          host    all             all             ::/0                     md5
      notify: Restart Postgresql

    - name: Change peer identification to trust
      shell: /bin/sed -i '/^local/s/peer/trust/' /etc/postgresql/14/main/pg_hba.conf
      args:
        warn: no
      notify: Restart Postgresql

    - name: Create a Superuser PostgreSQL database user
      become: yes
      become_user: postgres
      postgresql_user:
        name: '{{ postgres_root_user }}'
        password: '{{ postgres_root_pass }}'
        role_attr_flags: CREATEDB,SUPERUSER,CREATEROLE
        encrypted: yes
        state: present

  handlers:
    - name: Restart Postgresql
      systemd:
        name: postgresql
        state: restarted

    - name: Start Postgresql
      systemd:
        name: postgresql
        state: started

    - name: Stop Postgresql
      systemd:
        name: postgresql
        state: stopped

    - name: Enable Postgresql
      systemd:
        name: postgresql
        enabled: yes

10. Running the ansible playbook

To run the ansible playbook, you need ansible installed locally. You can install ansible using OS package manager or if you have python pip you can use it:

sudo pip install ansible

You also need to have ssh access to the server. Ensure that you set up connection to the server. I am using ssh key authentication and ssh works for me using this command:

ssh [email protected]

Now you can run the playbook using this command:

ansible-playbook -i hosts.yaml postgres.yaml -vv

Once the playbook finish executing you can access Postgres installed on the server.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

2 Comments

  1. Pingback: How to Install and Configure Postgres 14 Ubuntu 20.04

Write A Comment