postgresql.sh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 -q -y sudo postgresql
  18. else
  19. apt-get install -q -y sudo postgresql-client
  20. fi
  21. fi
  22. #postgres official repository
  23. ##TODO would newer versions work without systemd?
  24. if [ ."$database_repo" = ."official" ]; then
  25. verbose "Using official repos"
  26. echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' > /etc/apt/sources.list.d/pgdg.list
  27. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
  28. apt-get -q update && apt-get upgrade -y
  29. apt-get install -y postgresql
  30. fi
  31. #Add PostgreSQL and BDR REPO
  32. ##TODO would newer versions work without systemd?
  33. if [ ."$database_repo" = ."2ndquadrant" ]; then
  34. verbose "Using 2ndquadrant.com repos"
  35. echo 'deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main' >> /etc/apt/sources.list.d/postgresql.list
  36. echo 'deb http://packages.2ndquadrant.com/bdr/apt/ jessie-2ndquadrant main' >> /etc/apt/sources.list.d/2ndquadrant.list
  37. wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add -
  38. wget --quiet -O - http://packages.2ndquadrant.com/bdr/apt/AA7A6805.asc | apt-key add -
  39. apt-get -q update && apt-get upgrade -y
  40. apt-get install -y postgresql-bdr-9.4 postgresql-bdr-9.4-bdr-plugin postgresql-bdr-contrib-9.4
  41. fi
  42. #init.d
  43. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  44. /usr/sbin/service postgresql restart
  45. fi
  46. #install the database backup
  47. #cp backup/fusionpbx-backup /etc/cron.daily
  48. #cp backup/fusionpbx-maintenance /etc/cron.daily
  49. #chmod 755 /etc/cron.daily/fusionpbx-backup
  50. #chmod 755 /etc/cron.daily/fusionpbx-maintenance
  51. #sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-backup
  52. #sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-maintenance
  53. #move to /tmp to prevent a red herring error when running sudo with psql
  54. cwd=$(pwd)
  55. cd /tmp
  56. if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
  57. # add the databases, users and grant permissions to them
  58. sudo -u postgres psql -c "CREATE DATABASE fusionpbx;";
  59. sudo -u postgres psql -c "CREATE DATABASE freeswitch;";
  60. sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
  61. sudo -u postgres psql -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$password';"
  62. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
  63. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
  64. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"
  65. # ALTER USER fusionpbx WITH PASSWORD 'newpassword';
  66. fi
  67. cd $cwd
  68. #set the ip address
  69. #server_address=$(hostname -I)