---
- name: Ensure PostgreSQL database is initialized.
  command: "postgresql-setup --initdb --unit postgresql"
  args:
    creates: "/var/lib/pgsql/data/PG_VERSION"

- name: set max_connections for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^max_connections ='
    line: 'max_connections = 150'
  notify: restart postgresql

- name: set shared_buffers for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^shared_buffers ='
    line: 'shared_buffers = 25536MB'
  notify: restart postgresql

- name: set effective_cache_size for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^effective_cache_size ='
    line: 'effective_cache_size = 50608MB'
  notify: restart postgresql

- name: set work_mem for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^work_mem ='
    line: 'work_mem = 6MB'
  notify: restart postgresql

- name: set maintenance_work_mem for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^maintenance_work_mem ='
    line: 'maintenance_work_mem = 2GB'
  notify: restart postgresql

- name: set checkpoint_completion_target for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^checkpoint_completion_target ='
    line: 'checkpoint_completion_target = 0.9'
  notify: restart postgresql

- name: set wal_buffers for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^wal_buffers ='
    line: 'wal_buffers = -1'
  notify: restart postgresql

- name: set default_statistics_target for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^default_statistics_target ='
    line: 'default_statistics_target = 100'
  notify: restart postgresql

- name: drop faf database
  postgresql_db:
    name: faf
    owner: postgres
    state: absent
  when: faf_recreate_database|bool

- name: start service postgresql
  service:
    name: postgresql
    state: started
    enabled: yes
  become: true

- name: pgsql create db faf
  postgresql_db:
    name: faf
    owner: postgres
    state: present
  become: true
  become_user: postgres

- name: pgsql create user faf
  postgresql_user:
    db: faf
    name: faf
    priv: ALL
    role_attr_flags: SUPERUSER
    state: present
  become: true
  become_user: postgres

- name: create extension for faf
  postgresql_ext:
    name: semver
    db: faf
    state: present
  become: true
  become_user: postgres
