iie labs

Easiest Postgres Install Ever - Mac Edition

Posted almost 9 years ago by Philip Ingram

Well do I have a script for you. One of the best sources of ’nix based solutions for our Ruby & Rails world is wayneeseguin .

wayneeseguin (irc name) trolls plenty of irc channels but he also is known for a little thing called rvm , which is the EASIEST way to get multiple versions of ruby onto your mac/’nix based system.

He has also authored a script I have just used successfully to install postgres on Snow Leopard. Now I’ve tried 100 different blog posts and never had the luck; Plenty of pain, but no luck.

Get to the Easy Postgres Install Script

Geez, here it is && save this script below as postgres_install_script.sh (for those that are new to shell scripts).


#!/usr/bin/env bash

package=“postgresql”
version=“8.4.2”
source_path=“$HOME/.src”
prefix=“${prefix:-”/usr/local"}"
pgsql_path=“$HOME/.pgsql”
install_path=“$prefix/$package-$version”
user=“$(whoami)”
timestamp=“$(date +”%Y-%m-%dT%H:%M")"
color_green=“$(tput setaf 2)”
color_none=“$(tput sgr0)”

log() {
echo -e “${color_green}${1}${color_none}”
}

log “\n${package}-${version} installation\n”

echo -e "
Author: Wayne E. Seguin (wayneeseguin@gmail.com)
wayneeseguin in #rvm on irc.freenode.net

This script will install postgresql to /usr/local/postgresql and
configure your system for development as your local user.
"

if [[ “root” = “$(whoami)” ]] ; then
echo -e “\nThis script is meant to be run as a user not as root.\n”
exit 1
fi

log “\nDownloading postgresql…\n”
mkdir p $source_path ; cd $source_path
curl -O -L ftp://ftp5.us.postgresql.org/pub/PostgreSQL/v$version/$package
$version.tar.bz2

log “\nExtracting postgresql…\n”
tar jxf $package-$version.tar.bz2 && cd $package-$version/
cd $source_path/postgresql-$version

log “\nBuilding postgresql…\n”
if [[ “Darwin” = “$user” ]] ; then
./configure —prefix=$install_path —datadir=$pgsql_path —with-openssl —enable-shared CFLAGS=‘-O2 -pipe’ —with-bonjour
else
./configure —prefix=$install_path —datadir=$pgsql_path —with-openssl —enable-shared CFLAGS=‘-O2 -pipe’
fi
make -j3

log “\nInstalling postgresql to $install_path, you will be prompted for your password to run ‘make install’ as root…\n”
sudo make install

log “\nInitializing postgresql data directory in $pgsql_path/data with superuser set to ‘$user’…\n”
sudo chown -R $user $pgsql_path

$install_path/bin/initdb —pgdata=$pgsql_path/data —encoding=utf8 —username=$user

if [[ ! -s “$pgsql_path/data/postgresql.conf” ]] ; then
log “\nCopying over sample postgresql.conf to $pgsql_path/data/postgresql.conf…\n”
cp $pgsql_path/postgresql.conf.sample $pgsql_path/data/postgresql.conf
fi

log “\nSymlinking $install_path to $prefix/postgresql”
sudo rm -f $prefix/postgresql
sudo ln -nfs $install_path $prefix/postgresql

log “\nNow updating $HOME/.bash_profile with the postgres environment…\n”
if ! grep -q PGUSER $HOME/.bash_profile ; then
echo “export PGUSER=‘$user’” >> $HOME/.bash_profile.postgres
fi
if ! grep -q PGDATA $HOME/.bash_profile ; then
echo “export PGDATA=‘$pgsql_path/data’” >> $HOME/.bash_profile.postgres
fi
if ! grep -q PGLOG $HOME/.bash_profile ; then
echo “export PGLOG=‘$pgsql_path/server.log’” >> $HOME/.bash_profile.postgres
fi
if ! grep -q “$install_path” $HOME/.bash_profile ; then
echo “export PATH=\”$install_path/bin:\$PATH\"" >> $HOME/.bash_profile.postgres
fi

cat $HOME/.bash_profile >> $HOME/.bash_profile.postgres
mv $HOME/.bash_profile $HOME/.bash_profile.$timestamp
mv $HOME/.bash_profile.postgres $HOME/.bash_profile

log “\nNOTE: Your original .bash_profile has been backed up as $HOME/.bash_profile.$timestamp”

if [[ “Darwin” = “$(uname)” ]] ; then
log “Temporarily adjusting your shared memory settings, follow instructions below to make these permanent…\n”
sudo sysctl -w kern.sysv.shmmax=1610612736
sudo sysctl -w kern.sysv.shmall=393216
sudo sysctl -w kern.sysv.shmmin=1
sudo sysctl -w kern.sysv.shmmni=32
sudo sysctl -w kern.sysv.shmseg=8
sudo sysctl -w kern.maxprocperuid=1536
sudo sysctl -w kern.maxproc=2048
fi

echo "

Be sure that your shared memory settings are adjusted properly, the defaults are usually way too low.
For Example I have 8GB RAM running MacOSX and my /etc/sysctl.conf look like this:

∴ cat /etc/sysctl.conf

kern.sysv.shmmax=1610612736
kern.sysv.shmall=393216
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.maxprocperuid=1536
kern.maxproc=2048

Installation is now complete.

*postgresql will be installed to $install_path
*postgresql configuration and data files will be in $pgsql_path
*You can control the postgresql server via ‘pg_ctl {start|stop}’
*The ‘superuser’ for postgersql will be ‘$(whoami)’, no password.

Then please open a new shell or source your $HOME/.bash_profile and then run ‘pg_ctl start’ in order to run postgresql.

w00t,

~Wayne

"
echo ‘woot!’

exit 0

To Run the script, just go to the directory you saved the script and run


chmod 777 postgres_install_script.sh
./postgres_install_script.sh

You may be asked to add your sudo password during the process.

After Installation…

Last few things,

  • Adjust your /etc/sysctl.conf file – You’ll want this file to contain the permanent settings. Do so by:
    
    sudo vim /etc/sysctl.conf
    #Now edit your file, I have 4GB of ram and his settings are fine
    #I personally just had to change this line:
    kern.maxprocperuid=1536
    
  • Install the pg gem – This gem is what makes your rails app talk to Postgres. You’ll need to find out which arch flag to use however, before you install this gem.

Go to your terminal and type

uname -a

If the output is similar to below (notice the i386 bits)

Darwin macbook.local 10.2.0 Darwin Kernel Version 10.2.0: Tue Nov  3 10:37:10 PST 2009; root:xnu-1486.2.11~1/RELEASE_I386 i386

Then use:
env ARCHFLAGS='i386' gem install pg -- --with-pgsql-dir=/usr/local/postgresql/

Or if your uname says x86_64 or some other 64 bit shit that matches Snow Leopard, then issue the following command:

gem install pg -- --with-pgsql-dir=/usr/local/postgresql/

Tricks to using Postgres with Rails

Ok, maybe they aren’t tricks but here is a couple of things to get you going.

Create your rails app using -d postgresql . For example:


rails crazyapp -d postgresql

After it is created, you’ll need to remove two things from config/database.yml.

Remove the Username and password from all three sections of the file. No need for them anymore as this script has taken care of them.

When you get to production, you’ll need them, but you can find out how to set up users on postgres later, on your own.

With the file removed, you can now run


rake db:create:all

You should be good to go.

Thanks again to Wayne, a gentleman among boys, that man is.

Have fun with the script.

Comments

blog comments powered by Disqus