Optimized postgres

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-14 10:05:22 +02:00
parent ac72544b72
commit 8161dd1b6d
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
4 changed files with 24 additions and 18 deletions

View File

@ -1,6 +1,5 @@
hostname: "svc-db-postgres" hostname: "svc-db-postgres"
network: "<< defaults_applications[svc-db-postgres].hostname >>" network: "<< defaults_applications[svc-db-postgres].hostname >>"
volume: "<< defaults_applications[svc-db-postgres].hostname >>"
docker: docker:
services: services:
postgres: postgres:
@ -9,3 +8,4 @@ docker:
# Please set an version in your inventory file! # Please set an version in your inventory file!
# Rolling release isn't recommended # Rolling release isn't recommended
version: "latest" version: "latest"
volume: "<< defaults_applications[svc-db-postgres].hostname >>_data"

View File

@ -1,25 +1,25 @@
- name: Create Docker network for PostgreSQL - name: Create Docker network for PostgreSQL
docker_network: docker_network:
name: "{{ applications | get_app_conf(application_id, 'network', True) }}" name: "{{ postgres_network_name }}"
state: present state: present
ipam_config: ipam_config:
- subnet: "{{ networks.local['svc-db-postgres'].subnet }}" - subnet: "{{ postgres_subnet }}"
when: run_once_docker_postgres is not defined when: run_once_docker_postgres is not defined
- name: Install PostgreSQL - name: Install PostgreSQL
docker_container: docker_container:
name: "{{ applications | get_app_conf(application_id, 'hostname', True) }}" name: "{{ postgres_hostname }}"
image: "{{ applications | get_app_conf(application_id, 'docker.services.postgres.image', True) }}:{{ applications | get_app_conf(application_id, 'docker.services.postgres.version', True) }}" image: "{{ postgres_image }}:{{ postgres_version }}"
detach: yes detach: yes
env: env:
POSTGRES_PASSWORD: "{{ applications | get_app_conf(application_id, 'credentials.postgres_password', True) }}" POSTGRES_PASSWORD: "{{ postgres_password }}"
POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C" # Necessary for web-app-matrix POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C" # Necessary for web-app-matrix
networks: networks:
- name: "{{ applications | get_app_conf(application_id, 'network', True) }}" - name: "{{ postgres_network_name }}"
published_ports: published_ports:
- "127.0.0.1:{{ database_port }}:5432" - "127.0.0.1:{{ postgres_port }}:5432"
volumes: volumes:
- "{{ applications['svc-db-postgres'].volume }}:/var/lib/postgresql/data" - "{{ postgres_volume }}:/var/lib/postgresql/data"
restart_policy: "{{ docker_restart_policy }}" restart_policy: "{{ docker_restart_policy }}"
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"] test: ["CMD-SHELL", "pg_isready -U postgres"]
@ -31,7 +31,7 @@
when: run_once_docker_postgres is not defined when: run_once_docker_postgres is not defined
- name: Wait for Postgres inside the container - name: Wait for Postgres inside the container
shell: "docker exec {{ applications | get_app_conf(application_id, 'hostname', True) }} pg_isready -U postgres" shell: "docker exec {{ postgres_hostname }} pg_isready -U postgres"
register: pg_ready register: pg_ready
until: pg_ready.rc == 0 until: pg_ready.rc == 0
retries: 30 retries: 30
@ -47,12 +47,9 @@
state: present state: present
when: run_once_docker_postgres is not defined when: run_once_docker_postgres is not defined
- name: Load database initialization tasks dynamically - name: "Initialize database for '{{ database_name }}'"
include_tasks: init_database.yml include_tasks: init.yml
when: when: "{{ postgres_init }}"
- database_username is defined
- database_password is defined
- database_name is defined
- name: Run the docker_postgres tasks once - name: Run the docker_postgres tasks once
set_fact: set_fact:

View File

@ -1 +1,10 @@
application_id: svc-db-postgres application_id: svc-db-postgres
postgres_volume: "{{ applications | get_app_conf(application_id, 'docker.services.postgres.volume', True) }}"
postgres_hostname: "{{ applications | get_app_conf(application_id, 'hostname', True) }}"
postgres_image: "{{ applications | get_app_conf(application_id, 'docker.services.postgres.image', True) }}"
postgres_subnet: "{{ networks.local['svc-db-postgres'].subnet }}"
postgres_network_name: "{{ applications | get_app_conf(application_id, 'network', True) }}"
postgres_version: "{{ applications | get_app_conf(application_id, 'docker.services.postgres.version', True) }}"
postgres_password: "{{ applications | get_app_conf(application_id, 'credentials.postgres_password', True) }}"
postgres_port: "{{ database_port | default(ports.localhost.database[ application_id ]) }}"
postgres_init: "{{ database_username is defined and database_password is defined and database_name is defined }}"