postgresql-v5-install.sh 7.9 KB

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