aboutsummaryrefslogtreecommitdiffstats
path: root/roles/postgres/tasks/main.yml
blob: f3d24daded1ed51486bb8b56b66b2326a88e5033 (plain) (blame)
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
---

- name: install postgres
  ansible.builtin.package:
    name:
      - postgresql-server
      - postgresql-contrib
    state: present

- name: create postgres directories
  ansible.builtin.file:
    path: "{{ postgres_dir }}/{{ item }}"
    owner: "{{ postgres_user }}"
    group: "{{ postgres_group }}"
    mode: "0750"
    state: directory
  loop:
    - ""
    - "data"

- name: delete default data dir if exists
  ansible.builtin.file:
    path: "{{ postgres_default_dir }}"
    state: absent
  when: postgres_dir != "/var/postgresql"

- name: link postgres directory to default one
  ansible.builtin.file:
    src: "{{ postgres_dir }}"
    dest: "{{ postgres_default_dir }}"
    owner: "{{ postgres_user }}"
    group: "{{ postgres_group }}"
    state: link
    mode: "0750"
    follow: false
  when: postgres_dir != "/var/postgresql"

- name: init postgres database  # noqa: no-changed-when
  ansible.builtin.shell:
    cmd: |
      su "{{ postgres_user }}" \
        -c 'initdb -D "{{ postgres_dir }}/data/" -U "{{ postgres_db_user }}" --encoding=UTF-8 --locale=en_US.UTF-8'
    chdir: "{{ postgres_dir }}"
  register: result
  failed_when:
    - result.rc != 0
    - "'exists but is not empty' not in result.stderr"

- name: enable and restart postgres service
  ansible.builtin.service:
    name: postgresql
    state: restarted
    enabled: true
remember that computers suck.