postgresql-install.sh 7.2 KB

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