postgresql-install.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. source /dev/stdin <<< "$FUNCTIONS_FILE_PATH"
  7. color
  8. verb_ip6
  9. catch_errors
  10. setting_up_container
  11. network_check
  12. update_os
  13. msg_info "Installing Dependencies"
  14. $STD apt-get install -y curl
  15. $STD apt-get install -y sudo
  16. $STD apt-get install -y mc
  17. $STD apt-get install -y gnupg
  18. msg_ok "Installed Dependencies"
  19. msg_info "Setting up PostgreSQL Repository"
  20. VERSION="$(awk -F'=' '/^VERSION_CODENAME=/{ print $NF }' /etc/os-release)"
  21. echo "deb http://apt.postgresql.org/pub/repos/apt ${VERSION}-pgdg main" >/etc/apt/sources.list.d/pgdg.list
  22. curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor --output /etc/apt/trusted.gpg.d/postgresql.gpg
  23. msg_ok "Setup PostgreSQL Repository"
  24. msg_info "Installing PostgreSQL"
  25. $STD apt-get update
  26. $STD apt-get install -y postgresql
  27. cat <<EOF >/etc/postgresql/16/main/pg_hba.conf
  28. # PostgreSQL Client Authentication Configuration File
  29. local all postgres peer
  30. # TYPE DATABASE USER ADDRESS METHOD
  31. # "local" is for Unix domain socket connections only
  32. local all all peer
  33. # IPv4 local connections:
  34. host all all 127.0.0.1/32 scram-sha-256
  35. host all all 0.0.0.0/24 md5
  36. # IPv6 local connections:
  37. host all all ::1/128 scram-sha-256
  38. host all all 0.0.0.0/0 md5
  39. # Allow replication connections from localhost, by a user with the
  40. # replication privilege.
  41. local replication all peer
  42. host replication all 127.0.0.1/32 scram-sha-256
  43. host replication all ::1/128 scram-sha-256
  44. EOF
  45. cat <<EOF >/etc/postgresql/16/main/postgresql.conf
  46. # -----------------------------
  47. # PostgreSQL configuration file
  48. # -----------------------------
  49. #------------------------------------------------------------------------------
  50. # FILE LOCATIONS
  51. #------------------------------------------------------------------------------
  52. data_directory = '/var/lib/postgresql/16/main'
  53. hba_file = '/etc/postgresql/16/main/pg_hba.conf'
  54. ident_file = '/etc/postgresql/16/main/pg_ident.conf'
  55. external_pid_file = '/var/run/postgresql/16-main.pid'
  56. #------------------------------------------------------------------------------
  57. # CONNECTIONS AND AUTHENTICATION
  58. #------------------------------------------------------------------------------
  59. # - Connection Settings -
  60. listen_addresses = '*'
  61. port = 5432
  62. max_connections = 100
  63. unix_socket_directories = '/var/run/postgresql'
  64. # - SSL -
  65. ssl = on
  66. ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
  67. ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'
  68. #------------------------------------------------------------------------------
  69. # RESOURCE USAGE (except WAL)
  70. #------------------------------------------------------------------------------
  71. shared_buffers = 128MB
  72. dynamic_shared_memory_type = posix
  73. #------------------------------------------------------------------------------
  74. # WRITE-AHEAD LOG
  75. #------------------------------------------------------------------------------
  76. max_wal_size = 1GB
  77. min_wal_size = 80MB
  78. #------------------------------------------------------------------------------
  79. # REPORTING AND LOGGING
  80. #------------------------------------------------------------------------------
  81. # - What to Log -
  82. log_line_prefix = '%m [%p] %q%u@%d '
  83. log_timezone = 'Etc/UTC'
  84. #------------------------------------------------------------------------------
  85. # PROCESS TITLE
  86. #------------------------------------------------------------------------------
  87. cluster_name = '16/main'
  88. #------------------------------------------------------------------------------
  89. # CLIENT CONNECTION DEFAULTS
  90. #------------------------------------------------------------------------------
  91. # - Locale and Formatting -
  92. datestyle = 'iso, mdy'
  93. timezone = 'Etc/UTC'
  94. lc_messages = 'C'
  95. lc_monetary = 'C'
  96. lc_numeric = 'C'
  97. lc_time = 'C'
  98. default_text_search_config = 'pg_catalog.english'
  99. #------------------------------------------------------------------------------
  100. # CONFIG FILE INCLUDES
  101. #------------------------------------------------------------------------------
  102. include_dir = 'conf.d'
  103. EOF
  104. sudo systemctl restart postgresql
  105. msg_ok "Installed PostgreSQL"
  106. read -r -p "Would you like to add Adminer? <y/N> " prompt
  107. if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
  108. msg_info "Installing Adminer"
  109. $STD apt install -y adminer
  110. $STD a2enconf adminer
  111. systemctl reload apache2
  112. msg_ok "Installed Adminer"
  113. fi
  114. motd_ssh
  115. customize
  116. msg_info "Cleaning up"
  117. $STD apt-get autoremove
  118. $STD apt-get autoclean
  119. msg_ok "Cleaned"