# I want to init postgresql in k8s, who can share me the sample,thanks!

**URL:** <https://forum.ansible.com/t/i-want-to-init-postgresql-in-k8s-who-can-share-me-the-sample-thanks/36480>\
**Category:** Ansible Project\
**Tags:** kubernetes\
**Created:** [March 25, 2022, 3:44pm UTC](https://forum.ansible.com/t/i-want-to-init-postgresql-in-k8s-who-can-share-me-the-sample-thanks/36480 "2022-03-25T15:44:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![user122](https://avatars.discourse-cdn.com/v4/letter/u/5e9695/32.png) [@user122](https://forum.ansible.com/u/user122)\
**Post date:** [March 25, 2022, 3:44pm UTC](https://forum.ansible.com/t/i-want-to-init-postgresql-in-k8s-who-can-share-me-the-sample-thanks/36480/1 "2022-03-25T15:44:39Z")

</div>

Hi all,  
I want to init postgresql in k8s, try use the code as below to do:

- name: Get the password for postgresql  
kubernetes.core.k8s\_info:  
kind: secret  
name: postgresql-ha-postgresql  
namespace: pgdb  
register: pgsql\_info

- name:  
set\_fact:  
pg\_pwd: “{{ pgsql\_info |json\_query(item) | b64decode }}”  
with\_items: ‘resources[\*].data.“postgresql-password”’

- name:  
debug: msg={{ pg\_pwd }}

- name: Install psycopg2 for ansible to be able to create postgresql users  
pip:  
name: psycopg2-binary

- name: start k8s port forward  
shell: nohup kubectl port-forward $(kubectl get pod -l [app.kubernetes.io/component=pgpool](http://app.kubernetes.io/component=pgpool) -n pgdb -o name|cut -d/ -f2) 5432 -n pgdb &

- name: Check if database is available  
community.postgresql.postgresql\_ping:  
db: postgres  
login\_host: 127.0.0.1  
login\_port: 5432  
login\_user: postgres  
login\_password: “{{ pg\_pwd }}”  
register: ping\_database  
failed\_when: ping\_database.warnings is defined

- name: Check the DB of ioccx  
community.postgresql.postgresql\_query:  
db: postgres  
login\_host: 127.0.0.1  
port: 5432  
login\_user: postgres  
login\_password: “{{ pg\_pwd }}”  
query: |  
SELECT datname FROM pg\_database where datname = ‘ioccx’  
encoding: UTF-8  
positional\_args: 1  
register: db\_ioccx  
when: ping\_database.is\_available

# - name: restore dump to DB of ioccx

# postgresql\_db:

# login\_host: 127.0.0.1

# login\_user: postgres

# login\_password: “{{ pg\_pwd }}”

# name: ioccx

# state: restore

# target: 20180626.dump.tar.gz

# target\_opts: “–create --clean”

# when: db\_ioccx.query\_result | length == 0

- name: “CREATE ROLE ioccadmin”  
register: result  
retries: 100  
until: result is success  
when: db\_ioccx.query\_result | length == 0  
postgresql\_user:  
login\_host: 127.0.0.1  
login\_user: postgres  
login\_password: “{{ pg\_pwd }}”  
port: 5432  
state: present  
name: ioccadmin  
password: “zaq12wsx”  
encrypted: true  
role\_attr\_flags: SUPERUSER

- name: “CREATE DATABASE ioccx”  
when: db\_ioccx.query\_result | length == 0  
register: create\_database  
postgresql\_db:  
login\_host: 127.0.0.1  
login\_user: postgres  
login\_password: “{{ pg\_pwd }}”  
port: 5432  
state: present  
name: ioccx  
owner: ioccadmin  
encoding: “UTF-8”  
lc\_collate: “en\_EN.UTF-8”  
lc\_ctype: “en\_EN.UTF-8”  
template: “template0”

- name: “GRANT ALL PRIVILEGES ON ioccx TO ioccadmin”  
when: db\_ioccx.query\_result | length == 0  
postgresql\_privs:  
login\_host: 127.0.0.1  
login\_user: postgres  
login\_password: “{{ pg\_pwd }}”  
port: 5432  
state: present  
db: ioccx  
privs: “ALL”  
type: “database”  
obj: ioccx  
role: ioccadmin

but can’t connect the postgresql, presumably the problem is that for-forward is not running continuously in the background

Do you have any more elegant way to initialize PostgreSQL in k8s？By the way, would you pls tell me how to keep shell commands running in the background？

---

<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:** [March 25, 2022, 6:07pm UTC](https://forum.ansible.com/t/i-want-to-init-postgresql-in-k8s-who-can-share-me-the-sample-thanks/36480/2 "2022-03-25T18:07:55Z")

</div>

正光，你好！

> I want to init postgresql in k8s, try use the code as below to do:

I would say this happens when the container image is run, latest with an entrypoint script.  
If you study the official docker postgres repository I think this happens starting from line 63 in the [docker-entrypoint.sh](https://github.com/docker-library/postgres/blob/master/14/bullseye/docker-entrypoint.sh)

that said … there wouldn‘t be a need really to try doing this with Ansible, unless you are doing something very unique

---

<div class="post-metadata">

**Author:** ![user122](https://avatars.discourse-cdn.com/v4/letter/u/5e9695/32.png) [@user122](https://forum.ansible.com/u/user122)\
**Post date:** [March 29, 2022, 3:24am UTC](https://forum.ansible.com/t/i-want-to-init-postgresql-in-k8s-who-can-share-me-the-sample-thanks/36480/3 "2022-03-29T03:24:03Z")

</div>

Thanks for your answer, I need some special operations like I need to create separate database , suah as my DB ioccx, and create some tables for this database etc. My real intention is not to initialize empty PGDATA directory

在2022年3月26日星期六 UTC+8 02:08:15[dulh...@mailbox.org](mailto:dulh...@mailbox.org) 写道：
