postgresql.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #send a message
  8. verbose "Installing PostgreSQL"
  9. #generate a random password
  10. password=$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | base64)
  11. # Install the repository
  12. sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  13. # Install PostgreSQL:
  14. sudo yum install -y postgresql14-server postgresql14-contrib postgresql14 postgresql14-libs
  15. #send a message
  16. verbose "Initalize PostgreSQL database"
  17. #initialize the database
  18. sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
  19. sudo systemctl enable postgresql-14
  20. sudo systemctl start postgresql-14
  21. #allow loopback
  22. sed -i 's/\(host *all *all *127.0.0.1\/32 *\)ident/\1md5/' /var/lib/pgsql/14/data/pg_hba.conf
  23. sed -i 's/\(host *all *all *::1\/128 *\)ident/\1md5/' /var/lib/pgsql/14/data/pg_hba.conf
  24. #systemd
  25. systemctl daemon-reload
  26. systemctl restart postgresql-14
  27. #move to /tmp to prevent a red herring error when running sudo with psql
  28. cwd=$(pwd)
  29. cd /tmp
  30. #add the databases, users and grant permissions to them
  31. sudo -u postgres /usr/bin/psql -d fusionpbx -c "DROP SCHEMA public cascade;";
  32. sudo -u postgres /usr/bin/psql -d fusionpbx -c "CREATE SCHEMA public;";
  33. sudo -u postgres /usr/bin/psql -c "CREATE DATABASE fusionpbx";
  34. sudo -u postgres /usr/bin/psql -c "CREATE DATABASE freeswitch";
  35. sudo -u postgres /usr/bin/psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
  36. sudo -u postgres /usr/bin/psql -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$password';"
  37. sudo -u postgres /usr/bin/psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
  38. sudo -u postgres /usr/bin/psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
  39. sudo -u postgres /usr/bin/psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"
  40. #ALTER USER fusionpbx WITH PASSWORD 'newpassword';
  41. cd $cwd
  42. #send a message
  43. verbose "PostgreSQL installed"