What you'll learn
- How to Install PostgreSQL with Ansible in RedHat-like systems?
- Ansible Install PostgreSQL in Debian-like systems
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
- Related Articles
How to Install PostgreSQL with Ansible in RedHat-like systems?
I’m going to show you a live Playbook and some simple Ansible code.
Ansible Install PostgreSQL in Debian-like systems
- Install server, client, utils => `ansible.builtin.apt`
- Initialize db => `ansible.builtin.stat`, `ansible.builtin.shell`
- Start and Enable at boot => `ansible.builtin.service`
In order to install PostgreSQL on a Debian-like system, you need to perform three steps.
The first step is to install the packages to perform server, client, and utils. You are going to use the `ansible.builtin.apt` Ansible module.
These include the distribution-related binaries, libraries, and documentation for your Debian-like system, Ubuntu as well.
The second step is to initialize the PostgreSQL database. There is a command-line utility that you could execute using the Ansible `ansible.builtin.shell` module. The effective command executed is `/usr/lib/postgresql/14/bin/initdb -D /var/lib/postgresql/14/main` (suppose you are using version 14).
This code is executed only if needed, the conditional check was performed by the `ansible.builtin.stat` module.
The third step is to Start and Enable the PostgreSQL service at boot using the `ansible.builtin.service` Ansible module.
Links
- [`ansible.builtin.apt`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/apt_module.html)
- [`ansible.builtin.stat`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/stat_module.html)
- [`ansible.builtin.shell`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html)
- [`ansible.builtin.service`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html)
## Playbook
Let’s jump into a real-life playbook to install PostgreSQL in Debian-like systems with Ansible.
I’m going to show you how to install the PostgreSQL server, client utilities, and the Python libraries to manage the DBMS.
The second step is to perform the initial PostgreSQL database initialization, only if data were not present.
After these steps, you’re able to start the service and enable it at boot time.
code
```yaml
---
- name: postgresql Playbook
hosts: all
become: true
vars:
postgresql_version: "14"
postgresql_bin_path: "/usr/lib/postgresql/{{ postgresql_version }}/bin"
postgresql_data_dir: "/var/lib/postgresql/{{ postgresql_version }}/main"
ansible_ssh_pipelining: true
tasks:
- name: Install packages
ansible.builtin.apt:
name:
- postgresql
- postgresql-contrib
- libpq-dev
- python3-psycopg2
state: present
- name: Check if PostgreSQL is initialized
ansible.builtin.stat:
path: "{{ postgresql_data_dir }}/pg_hba.conf"
register: postgres_data
- name: Empty data dir
ansible.builtin.file:
path: "{{ postgresql_data_dir }}"
state: absent
when: not postgres_data.stat.exists
- name: Initialize