1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# https://wiki.debian.org/PostgreSql
- name: Set up postgres
  hosts: lotus-land-story
  tasks:

    - name: Install postgres
      ansible.builtin.apt:
        pkg:
          - postgresql
          - postgresql-client
          - prometheus-postgres-exporter
        state: present

    - name: Make data directory
      ansible.builtin.file:
        path: /mnt/lotus-land-story/postgresql
        state: directory
        owner: postgres
        mode: "0700"

    - name: Set data directory
      ansible.builtin.lineinfile:
        dest: "/etc/postgresql/13/main/postgresql.conf"
        regexp: '^#?data_directory ='
        line: "data_directory = '/mnt/lotus-land-story/postgresql'"
        state: present
      notify: Restart postgres

    - name: Allow access from localhost
      community.general.ufw:
        rule: allow
        port: 5432
        proto: tcp
        from_ip: 127.0.0.1
      notify: Reload ufw

    # https://pgtune.leopard.in.ua/
    #   DB Version: 15
    #   OS Type: linux
    #   DB Type: web
    #   Total Memory (RAM): 4 GB
    #   CPUs num: 2
    #   Data Storage: ssd
    - name: Tune postgres
      ansible.builtin.lineinfile:
        dest: "/etc/postgresql/13/main/postgresql.conf"
        regexp: '^#?{{ item.key }} ='
        line: "{{ item.key }} = {{ item.value }}"
        state: present
      loop: "{{ configs | dict2items }}"
      vars:
        configs:
          max_connections: 200
          shared_buffers: 1GB
          effective_cache_size: 3GB
          maintenance_work_mem: 256MB
          checkpoint_completion_target: 0.9
          wal_buffers: 16MB
          default_statistics_target: 100
          random_page_cost: 1.1
          effective_io_concurrency: 200
          work_mem: 2621kB
          min_wal_size: 1GB
          max_wal_size: 4GB
      notify: Restart postgres

  handlers:
    - name: Import restarts
      ansible.builtin.import_tasks: restarts.yml

# vim: ft=yaml.ansible