---
- name: Install dependencies
  ansible.builtin.apt:
    name:
      - curl
      - ca-certificates
      - python3-psycopg2
    state: present
    update_cache: yes

- name: Create directory for PostgreSQL repo key
  ansible.builtin.file:
    path: /usr/share/postgresql-common/pgdg
    state: directory
    mode: '0755'

- name: Download PostgreSQL repo GPG key
  ansible.builtin.get_url:
    url: https://www.postgresql.org/media/keys/ACCC4CF8.asc
    dest: /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc
    mode: '0644'

- name: Add PostgreSQL APT repository
  apt_repository:
    repo: "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt {{ ansible_distribution_release }}-pgdg main"
    state: present
    update_cache: yes

- name: Install PostgreSQL
  ansible.builtin.apt:
    name: "postgresql-{{ db_version }}"
    state: present

