postgresql-v5-install.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if [ "$VERBOSE" = "yes" ]; then set -x; STD=""; else STD="silent"; fi
  7. silent() { "$@" > /dev/null 2>&1; }
  8. if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi
  9. YW=$(echo "\033[33m")
  10. RD=$(echo "\033[01;31m")
  11. BL=$(echo "\033[36m")
  12. GN=$(echo "\033[1;92m")
  13. CL=$(echo "\033[m")
  14. RETRY_NUM=10
  15. RETRY_EVERY=3
  16. CM="${GN}✓${CL}"
  17. CROSS="${RD}✗${CL}"
  18. BFR="\\r\\033[K"
  19. HOLD="-"
  20. set -Eeuo pipefail
  21. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  22. function error_handler() {
  23. local exit_code="$?"
  24. local line_number="$1"
  25. local command="$2"
  26. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  27. echo -e "\n$error_message\n"
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. LANG=$(grep -v '^#' /etc/locale.gen | grep -o '^[^ ]*')
  45. update-locale LANG=$LANG
  46. echo "export LANG=$LANG" >> ~/.bashrc
  47. echo $tz > /etc/timezone
  48. ln -sf /usr/share/zoneinfo/$tz /etc/localtime
  49. for ((i=RETRY_NUM; i>0; i--)); do
  50. if [ "$(hostname -I)" != "" ]; then
  51. break
  52. fi
  53. echo 1>&2 -en "${CROSS}${RD} No Network! "
  54. sleep $RETRY_EVERY
  55. done
  56. if [ "$(hostname -I)" = "" ]; then
  57. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  58. echo -e " 🖧 Check Network Settings"
  59. exit 1
  60. fi
  61. msg_ok "Set up Container OS"
  62. msg_ok "Network Connected: ${BL}$(hostname -I)"
  63. set +e
  64. trap - ERR
  65. if ping -c 1 -W 1 1.1.1.1 &> /dev/null; then msg_ok "Internet Connected"; else
  66. msg_error "Internet NOT Connected"
  67. read -r -p "Would you like to continue anyway? <y/N> " prompt
  68. if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
  69. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  70. else
  71. echo -e " 🖧 Check Network Settings"
  72. exit 1
  73. fi
  74. fi
  75. RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
  76. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  77. set -e
  78. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  79. msg_info "Updating Container OS"
  80. $STD apt-get update
  81. $STD apt-get -y upgrade
  82. msg_ok "Updated Container OS"
  83. msg_info "Installing Dependencies"
  84. $STD apt-get install -y curl
  85. $STD apt-get install -y sudo
  86. $STD apt-get install -y mc
  87. $STD apt-get install -y gnupg
  88. msg_ok "Installed Dependencies"
  89. msg_info "Setting up PostgreSQL Repository"
  90. sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
  91. $STD apt-key add <(curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc)
  92. msg_ok "Setup PostgreSQL Repository"
  93. msg_info "Installing PostgreSQL"
  94. $STD apt-get update
  95. $STD apt-get install -y postgresql
  96. cat <<EOF >/etc/postgresql/15/main/pg_hba.conf
  97. # PostgreSQL Client Authentication Configuration File
  98. local all postgres peer
  99. # TYPE DATABASE USER ADDRESS METHOD
  100. # "local" is for Unix domain socket connections only
  101. local all all peer
  102. # IPv4 local connections:
  103. host all all 127.0.0.1/32 scram-sha-256
  104. host all all 0.0.0.0/24 md5
  105. # IPv6 local connections:
  106. host all all ::1/128 scram-sha-256
  107. host all all 0.0.0.0/0 md5
  108. # Allow replication connections from localhost, by a user with the
  109. # replication privilege.
  110. local replication all peer
  111. host replication all 127.0.0.1/32 scram-sha-256
  112. host replication all ::1/128 scram-sha-256
  113. EOF
  114. cat <<EOF >/etc/postgresql/15/main/postgresql.conf
  115. # -----------------------------
  116. # PostgreSQL configuration file
  117. # -----------------------------
  118. #------------------------------------------------------------------------------
  119. # FILE LOCATIONS
  120. #------------------------------------------------------------------------------
  121. data_directory = '/var/lib/postgresql/15/main'
  122. hba_file = '/etc/postgresql/15/main/pg_hba.conf'
  123. ident_file = '/etc/postgresql/15/main/pg_ident.conf'
  124. external_pid_file = '/var/run/postgresql/15-main.pid'
  125. #------------------------------------------------------------------------------
  126. # CONNECTIONS AND AUTHENTICATION
  127. #------------------------------------------------------------------------------
  128. # - Connection Settings -
  129. listen_addresses = '*'
  130. port = 5432
  131. max_connections = 100
  132. unix_socket_directories = '/var/run/postgresql'
  133. # - SSL -
  134. ssl = on
  135. ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
  136. ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'
  137. #------------------------------------------------------------------------------
  138. # RESOURCE USAGE (except WAL)
  139. #------------------------------------------------------------------------------
  140. shared_buffers = 128MB
  141. dynamic_shared_memory_type = posix
  142. #------------------------------------------------------------------------------
  143. # WRITE-AHEAD LOG
  144. #------------------------------------------------------------------------------
  145. max_wal_size = 1GB
  146. min_wal_size = 80MB
  147. #------------------------------------------------------------------------------
  148. # REPORTING AND LOGGING
  149. #------------------------------------------------------------------------------
  150. # - What to Log -
  151. log_line_prefix = '%m [%p] %q%u@%d '
  152. log_timezone = 'Etc/UTC'
  153. #------------------------------------------------------------------------------
  154. # PROCESS TITLE
  155. #------------------------------------------------------------------------------
  156. cluster_name = '15/main'
  157. #------------------------------------------------------------------------------
  158. # CLIENT CONNECTION DEFAULTS
  159. #------------------------------------------------------------------------------
  160. # - Locale and Formatting -
  161. datestyle = 'iso, mdy'
  162. timezone = 'Etc/UTC'
  163. lc_messages = 'C'
  164. lc_monetary = 'C'
  165. lc_numeric = 'C'
  166. lc_time = 'C'
  167. default_text_search_config = 'pg_catalog.english'
  168. #------------------------------------------------------------------------------
  169. # CONFIG FILE INCLUDES
  170. #------------------------------------------------------------------------------
  171. include_dir = 'conf.d'
  172. EOF
  173. sudo systemctl restart postgresql
  174. msg_ok "Installed PostgreSQL"
  175. read -r -p "Would you like to add Adminer? <y/N> " prompt
  176. if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
  177. msg_info "Installing Adminer"
  178. $STD apt install -y adminer
  179. $STD sudo a2enconf adminer
  180. $STD systemctl reload apache2
  181. msg_ok "Installed Adminer"
  182. fi
  183. echo "export TERM='xterm-256color'" >>/root/.bashrc
  184. echo -e "$APPLICATION LXC provided by https://tteck.github.io/Proxmox/\n" > /etc/motd
  185. chmod -x /etc/update-motd.d/*
  186. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  187. msg_info "Customizing Container"
  188. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  189. mkdir -p $(dirname $GETTY_OVERRIDE)
  190. cat <<EOF >$GETTY_OVERRIDE
  191. [Service]
  192. ExecStart=
  193. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  194. EOF
  195. systemctl daemon-reload
  196. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  197. msg_ok "Customized Container"
  198. fi
  199. if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  200. msg_info "Cleaning up"
  201. $STD apt-get autoremove
  202. $STD apt-get autoclean
  203. msg_ok "Cleaned"