deploy-database.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. #
  3. # Parameters
  4. #
  5. interactive=true
  6. if [ "$1" = "--no-interactive" ]; then
  7. interactive=false
  8. fi
  9. #
  10. # Enable and start Postgres
  11. #
  12. sudo systemctl start postgresql.service
  13. sudo systemctl enable postgresql.service
  14. #
  15. # Create databse and user
  16. #
  17. if [ "$interactive" = "true" ]; then
  18. sudo -u postgres -- createuser -P kemal
  19. sudo -u postgres -- createdb -O kemal invidious
  20. else
  21. # Generate a DB password
  22. if [ -z "$POSTGRES_PASS" ]; then
  23. echo "Generating database password"
  24. POSTGRES_PASS=$(tr -dc 'A-Za-z0-9.;!?{[()]}\\/' < /dev/urandom | head -c16)
  25. fi
  26. # hostname:port:database:username:password
  27. echo "Writing .pgpass"
  28. echo "127.0.0.1:*:invidious:kemal:${POSTGRES_PASS}" > "$HOME/.pgpass"
  29. sudo -u postgres -- psql -c "CREATE USER kemal WITH PASSWORD '$POSTGRES_PASS';"
  30. sudo -u postgres -- psql -c "CREATE DATABASE invidious WITH OWNER kemal;"
  31. sudo -u postgres -- psql -c "GRANT ALL ON DATABASE invidious TO kemal;"
  32. fi
  33. #
  34. # Instructions for modification of pg_hba.conf
  35. #
  36. if [ "$interactive" = "true" ]; then
  37. echo
  38. echo "-------------"
  39. echo " NOTICE "
  40. echo "-------------"
  41. echo
  42. echo "Make sure that your postgreSQL's pg_hba.conf file contains the follwong"
  43. echo "lines before previous 'host' configurations:"
  44. echo
  45. echo "host invidious kemal 127.0.0.1/32 md5"
  46. echo "host invidious kemal ::1/128 md5"
  47. echo
  48. fi