DEV Community

Cover image for Installing PostgreSQL with Ansible (The Simple Way)
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Installing PostgreSQL with Ansible (The Simple Way)

Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.


When you're automating infrastructure, a clean folder structure and roles make your Ansible setup scalable.

Here's how to install PostgreSQL the right way—with a proper Ansible layout and custom role.

Folder Structure

Start by generating a role using:

ansible-galaxy init roles/db --offline
Enter fullscreen mode Exit fullscreen mode

This gives you a reusable structure inside your Ansible project:

ansible/
├── ansible.cfg
├── hosts.ini
├── install-db.yml
├── install_ansible.sh
├── requirements.yml
├── README.md
├── roles/
│   └── db/
│       ├── tasks/
│       │   └── main.yml          # Actual installation steps
│       ├── defaults/             # Default vars if needed
│       ├── handlers/             # For service restart triggers
│       ├── meta/                 # Role metadata
│       ├── templates/            # Jinja templates (unused here)
│       ├── files/                # Static files (unused here)
│       ├── vars/                 # Hardcoded vars (unused here)
│       ├── tests/                # Smoke tests for the role
│       └── README.md
Enter fullscreen mode Exit fullscreen mode

install-db.yml

This is your playbook entrypoint:

- name: Install PostgreSQL
  hosts: all
  become: true
  roles:
    - db
Enter fullscreen mode Exit fullscreen mode

Run it using:

ansible-playbook -i hosts.ini install-db.yml
Enter fullscreen mode Exit fullscreen mode

roles/db/tasks/main.yml

Here’s the PostgreSQL installation broken down into Ansible tasks:

- name: Update apt cache
  apt:
    update_cache: yes

- name: Install postgresql-common
  apt:
    name: postgresql-common
    state: present

- name: Run PGDG setup script
  command: /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
  args:
    creates: /etc/apt/sources.list.d/pgdg.list

- name: Update apt cache after PGDG script
  apt:
    update_cache: yes

- name: Install PostgreSQL and contrib
  apt:
    name:
      - postgresql
      - postgresql-contrib
    state: present
Enter fullscreen mode Exit fullscreen mode

Other Files (Optional)

  • ansible.cfg: Set your default inventory and SSH options.
  • hosts.ini: Define target servers like so:
  [db]
  node1 ansible_host=192.168.1.10 ansible_user=ubuntu
Enter fullscreen mode Exit fullscreen mode
  • install_ansible.sh: If you want a script to bootstrap Ansible on your local.
  • requirements.yml: If you plan to use Galaxy roles (not used here).

Testing (optional)

Inside roles/db/tests/test.yml, you could write a basic test to ensure PostgreSQL is installed.

- name: Test PostgreSQL installation
  hosts: all
  tasks:
    - name: Check if postgres is installed
      command: psql --version
      register: pg_version
      changed_when: false
      failed_when: pg_version.rc != 0

    - debug:
        var: pg_version.stdout
Enter fullscreen mode Exit fullscreen mode

Done

Now you've got:

  • A structured Ansible project
  • A reusable role (db) for PostgreSQL
  • An easy way to install and test PostgreSQL on any server

LiveAPI helps you get all your backend APIs documented in a few minutes.

With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

LiveAPI Demo

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.

Top comments (0)