Skip to content

Instantly share code, notes, and snippets.

@mroach
Last active October 21, 2016 03:57
Show Gist options
  • Save mroach/70bd4b42a2e307c158481f009124cbcb to your computer and use it in GitHub Desktop.
Save mroach/70bd4b42a2e307c158481f009124cbcb to your computer and use it in GitHub Desktop.
#!/bin/bash
# If your system crashes or shuts-down hard, the PostgreSQL PID file is left
# behind and then it won't startup when your system restarts. Run this script
# to check if it's ok to delete the PID file and start PostgreSQL
prefix=$(brew --prefix)
pid_path="$prefix/var/postgres/postmaster.pid"
if [ ! -f "$pid_path" ]; then
echo "πŸ€” No PID file exists at $pid_path. PostgreSQL was shut down properly?"
exit 1
fi
pid=$(head -n1 "$pid_path")
cmd_at_pid=$(ps $pid -co "command=")
if [ "$cmd_at_pid" == "postgres" ]; then
echo "⚠️ Postgres is running with PID $pid! Aborting"
exit 1
fi
echo "πŸ‘ The existing PID file is can be deleted"
read -p "⁉️ Delete $pid_path? [y/n]: " delete
if [ "$delete" != "y" ]; then
echo "πŸ‘‹ Ok, bye"
exit 0
fi
rm "$pid_path";
echo "πŸ€– Re-loading PostgreSQL..."
brew services start postgresql
echo "πŸŽ‰ It should be running now!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment