infra/home/ansible/upgrade-postgres-simple.yml
2025-09-06 12:46:26 -05:00

53 lines
No EOL
1.9 KiB
YAML

---
- name: Upgrade PostgreSQL to 16 and Install PostGIS
hosts: lab02
become: true
vars:
ct_id: 300
tasks:
- name: Add PostgreSQL APT repository
command: pct exec {{ ct_id }} -- bash -c "wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O- | apt-key add - && echo 'deb http://apt.postgresql.org/pub/repos/apt bookworm-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
- name: Update apt cache
command: pct exec {{ ct_id }} -- apt-get update -qq
- name: Install PostgreSQL 16
command: pct exec {{ ct_id }} -- apt-get install -y postgresql-16 postgresql-client-16
- name: Check current PostgreSQL clusters
command: pct exec {{ ct_id }} -- pg_lsclusters
register: clusters
- name: Display current clusters
debug:
msg: "{{ clusters.stdout }}"
- name: Drop old PostgreSQL 16 cluster if exists
command: pct exec {{ ct_id }} -- pg_dropcluster 16 main --stop
ignore_errors: yes
- name: Upgrade cluster from 15 to 16
command: pct exec {{ ct_id }} -- pg_upgradecluster 15 main
- name: Install PostGIS for PostgreSQL 16
command: pct exec {{ ct_id }} -- apt-get install -y postgresql-16-postgis-3
- name: Enable PostGIS in APRS database
command: pct exec {{ ct_id }} -- sudo -u postgres psql -p 5433 -d aprs -c "CREATE EXTENSION IF NOT EXISTS postgis;"
- name: Switch to port 5432 for PostgreSQL 16
command: pct exec {{ ct_id }} -- bash -c "sed -i 's/port = 5433/port = 5432/' /etc/postgresql/16/main/postgresql.conf && systemctl restart postgresql@16-main"
- name: Stop PostgreSQL 15
command: pct exec {{ ct_id }} -- systemctl stop postgresql@15-main
- name: Verify upgrade
command: pct exec {{ ct_id }} -- sudo -u postgres psql -c "SELECT version();"
register: version
- name: Display results
debug:
msg: |
PostgreSQL upgrade completed!
{{ version.stdout }}