Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mac os x postgres install (homebrew option)
# Access tty to ask for confirmation even if we're in a pipe (thanks Pow)
TTY="/dev/$( ps -p$$ -o tty | tail -1 | awk '{print$1}' )"
read -p "*** Do you want to reinstall the 'pg' gem [y/n]?" REINSTALL_PG < $TTY
if [[ $REINSTALL_PG == "y" ]]; then
gem uninstall pg
gem install pg
fi
# Ask if the user wants to setup the db with a 'root' superuser?
read -p "*** Do you want to create a 'root' Postgres superuser [y/n]? " CREATE_ROOT < $TTY
if [[ $CREATE_ROOT == "y" ]]; then
createdb $(echo `whoami`) --owner=$(echo `whoami`)
createuser root --superuser
createdb root --owner=root
psql --username=root
fi
# Attempt to kill all other postgres processes
POSTGRES_PROCESS_COUNT=$(launchctl list | grep postgres | wc -l)
if [ $POSTGRES_PROCESS_COUNT != "0" ]
then
for X in `ps acx | grep -i postgres | awk {'print postgres'}`; do
sudo kill -9 $X;
done
launchctl remove org.postgresql.postgres
launchctl remove homebrew.mxcl.postgresql
rm ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
fi
# Access tty to ask for confirmation even if we're in a pipe (thanks Pow)
TTY="/dev/$( ps -p$$ -o tty | tail -1 | awk '{print$1}' )"
# Install/Reinstall Postgres
read -p "*** Do you want to reinstall PostgreSQL [y/n]? " REINSTALL_POSTGRES < $TTY
if [[ $REINSTALL_POSTGRES == "y" ]]; then
rm -rf /usr/local/var/postgres # Remove homebrew postgres db setup if it already exists
brew uninstall postgres
brew install postgres
fi
# Fix memory allocation issues for installing postgres via homebrew
sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216
# Start the Postgres Database Server
initdb /usr/local/var/postgres
# Auto-start postgres when computer starts
read -p "*** Do you want to start PostgreSQL when your computer starts [y/n]? " START_ON_BOOTUP < $TTY
if [[ $START_ON_BOOTUP == "y" ]]; then
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.1.3/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
fi
# Reinstall the 'pg' gem
read -p "*** Do you want to reinstall the 'pg' gem [y/n]?" REINSTALL_PG < $TTY
if [[ $REINSTALL_PG == "y" ]]; then
gem uninstall pg
gem install pg
fi
# Ask if the user wants to setup the db with a 'root' superuser?
read -p "*** Do you want to create a 'root' Postgres superuser [y/n]? " CREATE_ROOT < $TTY
if [[ $CREATE_ROOT == "y" ]]; then
createdb $(echo `whoami`) --owner=$(echo `whoami`)
createuser root --superuser
createdb root --owner=root
psql --username=root
fi
echo ''
echo 'Now you can run the following:'
echo ''
echo ' createdb $(echo `whoami`) --owner=$(echo `whoami`)'
echo ' createuser root --superuser'
echo ' createdb root --owner=root'
echo ' psql --username=root'
echo ''
echo 'If you are still having problems and you are running Mac OS X Lion, you may try running this command:'
echo ''
echo ' curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh'
echo ''
echo 'BE FOREWARNED! It is HIGHLY RECOMMENDED that you run this script only ONCE!'
echo 'It moves system files around to solve the problem.'
# Access tty to ask for confirmation even if we're in a pipe (thanks Pow)
TTY="/dev/$( ps -p$$ -o tty | tail -1 | awk '{print$1}' )"
# Fix memory allocation issues for installing postgres via homebrew
sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216
# Ask if the user wants to start postgres right away
echo "*** PostgreSQL Memory fixed."
read -p "*** Do you want to start PostgreSQL now? [y/n]? " START_POSTGRES < $TTY
if [[ $START_POSTGRES == "y" ]]; then
echo "*** Attempting to start PostgreSQL server..."
# Try to start postgres server right away
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.