postgresql-install.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. source /dev/stdin <<< "$FUNCTIONS_FILE_PATH"
  7. color
  8. verb_ip6
  9. catch_errors
  10. setting_up_container
  11. network_check
  12. update_os
  13. msg_info "Installing Dependencies"
  14. $STD apt-get install -y curl
  15. $STD apt-get install -y sudo
  16. $STD apt-get install -y mc
  17. $STD apt-get install -y gnupg
  18. msg_ok "Installed Dependencies"
  19. msg_info "Setting up PostgreSQL Repository"
  20. sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
  21. $STD apt-key add <(curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc)
  22. msg_ok "Setup PostgreSQL Repository"
  23. msg_info "Installing PostgreSQL"
  24. $STD apt-get update
  25. $STD apt-get install -y postgresql
  26. cat <<EOF >/etc/postgresql/15/main/pg_hba.conf
  27. # PostgreSQL Client Authentication Configuration File
  28. local all postgres peer
  29. # TYPE DATABASE USER ADDRESS METHOD
  30. # "local" is for Unix domain socket connections only
  31. local all all peer
  32. # IPv4 local connections:
  33. host all all 127.0.0.1/32 scram-sha-256
  34. host all all 0.0.0.0/24 md5
  35. # IPv6 local connections:
  36. host all all ::1/128 scram-sha-256
  37. host all all 0.0.0.0/0 md5
  38. # Allow replication connections from localhost, by a user with the
  39. # replication privilege.
  40. local replication all peer
  41. host replication all 127.0.0.1/32 scram-sha-256
  42. host replication all ::1/128 scram-sha-256
  43. EOF
  44. cat <<EOF >/etc/postgresql/15/main/postgresql.conf
  45. # -----------------------------
  46. # PostgreSQL configuration file
  47. # -----------------------------
  48. #------------------------------------------------------------------------------
  49. # FILE LOCATIONS
  50. #------------------------------------------------------------------------------
  51. data_directory = '/var/lib/postgresql/15/main'
  52. hba_file = '/etc/postgresql/15/main/pg_hba.conf'
  53. ident_file = '/etc/postgresql/15/main/pg_ident.conf'
  54. external_pid_file = '/var/run/postgresql/15-main.pid'
  55. #------------------------------------------------------------------------------
  56. # CONNECTIONS AND AUTHENTICATION
  57. #------------------------------------------------------------------------------
  58. # - Connection Settings -
  59. listen_addresses = '*'
  60. port = 5432
  61. max_connections = 100
  62. unix_socket_directories = '/var/run/postgresql'
  63. # - SSL -
  64. ssl = on
  65. ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
  66. ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'
  67. #------------------------------------------------------------------------------
  68. # RESOURCE USAGE (except WAL)
  69. #------------------------------------------------------------------------------
  70. shared_buffers = 128MB
  71. dynamic_shared_memory_type = posix
  72. #------------------------------------------------------------------------------
  73. # WRITE-AHEAD LOG
  74. #------------------------------------------------------------------------------
  75. max_wal_size = 1GB
  76. min_wal_size = 80MB
  77. #------------------------------------------------------------------------------
  78. # REPORTING AND LOGGING
  79. #------------------------------------------------------------------------------
  80. # - What to Log -
  81. log_line_prefix = '%m [%p] %q%u@%d '
  82. log_timezone = 'Etc/UTC'
  83. #------------------------------------------------------------------------------
  84. # PROCESS TITLE
  85. #------------------------------------------------------------------------------
  86. cluster_name = '15/main'
  87. #------------------------------------------------------------------------------
  88. # CLIENT CONNECTION DEFAULTS
  89. #------------------------------------------------------------------------------
  90. # - Locale and Formatting -
  91. datestyle = 'iso, mdy'
  92. timezone = 'Etc/UTC'
  93. lc_messages = 'C'
  94. lc_monetary = 'C'
  95. lc_numeric = 'C'
  96. lc_time = 'C'
  97. default_text_search_config = 'pg_catalog.english'
  98. #------------------------------------------------------------------------------
  99. # CONFIG FILE INCLUDES
  100. #------------------------------------------------------------------------------
  101. include_dir = 'conf.d'
  102. EOF
  103. sudo systemctl restart postgresql
  104. msg_ok "Installed PostgreSQL"
  105. read -r -p "Would you like to add Adminer? <y/N> " prompt
  106. if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
  107. msg_info "Installing Adminer"
  108. $STD apt install -y adminer
  109. $STD sudo a2enconf adminer
  110. $STD systemctl reload apache2
  111. msg_ok "Installed Adminer"
  112. fi
  113. motd_ssh
  114. customize
  115. msg_info "Cleaning up"
  116. $STD apt-get autoremove
  117. $STD apt-get autoclean
  118. msg_ok "Cleaned"