DEV Community

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

Posted on • Edited on

Installing PostgreSQL with Ansible (The Simple Way)

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.

When you're automating infrastructure, a clean fol
der 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
  • Aneasy way to install and test PostgreSQL on any server

FreeDevTools

I’ve been building FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools

Let’s make it even better together.

Top comments (0)