# installing postgres - error with initdb -D /path/to/pg\_data

**URL:** <https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932>\
**Category:** Ansible Project\
**Created:** [October 27, 2021, 11:55am UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932 "2021-10-27T11:55:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 27, 2021, 11:55am UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/1 "2021-10-27T11:55:20Z")

</div>

I have a playbook to install postgresql on a rhel8 target

one TASK should start the initdb process …

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

- name: configure postgres - create database  
command: “{{ postgres\_home }}/bin/initdb -D {{ postgres\_data }}”  
args:  
creates: “{{ postgres\_data }}/PG\_VERSION”  
become: yes  
become\_user: postgres

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

but fails with an “initdb: error: cannot be run as root” error \*details below

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

TASK [configure postgres - create database] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [[vm-51150-0180.step.zrz.dvz.cn-mv.de](http://vm-51150-0180.step.zrz.dvz.cn-mv.de)]: FAILED! =\> changed=true  
cmd:

- /opt/db/postgres/postgresql/bin/initdb
- -D
- /opt/db/data/postgres/data  
delta: ‘0:00:00.009346’  
end: ‘2021-10-27 11:54:25.210944’  
msg: non-zero return code  
rc: 1  
start: ‘2021-10-27 11:54:25.201598’  
stderr: |-  
initdb: error: cannot be run as root  
Please log in (using, e.g., “su”) as the (unprivileged) user that will  
own the server process.  
stderr\_lines:   
stdout: ‘’  
stdout\_lines:   
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

the TASK however has a particular become\_user: postgres specified. Furthermore Selinux is set to @permissive in an erlier TASK.

Manually running /opt/db/postgres/postgresql/bin/initdb -D /opt/db/data/postgres/data/ on the target as user postgres works, so I can not make much sense of this. It’s kind of obvious the become process does not work, but I can’t figure out why. Plaid a bit with indentation, but did not find any solution that works

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 27, 2021, 1:32pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/2 "2021-10-27T13:32:45Z")

</div>

- here is the [entire playbook](https://pastebin.com/edit/9Uvjdupe)
- here is the [defaults\_postgres.yml](https://pastebin.com/ZKjis1BA) I refer to in it

I am wondering whether the use of a …

- ansible\_become\_user: root

- ansbile\_postgres\_user: postgres

… in the same context may be related to my problem

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [October 27, 2021, 2:45pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/3 "2021-10-27T14:45:18Z")

</div>

Hey!

We can’t access the playbook here - [https://pastebin.com/edit/9Uvjdupe](https://pastebin.com/edit/9Uvjdupe) at least I can’t. pastebin redirects me to the login page. I can however access default\_postgres.yml file

I’m going to assume you are using sudo as become\_method. I am also going to assume selinux is not part of the issue here, nor standard unix permissions or posix acls.

When you run the task manually: /opt/db/postgres/postgresql/bin/initdb -D /opt/db/data/postgres/data/ on the target as user postgres … say you login as root to the machine then you run su - postgres, right? At this point you are running commands as postgres.

I’m wondering if you can mimic that very same behaviour adding become\_flags to the equation:

- name: configure postgres - create database  
command: “{{ postgres\_home }}/bin/initdb -D {{ postgres\_data }}”  
args:  
creates: “{{ postgres\_data }}/PG\_VERSION”  
become: yes  
become\_flags: “su - {{ ansible\_postgres\_user }} -c”  
become\_user: “{{ ansible\_postgres\_user }}”

Please let me know if that works,

Regards,

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [October 27, 2021, 2:47pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/4 "2021-10-27T14:47:24Z")

</div>

Just remove the word "edit/" from that first URL.

Antony.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 27, 2021, 3:02pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/5 "2021-10-27T15:02:39Z")

</div>

sorry, yes: [https://pastebin.com/9Uvjdupe](https://pastebin.com/9Uvjdupe)

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 27, 2021, 3:11pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/6 "2021-10-27T15:11:48Z")

</div>

I figured out that apparently using ansible\_become\_user: root in the defaults.yml is meesing things up

Changing  
ansible\_become\_user: root  
to  
ansible\_root\_user: root

in the defaults file and the playbok makes this issue going away. Apparently the term ansible\_become\_user is more then just a string and overwrites all other become\_user occurences.

If I remember correctly it may be even workable to skip the become\_user: xyz (and just say become: yes) in the playbook if I do not want to become anyone else then root and likewise share me this sort of trouble

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 27, 2021, 3:20pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/7 "2021-10-27T15:20:52Z")

</div>

> I figured out that apparently using ansible\_become\_user: root in the defaults.yml is meesing things up
> 
> Changing  
> ansible\_become\_user: root  
> to  
> ansible\_root\_user: root
> 
> in the defaults file and the playbok makes this issue going away. Apparently the term ansible\_become\_user is more then just a string and overwrites all other become\_user occurences.

I think there is actually no need to set these variables at all as "root" is the default.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 27, 2021, 6:01pm UTC](https://forum.ansible.com/t/installing-postgres-error-with-initdb-d-path-to-pg-data/35932/8 "2021-10-27T18:01:27Z")

</div>

I think so to. Just got carried away with it somewhat
