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"
network: "<< defaults_applications[svc-db-postgres].hostname >>"
volume: "<< defaults_applications[svc-db-postgres].hostname >>"
docker:
services:
postgres:
@ -8,4 +7,5 @@ docker:
image: postgis/postgis
# Please set an version in your inventory file!
# 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
docker_network:
name: "{{ applications | get_app_conf(application_id, 'network', True) }}"
name: "{{ postgres_network_name }}"
state: present
ipam_config:
- subnet: "{{ networks.local['svc-db-postgres'].subnet }}"
- subnet: "{{ postgres_subnet }}"
when: run_once_docker_postgres is not defined
- name: Install PostgreSQL
docker_container:
name: "{{ applications | get_app_conf(application_id, 'hostname', True) }}"
image: "{{ applications | get_app_conf(application_id, 'docker.services.postgres.image', True) }}:{{ applications | get_app_conf(application_id, 'docker.services.postgres.version', True) }}"
name: "{{ postgres_hostname }}"
image: "{{ postgres_image }}:{{ postgres_version }}"
detach: yes
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
networks:
- name: "{{ applications | get_app_conf(application_id, 'network', True) }}"
- name: "{{ postgres_network_name }}"
published_ports:
- "127.0.0.1:{{ database_port }}:5432"
- "127.0.0.1:{{ postgres_port }}:5432"
volumes:
- "{{ applications['svc-db-postgres'].volume }}:/var/lib/postgresql/data"
- "{{ postgres_volume }}:/var/lib/postgresql/data"
restart_policy: "{{ docker_restart_policy }}"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
@ -31,7 +31,7 @@
when: run_once_docker_postgres is not defined
- 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
until: pg_ready.rc == 0
retries: 30
@ -47,12 +47,9 @@
state: present
when: run_once_docker_postgres is not defined
- name: Load database initialization tasks dynamically
include_tasks: init_database.yml
when:
- database_username is defined
- database_password is defined
- database_name is defined
- name: "Initialize database for '{{ database_name }}'"
include_tasks: init.yml
when: "{{ postgres_init }}"
- name: Run the docker_postgres tasks once
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 }}"