AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service — Video Tutorial
How to automate the installation of PostgreSQL on Debian-like systems: installing the necessary packages and dependency, initializing the configuration.
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
Full Tutorial Content
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 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
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 5 min
- Category: installation
Read the full written article: Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service
Related Video Tutorials
- Install Docker in Debian-like systems - Ansible module apt_key, apt_repository and apt — How to automate the installation of the docker-ce engine in Ubuntu 20.04 LTS x86_64 (or amd64) using Ansible Playbook.
- Install Google Chrome in Debian like systems - Ansible module apt_key, apt_repository and apt — How to install the latest Google Chrome Stable on a Debian-like workstation (Debian, Ubuntu, Linux Mint, MX Linux, Deepin, AntiX, PureOS, Kali Linux, Parrot.
- Install Microsoft Edge on Debian with Ansible — Learn how to install Microsoft Edge on Debian using Ansible. Follow our guide for a smooth setup process, including repository and key additions.
- Install Spotify snap in Debian-like systems - Ansible module snap — How to automate the installation of Spotify snap system-wide in Debian-like systems using Ansible module snap.
- Install Zoom flatpak in Debian-like systems - Ansible module flatpak — How to automate the installation of Zoom flatpak system-wide in Debian-like systems using Ansible module flatpak.
- Ansible apt Module: Install Packages on Ubuntu/Debian (Examples) — How to install, remove, and update packages on Ubuntu, Debian, and Linux Mint using Ansible's apt module.