databases: use initdb folder to create DBs and users

This way when the database is cleared the user is automatically created
without the need to run Ansible.

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2023-11-21 16:11:21 +01:00
parent 8fcbe906fc
commit bbdb294e3d
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
7 changed files with 23 additions and 31 deletions

View File

@ -31,6 +31,8 @@ postgres_ha_databases:
```
The `user` field is optional. DB name is used by default.
:warning: __WARNING:__ This only takes effect at database creation.
Backup settings can be adjusted using:
```yaml
postgres_ha_backup: false

View File

@ -5,6 +5,7 @@ postgres_ha_compose_file: '{{ postgres_ha_service_path }}/docker-compose.yml'
postgres_ha_cont_name: '{{ postgres_ha_service_name }}'
postgres_ha_cont_data_vol: '{{ postgres_ha_service_path }}/data'
postgres_ha_cont_init_vol: '{{ postgres_ha_service_path }}/init'
postgres_ha_cont_backup_vol: '{{ postgres_ha_service_path }}/backup'
postgres_ha_cont_image: 'postgres:15.1-alpine'
postgres_ha_cont_port: 5432

View File

@ -9,8 +9,20 @@
with_items:
- '{{ postgres_ha_service_path }}'
- '{{ postgres_ha_cont_data_vol }}'
- '{{ postgres_ha_cont_init_vol }}'
- '{{ postgres_ha_cont_backup_vol }}'
- name: Create dbinit SQL files
when: postgres_ha_is_master
template:
src: '{{ item }}'
dest: '{{ postgres_ha_cont_init_vol }}/{{ item | basename }}'
owner: '{{ postgres_ha_host_uid }}'
group: 'dockremap'
mode: 0640
with_fileglob:
- 'templates/init/*.sql'
- name: 'Create compose file'
template:
src: 'docker-compose.yml.j2'

View File

@ -1,25 +0,0 @@
---
- name: 'Create user: {{ db.get("user", db.name) }}'
command: >
docker exec {{ postgres_ha_cont_name }}
psql
--username={{ postgres_ha_admin_user }}
--port={{ postgres_ha_cont_port }}
--command
"DO $$
BEGIN
CREATE USER \"{{ db.get("user", db.name) }}\" PASSWORD '{{ db.pass | mandatory }}';
EXCEPTION WHEN DUPLICATE_OBJECT THEN
RAISE NOTICE 'not creating role {{ db.get("user", db.name) }} -- it already exists';
END
$$;"
- name: 'Create database: {{ db.name | mandatory }}'
shell: >
echo "SELECT
'CREATE DATABASE \"{{ db.name }}\" WITH OWNER \"{{ db.get("user", db.name) }}\";'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '{{ db.name }}')\\\gexec" |
docker exec -i {{ postgres_ha_cont_name }} \
psql \
--username={{ postgres_ha_admin_user }} \
--port={{ postgres_ha_cont_port }}

View File

@ -16,12 +16,6 @@
- import_tasks: start.yml
when: not postgres_ha_is_master
- include_tasks: databases.yml
when: postgres_ha_is_master
with_items: '{{ postgres_ha_databases }}'
loop_control:
loop_var: db
- import_tasks: consul.yml
- import_tasks: backup.yml

View File

@ -21,6 +21,7 @@ services:
- '/tmp:size={{ postgres_ha_cont_tmp_size }}'
volumes:
- '{{ postgres_ha_cont_data_vol }}:/var/lib/postgresql/data'
- '{{ postgres_ha_cont_init_vol }}:/docker-entrypoint-initdb.d'
- '{{ postgres_ha_cont_backup_vol }}:/backup'
command: |
-p {{ postgres_ha_cont_port }}

View File

@ -0,0 +1,7 @@
-- File managed by Ansible.
{% for db in postgres_ha_databases %}
-- Create {{ db.name }} database.
CREATE USER "{{ db.get("user", db.name) }}" PASSWORD '{{ db.pass | mandatory }}';
CREATE DATABASE "{{ db.name }}" WITH OWNER "{{ db.get("user", db.name) }}";
{% endfor %}