postgresql.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/sh
  2. #move to script directory so all relative paths work
  3. cd "$(dirname "$0")"
  4. #includes
  5. . ./config.sh
  6. . ./colors.sh
  7. . ./environment.sh
  8. #send a message
  9. echo "Install PostgreSQL"
  10. #generate a random password
  11. password=$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | base64)
  12. #install message
  13. echo "Install PostgreSQL and create the database and users\n"
  14. #included in the distribution
  15. if [ ."$database_repo" = ."system" ]; then
  16. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  17. apt-get install -y sudo postgresql
  18. else
  19. apt-get install -y sudo postgresql-client
  20. fi
  21. fi
  22. #make sure keyrings directory exits
  23. mkdir /etc/apt/keyrings
  24. #postgres official repository
  25. if [ ."$database_repo" = ."official" ]; then
  26. apt install -y gpg
  27. sh -c 'echo "deb [signed-by=/etc/apt/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
  28. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/pgdg.gpg
  29. chmod 644 /etc/apt/keyrings/pgdg.gpg
  30. apt-get update && apt-get upgrade -y
  31. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  32. if [ ."$database_version" = ."latest" ]; then
  33. apt-get install -y sudo postgresql
  34. else
  35. apt-get install -y sudo postgresql-$database_version
  36. fi
  37. else
  38. apt-get install -y sudo postgresql-client
  39. fi
  40. fi
  41. #add PostgreSQL and 2ndquadrant repos
  42. if [ ."$database_repo" = ."2ndquadrant" ]; then
  43. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  44. apt install -y curl
  45. curl https://dl.2ndquadrant.com/default/release/get/deb | bash
  46. if [ ."$os_codename" = ."buster" ]; then
  47. sed -i /etc/apt/sources.list.d/2ndquadrant-dl-default-release.list -e 's#buster#stretch#g'
  48. fi
  49. if [ ."$os_codename" = ."bullseye" ]; then
  50. sed -i /etc/apt/sources.list.d/2ndquadrant-dl-default-release.list -e 's#bullseye#stretch#g'
  51. fi
  52. apt update
  53. apt-get install -y sudo postgresql-bdr-9.4 postgresql-bdr-9.4-bdr-plugin postgresql-bdr-contrib-9.4
  54. fi
  55. fi
  56. #install the database backup
  57. #cp backup/fusionpbx-backup /etc/cron.daily
  58. #cp backup/fusionpbx-maintenance /etc/cron.daily
  59. #chmod 755 /etc/cron.daily/fusionpbx-backup
  60. #chmod 755 /etc/cron.daily/fusionpbx-maintenance
  61. #sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-backup
  62. #sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-maintenance
  63. #initialize the database
  64. pg_createcluster $database_version main
  65. #replace scram-sha-256 with md5
  66. sed -i /etc/postgresql/$database_version/main/pg_hba.conf -e '/^#/!s/scram-sha-256/md5/g'
  67. #systemd
  68. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  69. systemctl daemon-reload
  70. systemctl restart postgresql
  71. fi
  72. #init.d
  73. #/usr/sbin/service postgresql restart
  74. #move to /tmp to prevent a red herring error when running sudo with psql
  75. cwd=$(pwd)
  76. cd /tmp
  77. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  78. #reload the config
  79. sudo -u postgres psql -c "SELECT pg_reload_conf();"
  80. #set client encoding
  81. sudo -u postgres psql -c "SET client_encoding = 'UTF8';";
  82. #add the database users and databases
  83. sudo -u postgres psql -c "CREATE DATABASE fusionpbx;";
  84. #add the users and grant permissions
  85. sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
  86. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
  87. #update the fusionpbx user password
  88. #ALTER USER fusionpbx WITH PASSWORD 'newpassword';
  89. fi
  90. cd $cwd
  91. #set the ip address
  92. #server_address=$(hostname -I)