Deploy PostgreSQL using Ansible-playbook on Ubuntu 22.04 LTS


PostgreSQL, often referred to as “Postgres,” is a powerful open-source relational database management system (RDBMS). It is known for its robustness, extensibility, and compliance with SQL standards. PostgreSQL is designed to handle various types of workloads, from small single-machine applications to large enterprise and web-based systems.


  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in Linux commands.
  • Internet connectivity.
  • Ansible should be installed.

We can use the given link to install Ansible on Ubuntu 22.04 LTS Linux machine.

In this post, We will show you how to deploy PostgreSQL using Ansible-play on ubuntu 22.04 LTS Linux machine.

Step 1: Create an Ansible playbook

To deploy PostgreSQL using Ansible on Ubuntu 22.04 LTS, you can create an Ansible playbook. Below is a simple example playbook that installs PostgreSQL and creates a database and a user. Make sure you have Ansible installed on your local machine before running the playbook.

Create an inventory file (e.g., inventory.ini) with the target server’s IP address:

your_server_ip ansible_ssh_user=your_ssh_user

Replace your_server_ip and your_ssh_user with the appropriate values.

To create ansible playbook.

sudo nano deploy_postgresql.yml

Paste the following ansible tasks.

- name: Deploy PostgreSQL on Ubuntu 22.04
  hosts: database
  become: true
    - name: Update apt package cache
        update_cache: yes

    - name: Install PostgreSQL and dependencies
          - postgresql
          - postgresql-contrib
          - libpq-dev
        state: present

    - name: Create a PostgreSQL database
        name: your_database_name
        state: present

    - name: Create a PostgreSQL user
        name: your_username
        password: your_password
        encrypted: yes
        db: your_database_name
        priv: "*.*:ALL"
        state: present

Replace your_database_name, your_username, and your_password with the desired values.

Step 2: Run the playbook

We need to execute the given command in order to deploy PostgreSQL databse server.

ansible-playbook -i inventory.ini deploy_postgresql.yml

This playbook performs the following tasks:

  • Updates the package cache.
  • Installs PostgreSQL and its dependencies.
  • Creates a PostgreSQL database.
  • Creates a PostgreSQL user.

Adjust the playbook according to your specific requirements. For example, you may want to secure the PostgreSQL installation by modifying the PostgreSQL configuration file (postgresql.conf) and the pg_hba.conf file.


We have successfully deployed PostgreSQL database server using Ansible playbook on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.


Deploy PostgreSQL using Ansible-playbook on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top