postgresql.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. apt-get install -y sudo postgresql
  17. fi
  18. #postgres official repository
  19. if [ ."$database_repo" = ."official" ]; then
  20. 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'
  21. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/pgdg.gpg
  22. apt-get update && apt-get upgrade -y
  23. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  24. if [ ."$database_version" = ."latest" ]; then
  25. apt-get install -y sudo postgresql
  26. else
  27. apt-get install -y sudo postgresql-$database_version
  28. fi
  29. fi
  30. fi
  31. #add PostgreSQL and 2ndquadrant repos
  32. if [ ."$database_repo" = ."2ndquadrant" ]; then
  33. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  34. apt install -y curl
  35. curl https://dl.2ndquadrant.com/default/release/get/deb | bash
  36. if [ ."$os_codename" = ."focal" ]; then
  37. sed -i /etc/apt/sources.list.d/2ndquadrant-dl-default-release.list -e 's#focal#bionic#g'
  38. fi
  39. apt update
  40. apt-get install -y sudo postgresql-bdr-9.4 postgresql-bdr-9.4-bdr-plugin postgresql-bdr-contrib-9.4
  41. fi
  42. fi
  43. #add additional dependencies
  44. apt install -y libpq-dev
  45. #systemd
  46. systemctl daemon-reload
  47. systemctl restart postgresql
  48. #init.d
  49. #/usr/sbin/service postgresql restart
  50. #install the database backup
  51. #cp backup/fusionpbx-backup /etc/cron.daily
  52. #cp backup/fusionpbx-maintenance /etc/cron.daily
  53. #chmod 755 /etc/cron.daily/fusionpbx-backup
  54. #chmod 755 /etc/cron.daily/fusionpbx-maintenance
  55. #sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-backup
  56. #sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-maintenance
  57. #move to /tmp to prevent a red herring error when running sudo with psql
  58. cwd=$(pwd)
  59. cd /tmp
  60. #add the databases, users and grant permissions to them
  61. sudo -u postgres psql -c "CREATE DATABASE fusionpbx;";
  62. sudo -u postgres psql -c "CREATE DATABASE freeswitch;";
  63. sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
  64. sudo -u postgres psql -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$password';"
  65. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
  66. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
  67. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"
  68. #ALTER USER fusionpbx WITH PASSWORD 'newpassword';
  69. cd $cwd
  70. #set the ip address
  71. #server_address=$(hostname -I)