🐘 PostgreSQL

PostgreSQL is the standardized database engine used across the homelab whenever an application supports it. Services such as Radarr, Sonarr, and Lidarr all use PostgreSQL for their backend storage. The entire lifecycle—installation, configuration, database creation, access control, and backups—is automated using Ansible.

👉 The PostgreSQL Usage page provides a view on how PostgreSQL is used in this home lab.


🏗️ Architecture Overview

Component Description
pg-1 Dedicated PostgreSQL VM (inventory group: pgdb)
Ansible Role postgresql_setup installs and configures PostgreSQL
Application DB Roles Each service (Radarr, Sonarr, etc.) has a role that creates its DB + user
Backups Per‑service backup tasks using pg_dump
Storage Backups stored on NFS share hosted by TrueNAS

🚀 Deployment via Ansible

PostgreSQL is deployed using the postgresql_setup role. This role:

Key Features of the Role


🗂️ Inventory Layout

Applications that use PostgreSQL reference the pgdb group:

[pgdb]
pg-1

This ensures all database‑related tasks run on the correct host.


🛠️ Creating Databases for Applications

Each application has a dedicated Ansible role that:

Example: Radarr

The Radarr role:

This pattern is reused for Sonarr, Lidarr, and any future services.


💾 Backups

Backups are performed using a dedicated task file that:

  1. Runs pg_dump in custom format (-Fc)
  2. Stores the backup on an NFS mount provided by TrueNAS
  3. Keeps only the latest 3 backups per service

Backup Workflow

This ensures backups remain lightweight and manageable.


🧩 Adding a New PostgreSQL‑Backed Service

To add a new service:

  1. Add the service host to inventory
  2. Create a role with a database.yml task file
  3. Use community.postgresql modules to create user + DB
  4. Add pg_hba.conf entries for the service host
  5. Add .pgpass entries for automation
  6. Add a backup task file using the standard pattern

A full contributor guide Adding PostgreSQL to a Service Contributor Guide is provided.