---
# RHEL/CentOS only. Set a repository to use for PostgreSQL installation.
postgresql_enablerepo: ""

# Set postgresql state when configuration changes are made. Recommended values:
# `restarted` or `reloaded`
postgresql_restarted_state: restarted

postgresql_python_library: python-psycopg2
postgresql_user: postgres
postgresql_group: postgres

# `md5` or `scram-sha-256` (https://www.postgresql.org/docs/10/auth-methods.html)
postgresql_auth_method: "{{ ansible_fips | ternary('scram-sha-256', 'md5') }}"

postgresql_unix_socket_directories:
  - /var/run/postgresql

postgresql_service_state: started
postgresql_service_enabled: true

# Global configuration options that will be set in postgresql.conf.
postgresql_global_config_options:
  - option: unix_socket_directories
    value: '{{ postgresql_unix_socket_directories | join(",") }}'
  - option: log_directory
    value: log

# Enabe timesaledb
timescaledb_enabled: false

# Host based authentication (hba) entries to be added to the pg_hba.conf. This
# variable's defaults reflect the defaults that come with a fresh installation.
postgresql_hba_entries:
  - type: local
    database: all
    user: postgres
    auth_method: peer
  - type: local
    database: all
    user: all
    auth_method: peer
  - type: host
    database: all
    user: all
    address: 127.0.0.1/32
    auth_method: "{{ postgresql_auth_method }}"
  - type: host
    database: all
    user: all
    address: ::1/128
    auth_method: "{{ postgresql_auth_method }}"

# Debian only. Used to generate the locales used by PostgreSQL databases.
postgresql_locales:
  - en_US.UTF-8

# Databases to ensure exist.
postgresql_databases: []
# - name: exampledb # required; the rest are optional
#   lc_collate: # defaults to 'en_US.UTF-8'
#   lc_ctype: # defaults to 'en_US.UTF-8'
#   encoding: # defaults to 'UTF-8'
#   template: # defaults to 'template0'
#   login_host: # defaults to 'localhost'
#   login_password: # defaults to not set
#   login_user: # defaults to '{{ postgresql_user }}'
#   login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories
#   port: # defaults to not set
#   owner: # defaults to postgresql_user
#   state: # defaults to 'present'

# Users to ensure exist.
postgresql_users: []
# - name: jdoe #required; the rest are optional
#   password: # defaults to not set
#   encrypted: # defaults to not set
#   role_attr_flags: # defaults to not set
#   db: # defaults to not set
#   login_host: # defaults to 'localhost'
#   login_password: # defaults to not set
#   login_user: # defaults to '{{ postgresql_user }}'
#   login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories
#   port: # defaults to not set
#   state: # defaults to 'present'

# Privileges to configure
# see
# https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql_privs_module.html#ansible-collections-community-postgresql-postgresql-privs-module
postgresql_privs: []
# - db: exampledb # database (required)
#   roles: jdoe  # role(s) the privs apply to (required)
#   privs: # comma separated list of privileges - defaults to not set
#   type: # type of database object to set privileges on - defaults to not set
#   objs: # list of database objects to set privileges on - defaults to not set
#   schema: # defaults to not set
#   session_role: # defaults to not set
#   fail_on_role: # defaults to true
#   grant_option: # defaults to not set
#   target_roles: # defaults to not set
#   login_host: # defaults to 'localhost'
#   login_password: # defaults to not set
#   login_user: # defaults to '{{ postgresql_user }}'
#   login_unix_socket: # defaults to 1st of postgresql_unix_socket_directories
#   port: # defaults to not set
#   state: # defaults to 'present'

# Whether to output user data when managing users.
postgres_users_no_log: true

# Useful hints:
# To start replication
# Create replication user on master

# Dont forget to add rep_user to pg_hba.conf
# TYPE    DATABASE        USER          ADDRESS                 METHOD
# host    all             rep_user      10.203.0.0/16           scram-sha-256

# On PostgreSQL:
# CREATE ROLE rep_user WITH REPLICATION LOGIN PASSWORD 'superstrongpass123';
# GRANT CONNECT ON DATABASE <db_name> TO rep_user;
# GRANT USAGE ON SCHEMA public TO rep_user;
# GRANT SELECT ON ALL TABLES IN SCHEMA public TO rep_user;

# DO $$
# DECLARE
#    tables_list text;
# BEGIN
#    SELECT string_agg(quote_ident(schemaname) || '.' || quote_ident(tablename), ', ')
#    INTO tables_list
#    FROM pg_tables
#    WHERE schemaname = 'public'
#      AND tablename NOT IN ('null' ); -- Список исключаемых таблиц
#    IF tables_list IS NOT NULL THEN
#        EXECUTE 'CREATE PUBLICATION export_tables FOR TABLE ' || tables_list;
#    ELSE
#        RAISE NOTICE 'Нет таблиц для добавления в публикацию';
#    END IF;
# END $$;

# Migrate db schema to file
# sudo -u postgres pg_dump -s -d zabbix > schema_dump.sql
# Upload schema_dump.sql to secondary server
# CREATE DATABASE db_name;
# Put schema_dump.sql on it
# cat schema_dump.sql | sudo -u postgres psql db_name

# Create subscription on secondary server
# CREATE SUBSCRIPTION my_import CONNECTION 'host=[hostname] user=rep_user dbname=[db] password=superstrongpass123' PUBLICATION export_tables;
#
# -- Check pgsql replication status
# SELECT
#    srrelid::regclass AS table_name,
#    srsubstate AS state_code,
#    CASE srsubstate
#        WHEN 'i' THEN 'initialize'
#        WHEN 'd' THEN 'data copy'
#        WHEN 'f' THEN 'finished copy'
#        WHEN 's' THEN 'synchronized'
#        WHEN 'r' THEN 'ready (streaming)'
#    END AS state_description,
#    srsublsn AS lsn
# FROM pg_subscription_rel;
