pod-create-database-and-user.sh 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. POD_DB_USER='pod_user'
  3. POD_DB_USER_PASSWORD='pod_user_password'
  4. POD_DB_NAME='pod'
  5. # DB_HOST='127.0.0.1'
  6. # DB_PORT='5432'
  7. BUILD_DB_SQL="
  8. CREATE USER ${POD_DB_USER} WITH PASSWORD '${POD_DB_USER_PASSWORD}';
  9. CREATE DATABASE ${POD_DB_NAME};
  10. GRANT ALL PRIVILEGES ON DATABASE ${POD_DB_NAME} to ${POD_DB_USER};
  11. "
  12. if [ `whoami` != 'postgres' ]; then
  13. echo "This script is intended to be executed as postgres user."
  14. exit 1
  15. fi
  16. echo "About to create a new database and user:"
  17. echo "- database: ${POD_DB_NAME}"
  18. echo "- user: ${POD_DB_USER}"
  19. echo "- password: xxxxxx"
  20. echo
  21. echo "Sleeping 10 seconds."
  22. echo "- stop process by CTRL+C if in doubt."
  23. echo "..."
  24. sleep 10
  25. echo ${BUILD_DB_SQL} | psql
  26. #  -h ${DB_HOST} -p ${DB_PORT}
  27. # IF AN ERROR OCCURS, YOU CAn SIMPLY RESET YOUR POSTGRES SERVER
  28. # WITH THE FOLLOWING SQL COMMANDS (EXECUTED AS postgres USER):
  29. #
  30. # DROP DATABASE pod
  31. # DROP OWNED BY pod_master;
  32. # DROP USER pod_master;
  33. #
  34. # or more faster:
  35. #
  36. # psql -c "DROP DATABASE pod;"
  37. # psql -c "DROP OWNED BY pod_master;"
  38. # psql -c "DROP USER pod_master;"
  39. echo
  40. echo "You can now init schema and data by running the following command:"
  41. echo
  42. echo "psql -h 127.0.0.1 -U ${POD_DB_USER} -W ${POD_DB_NAME} < pod-init-database.sql"
  43. echo
  44. echo "note that you'll be asked for the password"