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.

Watch Video

Watch "Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service" on YouTube

What You'll Learn

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

Read the full written article: Install PostgreSQL in Debian-like systems - Ansible modules apt, stat, shell, service

Topics Covered

Related Video Tutorials