start-database.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. # Use this script to start a docker container for a local development database
  3. # TO RUN ON WINDOWS:
  4. # 1. Install WSL (Windows Subsystem for Linux) - https://learn.microsoft.com/en-us/windows/wsl/install
  5. # 2. Install Docker Desktop or Podman Deskop
  6. # - Docker Desktop for Windows - https://docs.docker.com/docker-for-windows/install/
  7. # - Podman Desktop - https://podman.io/getting-started/installation
  8. # 3. Open WSL - `wsl`
  9. # 4. Run this script - `./start-database.sh`
  10. # On Linux and macOS you can run this script directly - `./start-database.sh`
  11. # import env variables from .env
  12. set -a
  13. source .env
  14. DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}')
  15. DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}')
  16. DB_NAME=$(echo "$DATABASE_URL" | awk -F'/' '{print $4}')
  17. DB_CONTAINER_NAME="$DB_NAME-postgres"
  18. if ! [ -x "$(command -v docker)" ] && ! [ -x "$(command -v podman)" ]; then
  19. echo -e "Docker or Podman is not installed. Please install docker or podman and try again.\nDocker install guide: https://docs.docker.com/engine/install/\nPodman install guide: https://podman.io/getting-started/installation"
  20. exit 1
  21. fi
  22. # determine which docker command to use
  23. if [ -x "$(command -v docker)" ]; then
  24. DOCKER_CMD="docker"
  25. elif [ -x "$(command -v podman)" ]; then
  26. DOCKER_CMD="podman"
  27. fi
  28. if ! $DOCKER_CMD info > /dev/null 2>&1; then
  29. echo "$DOCKER_CMD daemon is not running. Please start $DOCKER_CMD and try again."
  30. exit 1
  31. fi
  32. if command -v nc >/dev/null 2>&1; then
  33. if nc -z localhost "$DB_PORT" 2>/dev/null; then
  34. echo "Port $DB_PORT is already in use."
  35. exit 1
  36. fi
  37. else
  38. echo "Warning: Unable to check if port $DB_PORT is already in use (netcat not installed)"
  39. read -p "Do you want to continue anyway? [y/N]: " -r REPLY
  40. if ! [[ $REPLY =~ ^[Yy]$ ]]; then
  41. echo "Aborting."
  42. exit 1
  43. fi
  44. fi
  45. if [ "$($DOCKER_CMD ps -q -f name=$DB_CONTAINER_NAME)" ]; then
  46. echo "Database container '$DB_CONTAINER_NAME' already running"
  47. exit 0
  48. fi
  49. if [ "$($DOCKER_CMD ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then
  50. $DOCKER_CMD start "$DB_CONTAINER_NAME"
  51. echo "Existing database container '$DB_CONTAINER_NAME' started"
  52. exit 0
  53. fi
  54. if [ "$DB_PASSWORD" = "password" ]; then
  55. echo "You are using the default database password"
  56. read -p "Should we generate a random password for you? [y/N]: " -r REPLY
  57. if ! [[ $REPLY =~ ^[Yy]$ ]]; then
  58. echo "Please change the default password in the .env file and try again"
  59. exit 1
  60. fi
  61. # Generate a random URL-safe password
  62. DB_PASSWORD=$(openssl rand -base64 12 | tr '+/' '-_')
  63. if [[ "$(uname)" == "Darwin" ]]; then
  64. # macOS requires an empty string to be passed with the `i` flag
  65. sed -i '' "s#:password@#:$DB_PASSWORD@#" .env
  66. else
  67. sed -i "s#:password@#:$DB_PASSWORD@#" .env
  68. fi
  69. fi
  70. $DOCKER_CMD run -d \
  71. --name $DB_CONTAINER_NAME \
  72. -e POSTGRES_USER="postgres" \
  73. -e POSTGRES_PASSWORD="$DB_PASSWORD" \
  74. -e POSTGRES_DB="$DB_NAME" \
  75. -p "$DB_PORT":5432 \
  76. docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created"