How to Install PostgreSQL with Ansible in RedHat-like systems?
I’m going to show you a live Playbook and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible Install PostgreSQL in RedHat-like systems
- Install server, client, utils =>
ansible.builtin.yum
- Initialize db =>
ansible.builtin.stat
,ansible.builtin.shell
- Start and Enable at boot =>
ansible.builtin.service
In order to install PostgreSQL on a RedHat-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.yum
Ansible module.
These include the distribution-related binaries, libraries, and documentation for your RedHat-like system.
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 postgresql-setup initdb
.
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
Playbook
Let’s jump into a real-life playbook to install PostgreSQL in RedHat-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
---
- name: postgresql Playbook
hosts: all
become: true
tasks:
- name: Install packages
ansible.builtin.yum:
name:
- postgresql
- postgresql-server
- postgresql-contrib
- postgresql-libs
- python3-psycopg2
state: present
- name: Check if PostgreSQL is initialized
ansible.builtin.stat:
path: "/var/lib/pgsql/data/pg_hba.conf"
register: postgres_data
- name: Initialize PostgreSQL
ansible.builtin.shell: "postgresql-setup initdb"
when: not postgres_data.stat.exists
- name: Start and enable service
ansible.builtin.service:
name: postgresql
state: started
enabled: true
execution
$ ansible-playbook -i virtualmachines/demo/inventory postgresql/install.yml
PLAY [postgresql Playbook] ************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [demo.example.com]
TASK [Install packages] ***********************************************************************************
changed: [demo.example.com]
TASK [Check if PostgreSQL is initialized] *****************************************************************
ok: [demo.example.com]
TASK [Initialize PostgreSQL] ******************************************************************************
changed: [demo.example.com]
TASK [Start and enable service] ***************************************************************************
changed: [demo.example.com]
PLAY RECAP ************************************************************************************************
demo.example.com : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
idempotency
$ ansible-playbook -i virtualmachines/demo/inventory postgresql/install.yml
PLAY [postgresql Playbook] ************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [demo.example.com]
TASK [Install packages] ***********************************************************************************
ok: [demo.example.com]
TASK [Check if PostgreSQL is initialized] *****************************************************************
ok: [demo.example.com]
TASK [Initialize PostgreSQL] ******************************************************************************
skipping: [demo.example.com]
TASK [Start and enable service] ***************************************************************************
ok: [demo.example.com]
PLAY RECAP ************************************************************************************************
demo.example.com : ok=4 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
before execution
$ ssh [email protected]
Last login: Mon Jun 6 08:34:12 2022 from 192.168.43.5
[devops@demo ~]$ sudo su
[root@demo devops]# dnf list postgresql
Updating Subscription Management repositories.
Last metadata expiration check: 0:05:11 ago on Mon 06 Jun 2022 08:37:04 AM UTC.
Available Packages
postgresql.x86_64 10.21-2.module+el8.6.0+15342+53518fac rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# dnf list postgresql-server
Updating Subscription Management repositories.
Last metadata expiration check: 0:05:45 ago on Mon 06 Jun 2022 08:37:04 AM UTC.
Available Packages
postgresql-server.x86_64 10.21-2.module+el8.6.0+15342+53518fac rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# ls -al /var/lib/pgsql/data/pg_hba.conf
ls: cannot access '/var/lib/pgsql/data/pg_hba.conf': No such file or directory
[root@demo devops]# systemctl status postgresql
Unit postgresql.service could not be found.
[root@demo devops]#
after execution
$ ssh [email protected]
[devops@demo ~]$ sudo su
[root@demo devops]# dnf list postgresql
Updating Subscription Management repositories.
Last metadata expiration check: 0:12:37 ago on Mon 06 Jun 2022 08:37:04 AM UTC.
Installed Packages
postgresql.x86_64 10.21-2.module+el8.6.0+15342+53518fac @rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# dnf list postgresql-server
Updating Subscription Management repositories.
Last metadata expiration check: 0:12:59 ago on Mon 06 Jun 2022 08:37:04 AM UTC.
Installed Packages
postgresql-server.x86_64 10.21-2.module+el8.6.0+15342+53518fac @rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# ls -al /var/lib/pgsql/data/pg_hba.conf
-rw-------. 1 postgres postgres 4269 Jun 6 08:47 /var/lib/pgsql/data/pg_hba.conf
[root@demo devops]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-06-06 08:47:31 UTC; 3min 11s ago
Process: 8071 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCES>
Main PID: 8077 (postmaster)
Tasks: 8 (limit: 4952)
Memory: 15.8M
CGroup: /system.slice/postgresql.service
├─8077 /usr/bin/postmaster -D /var/lib/pgsql/data
├─8082 postgres: logger process
├─8085 postgres: checkpointer process
├─8086 postgres: writer process
├─8087 postgres: wal writer process
├─8088 postgres: autovacuum launcher process
├─8089 postgres: stats collector process
└─8090 postgres: bgworker: logical replication launcher
Jun 06 08:47:30 demo.example.com systemd[1]: Starting PostgreSQL database server...
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.064 UTC [8077] LOG: listening on I>
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.065 UTC [8077] LOG: could not bind>
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.065 UTC [8077] HINT: Is another po>
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.084 UTC [8077] LOG: listening on U>
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.089 UTC [8077] LOG: listening on U>
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.172 UTC [8077] LOG: redirecting lo>
Jun 06 08:47:31 demo.example.com postmaster[8077]: 2022-06-06 08:47:31.172 UTC [8077] HINT: Future log ou>
Jun 06 08:47:31 demo.example.com systemd[1]: Started PostgreSQL database server.
Conclusion
Now you know how to Install PostgreSQL in RedHat-like Linux systems. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate