Skip to content

Instantly share code, notes, and snippets.

@sebastianwebber
Created November 2, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sebastianwebber/dbf00307cc2100f59760a2de37bc2426 to your computer and use it in GitHub Desktop.
Save sebastianwebber/dbf00307cc2100f59760a2de37bc2426 to your computer and use it in GitHub Desktop.
a script to apply pgconfig using the api (useful for a ansible role/task)
#!/bin/bash
PGCONFIG_URL="https://api.pgconfig.org/v1/tuning/get-config"
DEFAULT_FORMAT="alter_system"
ENVIRONMENT="{{ postgresql_default_profile }}"
PG_VERSION="{{ postgresql_version }}"
TOTAL_RAM=$(({{ ansible_memtotal_mb }} /1024))
ADMIN_USER="postgres"
USE_RESTORE_TUNING="{{ apply_restore_tuning | default('NO') }}"
## alter system for versions greater or equal than 9.4
if [ $( echo "${PG_VERSION} >= 9.4" | bc) -eq 1 ]; then
curl "${PGCONFIG_URL}?enviroment_name=${ENVIRONMENT}&format=${DEFAULT_FORMAT}&include_pgbadger=true&log_format=stderr&max_connections=100&pg_version=${PG_VERSION}&total_ram=${TOTAL_RAM}GB" | psql -U "${ADMIN_USER}"
else
DEFAULT_FORMAT="conf"
curl "${PGCONFIG_URL}?enviroment_name=${ENVIRONMENT}&format=${DEFAULT_FORMAT}&include_pgbadger=true&log_format=stderr&max_connections=100&pg_version=${PG_VERSION}&total_ram=${TOTAL_RAM}GB" >> "{{ postgresql_data_dir}}/postgresql.conf"
fi
if [ "${USE_RESTORE_TUNING}" = "YES" ]; then
curl 'https://gist.githubusercontent.com/fabriziomello/dd0ad27fc66ea34474dfdbf918d6d230/raw/5b3a48e102f5e7fd901057bd459daa8a2cbd2b04/tuning-restore-postgres' >> "{{ postgresql_data_dir}}/postgresql.conf"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment