postgresql-v5-install.sh 7.7 KB

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