v2.0.0
This commit is contained in:
@@ -1,96 +1,111 @@
|
||||
- name: Install PostgreSQL packages
|
||||
---
|
||||
# Instalacja PostgreSQL (najnowsza wersja) i konfiguracja klastra
|
||||
# Role: primary i replica
|
||||
|
||||
- name: Install PostgreSQL and contrib packages
|
||||
apt:
|
||||
name:
|
||||
- postgresql
|
||||
- postgresql-contrib
|
||||
- python3-psycopg2
|
||||
update_cache: yes
|
||||
state: present
|
||||
|
||||
# --- Detect PostgreSQL version and cluster ---
|
||||
- name: Detect PostgreSQL version
|
||||
shell: "ls -d /etc/postgresql/*/ | head -n1 | xargs basename"
|
||||
register: pg_version_result
|
||||
# --- Auto-detect PostgreSQL version ---
|
||||
- name: Detect installed PostgreSQL version
|
||||
shell: "ls -d /etc/postgresql/*/ | head -1 | xargs basename"
|
||||
register: pg_version_detect
|
||||
changed_when: false
|
||||
failed_when: pg_version_detect.rc != 0
|
||||
|
||||
- name: Detect cluster name
|
||||
shell: "ls -d /var/lib/postgresql/*/main/ 2>/dev/null | head -n1 | xargs -I {} basename $(dirname {}) | tail -n1"
|
||||
register: pg_cluster_result
|
||||
changed_when: false
|
||||
|
||||
- name: Set PostgreSQL version and cluster facts
|
||||
- name: Set PostgreSQL version variable
|
||||
set_fact:
|
||||
pg_version: "{{ pg_version_result.stdout | trim }}"
|
||||
pg_version: "{{ pg_version_detect.stdout | trim }}"
|
||||
pg_cluster: "main"
|
||||
pg_data: "/var/lib/postgresql/{{ pg_version_detect.stdout | trim }}/main"
|
||||
pg_config_path: "/etc/postgresql/{{ pg_version_detect.stdout | trim }}/main"
|
||||
pg_bin_path: "/usr/lib/postgresql/{{ pg_version_detect.stdout | trim }}/bin"
|
||||
|
||||
- name: Define PostgreSQL cluster path
|
||||
set_fact:
|
||||
pg_cluster_path: "/etc/postgresql/{{ pg_version }}/{{ pg_cluster }}"
|
||||
pg_data_path: "/var/lib/postgresql/{{ pg_version }}/{{ pg_cluster }}"
|
||||
- name: Display detected PostgreSQL version
|
||||
debug:
|
||||
msg: "PostgreSQL {{ pg_version }} detected. Data path: {{ pg_data }}"
|
||||
|
||||
# --- Stop cluster before config ---
|
||||
- name: Stop PostgreSQL cluster
|
||||
shell: "pg_ctlcluster {{ pg_version }} {{ pg_cluster }} stop"
|
||||
ignore_errors: true
|
||||
|
||||
# --- Remove broken cluster configuration if exists ---
|
||||
- name: Remove broken cluster configuration
|
||||
shell: "rm -rf {{ pg_cluster_path }} {{ pg_data_path }}"
|
||||
ignore_errors: true
|
||||
|
||||
# --- Ensure data directory exists and is owned by postgres ---
|
||||
- name: Create PostgreSQL data directory
|
||||
# --- Tworzenie logów PostgreSQL ---
|
||||
- name: Ensure log directory exists
|
||||
file:
|
||||
path: "{{ pg_data_path }}"
|
||||
path: /var/log/postgresql
|
||||
state: directory
|
||||
owner: postgres
|
||||
group: postgres
|
||||
mode: "0755"
|
||||
|
||||
# --- Inicjalizacja klastra ---
|
||||
- name: Check if cluster data directory exists
|
||||
stat:
|
||||
path: "{{ pg_data }}"
|
||||
register: pg_data_stat
|
||||
|
||||
- name: Create cluster data directory if not exists
|
||||
file:
|
||||
path: "{{ pg_data }}"
|
||||
state: directory
|
||||
owner: postgres
|
||||
group: postgres
|
||||
mode: "0700"
|
||||
when: not pg_data_stat.stat.exists
|
||||
|
||||
# --- Initialize database cluster ---
|
||||
- name: Initialize PostgreSQL cluster
|
||||
shell: "sudo -u postgres /usr/lib/postgresql/{{ pg_version }}/bin/initdb -D {{ pg_data_path }} --encoding=UTF8 --locale=en_US.UTF-8"
|
||||
ignore_errors: true
|
||||
shell: "{{ pg_bin_path }}/initdb -D {{ pg_data }} --encoding=UTF8 --locale=en_US.UTF-8"
|
||||
become: true
|
||||
become_user: postgres
|
||||
when: not pg_data_stat.stat.exists
|
||||
register: initdb_result
|
||||
|
||||
# --- Ensure cluster config directory exists ---
|
||||
- name: Ensure cluster config directory exists
|
||||
- name: Create config directory for cluster
|
||||
file:
|
||||
path: "{{ pg_cluster_path }}"
|
||||
path: "{{ pg_config_path }}"
|
||||
state: directory
|
||||
owner: postgres
|
||||
group: postgres
|
||||
mode: "0700"
|
||||
mode: "0755"
|
||||
|
||||
# --- Configure PostgreSQL ---
|
||||
- name: postgresql.conf
|
||||
# --- Konfiguracja PostgreSQL ---
|
||||
- name: Deploy postgresql.conf
|
||||
template:
|
||||
src: postgresql.conf.j2
|
||||
dest: "{{ pg_cluster_path }}/postgresql.conf"
|
||||
dest: "{{ pg_config_path }}/postgresql.conf"
|
||||
owner: postgres
|
||||
group: postgres
|
||||
mode: "0600"
|
||||
notify: restart postgresql
|
||||
|
||||
- name: pg_hba.conf
|
||||
- name: Deploy pg_hba.conf
|
||||
template:
|
||||
src: pg_hba.conf.j2
|
||||
dest: "{{ pg_cluster_path }}/pg_hba.conf"
|
||||
dest: "{{ pg_config_path }}/pg_hba.conf"
|
||||
owner: postgres
|
||||
group: postgres
|
||||
mode: "0600"
|
||||
notify: restart postgresql
|
||||
|
||||
# --- Start cluster ---
|
||||
- name: Start PostgreSQL cluster
|
||||
shell: "sudo -u postgres /usr/lib/postgresql/{{ pg_version }}/bin/pg_ctl -D {{ pg_data_path }} -l /var/log/postgresql/postgresql-{{ pg_version }}-{{ pg_cluster }}.log start"
|
||||
ignore_errors: true
|
||||
# --- Uruchomienie PostgreSQL ---
|
||||
- name: Enable and start PostgreSQL service
|
||||
systemd:
|
||||
name: postgresql
|
||||
enabled: yes
|
||||
state: started
|
||||
daemon_reload: yes
|
||||
|
||||
# --- Wait for socket to be ready ---
|
||||
- name: Wait for PostgreSQL socket
|
||||
# --- Czekanie na socket ---
|
||||
- name: Wait for PostgreSQL socket to be ready
|
||||
wait_for:
|
||||
path: "/var/run/postgresql/{{ pg_version }}-{{ pg_cluster }}/.s.PGSQL.5432"
|
||||
path: "/var/run/postgresql/.s.PGSQL.5432"
|
||||
timeout: 60
|
||||
|
||||
# --- Install Python PostgreSQL libraries ---
|
||||
- name: Install python postgres libraries
|
||||
apt:
|
||||
name:
|
||||
- python3-psycopg2
|
||||
state: present
|
||||
update_cache: yes
|
||||
|
||||
# Handlery
|
||||
- name: restart postgresql
|
||||
systemd:
|
||||
name: postgresql
|
||||
state: restarted
|
||||
listen: "restart postgresql"
|
||||
|
||||
15
roles/postgres/templates/pg_hba.conf.j2
Normal file
15
roles/postgres/templates/pg_hba.conf.j2
Normal file
@@ -0,0 +1,15 @@
|
||||
# PostgreSQL 16 pg_hba.conf - Host Based Authentication
|
||||
# TYPE DATABASE USER ADDRESS METHOD
|
||||
|
||||
# Local connections
|
||||
local all postgres peer
|
||||
local all all peer
|
||||
|
||||
# Replication from pg2 (10.0.0.2)
|
||||
host replication replicator 10.0.0.2/32 md5
|
||||
|
||||
# Applications and MailArchiver
|
||||
host all all 10.0.0.0/24 md5
|
||||
|
||||
# IPv6 local
|
||||
host all all ::1/128 md5
|
||||
51
roles/postgres/templates/postgresql.conf.j2
Normal file
51
roles/postgres/templates/postgresql.conf.j2
Normal file
@@ -0,0 +1,51 @@
|
||||
# PostgreSQL Configuration for Production
|
||||
# Debian 13 - najnowsza dostępna wersja
|
||||
# Primary: pg1, Replica: pg2
|
||||
|
||||
# --- Network ---
|
||||
listen_addresses = '*'
|
||||
max_connections = 200
|
||||
port = 5432
|
||||
|
||||
# --- Memory (dla 8GB RAM) ---
|
||||
shared_buffers = 2GB
|
||||
effective_cache_size = 6GB
|
||||
work_mem = 64MB
|
||||
maintenance_work_mem = 1GB
|
||||
|
||||
# --- WAL and Logging ---
|
||||
wal_level = replica
|
||||
wal_compression = on
|
||||
max_wal_senders = 10
|
||||
max_replication_slots = 10
|
||||
wal_keep_size = 10GB
|
||||
|
||||
# --- Archiving (dla pgBackRest) ---
|
||||
archive_mode = on
|
||||
archive_command = 'pgbackrest --stanza=main archive-push %p'
|
||||
archive_timeout = 300
|
||||
|
||||
# --- Checkpoints ---
|
||||
checkpoint_timeout = 15min
|
||||
checkpoint_completion_target = 0.9
|
||||
max_wal_size = 64GB
|
||||
min_wal_size = 16GB
|
||||
|
||||
# --- Logging ---
|
||||
log_directory = '/var/log/postgresql'
|
||||
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
|
||||
log_truncate_on_rotation = on
|
||||
log_rotation_age = 1d
|
||||
log_rotation_size = 100MB
|
||||
log_min_duration_statement = 1000
|
||||
log_connections = on
|
||||
log_disconnections = on
|
||||
log_duration = off
|
||||
|
||||
# --- Replication slots ---
|
||||
max_replication_slots = 10
|
||||
|
||||
# --- Performance ---
|
||||
random_page_cost = 1.1
|
||||
effective_io_concurrency = 200
|
||||
synchronous_commit = local
|
||||
Reference in New Issue
Block a user