This commit is contained in:
dima
2024-12-08 22:11:48 +03:00
parent 66471a8a89
commit 08cc9fc069
16 changed files with 166 additions and 112 deletions

View File

@ -1,4 +1,4 @@
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted
state: restarted

View File

@ -5,8 +5,6 @@
owner: postgres
group: postgres
mode: '0755'
tags:
- backup
- name: Perform database backup
command: >
@ -14,28 +12,22 @@
become_user: postgres
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Daily cron full backup
cron:
name: 'PostgreSQL daily full backup'
name: 'PostgreSQL daily full backup {{ postgres_user }}'
user: postgres
minute: '0'
hour: '1'
job: "pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/full_db_backup_{{ postgres_db }}_$(date +\\%F-\\%H-%M).sql"
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Hourly cron incremental backup
cron:
name: 'PostgreSQL hourly incremental backup'
name: 'PostgreSQL hourly incremental backup {{ postgres_user }}'
user: postgres
minute: '0'
job: "pg_dump -U {{ postgres_user }} -F c --data-only --file=\"{{ backup_dir }}/incremental_db_backup_{{ postgres_db }}_$(date +\\%F-\\%H-%M).sql\" {{ postgres_db }}"
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
PGPASSWORD: '{{ postgres_password }}'

View File

@ -1,4 +1,4 @@
- name: Configure postgresql.conf with template
- name: Configure postgresql.conf
template:
src: postgresql.conf.j2
dest: /var/lib/pgsql/data/postgresql.conf
@ -6,10 +6,8 @@
group: postgres
mode: '0644'
notify: Restart PostgreSQL
tags:
- configure
- name: Configure pg_hba.conf with template
- name: Configure pg_hba.conf
template:
src: pg_hba.conf.j2
dest: /var/lib/pgsql/data/pg_hba.conf
@ -17,5 +15,3 @@
group: postgres
mode: '0644'
notify: Restart PostgreSQL
tags:
- configure

View File

@ -4,14 +4,15 @@
owner: '{{ postgres_user }}'
encoding: UTF8
state: present
tags:
- database
- name: Create contacts table in PostgreSQL
- name: Create contacts table
community.postgresql.postgresql_query:
db: '{{ postgres_db }}'
query: 'CREATE TABLE IF NOT EXISTS contacts (id SERIAL PRIMARY KEY, name VARCHAR(100), phone_number VARCHAR(15));'
query: |
CREATE TABLE IF NOT EXISTS contacts (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
phone_number VARCHAR(15) UNIQUE
);
login_user: '{{ postgres_user }}'
login_password: '{{ postgres_password }}'
tags:
- database

View File

@ -2,13 +2,9 @@
command: sudo -u postgres initdb -D /var/lib/pgsql/data
args:
creates: /var/lib/pgsql/data/PG_VERSION
tags:
- init
- name: Systemctl start and enable PostgreSQL
service:
name: postgresql
state: started
enabled: true
tags:
- init

View File

@ -0,0 +1,11 @@
- name: Insert data into PostgreSQL database
community.postgresql.postgresql_query:
db: '{{ postgres_db }}'
query: |
INSERT INTO contacts (name, phone_number) VALUES
('dima', '123123123'),
('vlad', '8800525252'),
('denis', '77777777')
ON CONFLICT (phone_number) DO NOTHING;
login_user: '{{ postgres_user }}'
login_password: '{{ postgres_password }}'

View File

@ -1,7 +1,31 @@
- import_tasks: setup.yml
tags:
- setup
- import_tasks: initialize.yml
tags:
- init
- import_tasks: configure.yml
tags:
- configure
- import_tasks: users.yml
tags:
- users
- import_tasks: databases.yml
tags:
- database
- import_tasks: insert_data.yml
tags:
- insert
- import_tasks: open_firewall.yml
- import_tasks: backup.yml
tags:
- firewall
- import_tasks: backup.yml
tags:
- backup

View File

@ -4,11 +4,9 @@
permanent: true
state: enabled
become: true
tags:
- firewall
- name: Reload firewall using command
command: firewall-cmd --reload
- name: Reload firewalld
systemd:
name: firewalld
state: reloaded
become: true
tags:
- firewall

View File

@ -4,12 +4,8 @@
- postgresql-server
- postgresql-contrib
state: present
tags:
- setup
- name: Install python3-psycopg2
zypper:
name: python3-psycopg2
state: present
tags:
- setup

View File

@ -2,6 +2,4 @@
community.postgresql.postgresql_user:
name: '{{ postgres_user }}'
password: '{{ postgres_password }}'
state: present
tags:
- users
state: present

View File

@ -1,12 +1,11 @@
listen_addresses = '{{ postgres_listen_addresses | default("*") }}'
listen_addresses = '{{ postgres_listen_addresses }}'
port = {{ postgres_port | default(5432) }}
port = {{ postgres_port }}
max_connections = {{ postgres_max_connections | default(100) }}
shared_buffers = {{ postgres_shared_buffers | default("128MB") }}
effective_cache_size = {{ postgres_effective_cache_size | default("4GB") }}
maintenance_work_mem = {{ postgres_maintenance_work_mem | default("64MB") }}
checkpoint_completion_target = {{ postgres_checkpoint_completion_target | default(0.7) }}
wal_buffers = {{ postgres_wal_buffers | default("16MB") }}
default_statistics_target = {{ postgres_default_statistics_target | default(100) }}
max_connections = {{ postgres_max_connections }}
shared_buffers = {{ postgres_shared_buffers }}
effective_cache_size = {{ postgres_effective_cache_size }}
maintenance_work_mem = {{ postgres_maintenance_work_mem }}
checkpoint_completion_target = {{ postgres_checkpoint_completion_target }}
wal_buffers = {{ postgres_wal_buffers }}
default_statistics_target = {{ postgres_default_statistics_target }}

View File

@ -5,7 +5,14 @@ postgres_hba_entries:
- { type: 'host', database: 'all', user: 'all', address: '0.0.0.0/0', method: 'md5' }
- { type: 'local', database: 'all', user: 'all', address: '', method: 'trust' }
backup_dir: "/var/lib/pgsql/backups"
backup_dir: "/var/backups/postgresql"
postgres_user: "postgres"
postgres_password: "your_password"
postgres_db: "your_database"
postgres_db: "mydb"
postgres_max_connections: 100
postgres_shared_buffers: '128MB'
postgres_effective_cache_size: '4GB'
postgres_maintenance_work_mem: '64MB'
postgres_checkpoint_completion_target: 0.7
postgres_wal_buffers: '16MB'
postgres_default_statistics_target: 100