postgresql-v5-install.sh 7.5 KB

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