postgresql.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #install the dependencies
  8. pkg install --yes sudo
  9. #move to /tmp to prevent an error when running sudo with psql
  10. cwd=$(pwd)
  11. cd /tmp
  12. #send a message
  13. echo "Install PostgreSQL"
  14. #generate a random password
  15. password=$(cat /dev/random | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 20)
  16. #install message
  17. echo "Install PostgreSQL and create the database and users\n"
  18. #postgres install
  19. if [ ."$database_version" = ."16" ]; then
  20. echo "IGNORE_DEPENDS=postgresql15-client" >> /usr/local/etc/pkg.conf
  21. pkg install --yes postgresql16-server
  22. #cd /usr/ports/databases/postgresql16-server/ && make install clean BATCH=yes
  23. fi
  24. if [ ."$database_version" = ."15" ]; then
  25. pkg install --yes postgresql15-server
  26. #cd /usr/ports/databases/postgresql15-server/ && make install clean BATCH=yes
  27. fi
  28. if [ ."$database_version" = ."14" ]; then
  29. pkg install --yes postgresql14-server
  30. #cd /usr/ports/databases/postgresql14-server/ && make install clean BATCH=yes
  31. fi
  32. if [ ."$database_version" = ."13" ]; then
  33. pkg install --yes postgresql13-server
  34. #cd /usr/ports/databases/postgresql13-server/ && make install clean BATCH=yes
  35. fi
  36. if [ ."$database_version" = ."12" ]; then
  37. pkg install --yes postgresql12-server
  38. #cd /usr/ports/databases/postgresql12-server/ && make install clean BATCH=yes
  39. fi
  40. #enable postgres
  41. echo 'postgresql_enable=true' >> /etc/rc.conf
  42. #initialize the database
  43. /usr/local/etc/rc.d/postgresql initdb
  44. #start postgresql
  45. if [ ."$database_version" = ."16" ]; then
  46. sudo -u postgres /usr/local/bin/pg_ctl -D /var/db/postgres/data16 start
  47. fi
  48. if [ ."$database_version" = ."15" ]; then
  49. sudo -u postgres /usr/local/bin/pg_ctl -D /var/db/postgres/data15 start
  50. fi
  51. if [ ."$database_version" = ."14" ]; then
  52. sudo -u postgres /usr/local/bin/pg_ctl -D /var/db/postgres/data14 start
  53. fi
  54. if [ ."$database_version" = ."13" ]; then
  55. sudo -u postgres /usr/local/bin/pg_ctl -D /var/db/postgres/data13 start
  56. fi
  57. if [ ."$database_version" = ."12" ]; then
  58. sudo -u postgres /usr/local/bin/pg_ctl -D /var/db/postgres/data12 start
  59. fi
  60. #restart the service
  61. service postgresql restart
  62. #install the database backup
  63. #cp backup/fusionpbx-backup.sh /etc/cron.daily
  64. #chmod 755 /etc/cron.daily/fusionpbx-backup.sh
  65. #sed -i' ' -e "s/zzz/$password/g" /etc/cron.daily/fusionpbx-backup.sh
  66. #add the databases, users and grant permissions to them
  67. #sudo -u postgres psql -d fusionpbx -c "DROP SCHEMA public cascade;";
  68. #sudo -u postgres psql -d fusionpbx -c "CREATE SCHEMA public;";
  69. sudo -u postgres psql -c "CREATE DATABASE fusionpbx;";
  70. sudo -u postgres psql -c "CREATE DATABASE freeswitch;";
  71. sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
  72. sudo -u postgres psql -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$password';"
  73. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
  74. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
  75. sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"
  76. #ALTER USER fusionpbx WITH PASSWORD 'newpassword';