DEV Community

Cover image for PostgreSQL 16 Installation with Ansible (and Custom Configs)
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on • Edited on

PostgreSQL 16 Installation with Ansible (and Custom Configs)

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.

---

Need to automate PostgreSQL 16 installation and make sure it ships with your exact postgresql.conf and pg_hba.conf? Here’s how to set it up like a dev who’s tired of ssh and vim.

Directory Structure

We’re working with a typical Ansible role structure. Your roles/db folder looks like this:

roles/db/
├── tasks/
│   ├── main.yml
│   └── install_postgres.yml
├── templates/
│   ├── pg_hba.conf.j2
│   └── postgresql.conf.j2
Enter fullscreen mode Exit fullscreen mode

Inventory

hosts.ini example:

[db]
my-postgres-host ansible_host=192.168.0.10 ansible_user=ubuntu
Enter fullscreen mode Exit fullscreen mode

db.yml (Entry point)

- name: Setup PostgreSQL
  hosts: db
  become: yes
  roles:
    - db
Enter fullscreen mode Exit fullscreen mode

roles/db/tasks/main.yml

---
- include_tasks: install_postgres.yml
Enter fullscreen mode Exit fullscreen mode

roles/db/tasks/install_postgres.yml

---
- name: Update apt cache
  apt:
    update_cache: yes

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

- name: Run pgdg 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 adding PGDG
  apt:
    update_cache: yes

- name: Install PostgreSQL 16 and contrib
  apt:
    name:
      - postgresql-16
      - postgresql-contrib-16
    state: present

- name: Set custom postgresql.conf
  template:
    src: postgresql.conf.j2
    dest: /etc/postgresql/16/main/postgresql.conf
    owner: postgres
    group: postgres
    mode: 0644
  notify: Restart PostgreSQL

- name: Set custom pg_hba.conf
  template:
    src: pg_hba.conf.j2
    dest: /etc/postgresql/16/main/pg_hba.conf
    owner: postgres
    group: postgres
    mode: 0640
  notify: Restart PostgreSQL
Enter fullscreen mode Exit fullscreen mode

roles/db/handlers/main.yml

---
- name: Restart PostgreSQL
  service:
    name: postgresql
    state: restarted
Enter fullscreen mode Exit fullscreen mode

roles/db/templates/postgresql.conf.j2

Minimal working config (customize as needed):

listen_addresses = '*'
port = 5432
max_connections = 100
shared_buffers = 128MB
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql.log'
Enter fullscreen mode Exit fullscreen mode

roles/db/templates/pg_hba.conf.j2

Allow local and password-based remote access:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5
Enter fullscreen mode Exit fullscreen mode

Run it

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

Bonus Tip

To reset the password for postgres user after install:


yaml
- name: Set postgres user password
  become_user: postgres
  shell: psql -c "ALTER USER postgres WITH PASSWORD '{{ postgres_password }}';"
`Define `postgres_password` in `roles/db/vars/main.yml`.


That’s it — your infra now auto-installs PostgreSQL 16 with config hardcoded to your needs.



---



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


Return only the cleaned text without any additional commentary:

 [![git-lrc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzvpkxm9mga1pweneahx.png)](https://github.com/HexmosTech/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, Unlimited 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.
  • 🔁 Build a



Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.