- name: Install gnupg
  apt:
    pkg: gnupg

- name: Install key
  apt_key:
    url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
    state: present

- name: Add postgresql repository into sources list as pgdg file
  ansible.builtin.apt_repository:
    repo: "deb http://apt.postgresql.org/pub/repos/apt {{ ansible_distribution_release }}-pgdg main" 
    state: present
    filename: pgdg

- name: Install postgre
  apt:
    pkg: postgresql-15*
  register: postgre_installation

- name: Delete old configuration
  shell: rm -rf /var/lib/postgresql/*
  when: postgre_installation.changed

- name: Init PostgreSQL
  shell: pg_createcluster 15 main --start
  when: postgre_installation.changed