From 5534eecf07c5feea015d90f6bbe1f7a864758511 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= <me@rgoncalves.se>
Date: Tue, 21 Dec 2021 18:39:25 +0000
Subject: roles/postgres: Force initdb to be run as pg user

---
 roles/postgres/tasks/main.yml | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/roles/postgres/tasks/main.yml b/roles/postgres/tasks/main.yml
index 78f208c..a203574 100644
--- a/roles/postgres/tasks/main.yml
+++ b/roles/postgres/tasks/main.yml
@@ -33,14 +33,13 @@
     follow: false
   when: postgres_dir != "/var/postgresql"
 
-  # NOTE: this tasks needs `pipelining` set to `false` to work correctly
 - name: init postgres database  # noqa: no-changed-when
-  command:
-    cmd: initdb -D "{{ postgres_dir }}/data/" -U "{{ postgres_db_user }}" --encoding=UTF-8 --locale=en_US.UTF-8
+  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
-  become: true
-  become_user: "{{ postgres_user }}"
   failed_when:
     - result.rc != 0
     - "'exists but is not empty' not in result.stderr"
-- 
cgit v1.2.3