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 building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

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

I’ve been building

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:
⭐ Star it on GitHub:

Let’s make it even better together.

git-lrc
*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Micro AI Code Reviews That Run on Commit




AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.

Top comments (0)