Installing Postgresql on OSX Lion
OSX Lion ships with the open source database, Postgresql, in place of the previous default that most developers have grown accept,m MySql. I don't know why Apple have made this choice to install the database as default. To be honest, I wish they wouldn't install the databases as default at all so we can choose our weapon of choice on a clean slate. With the rise of Macports and Homebrew, it is much easier to manage the installations and customised them to how we require them.
Even with the default installation many developers install another version alongside using Homebrew. For the sake of this article, I wanted to remove the default installation and install a single clean version on my development laptop using Homebrew.
A quick disclaimer: Check you current installation for data that may be lost. Make backups and if you are unsure, don't uninstall the default database.
Ok, so onto how I tackled the process.
Remove the default Postgresql installation
I used Frankie Inguanez's guide at http://bit.ly/W8zQWE
From the Terminal, run the command:
sudo /Library/PostgreSQL/9.1/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
Click Yes when asked if you want to uninstall Postgresql and all of it's modules
Remove the data files
sudo rm -rf /Library/PostgreSQL
and the the ini file
sudo rm /etc/postgres-reg.ini
Remove the PostgreSQL user
System Preferences -> Users & Groups
Unlock the settings panel by clicking on the padlock and enter your password
Select the PostgreSQL user and click on the minus button.
If you want, check to see if the Postgresql commands work
pqsl
This should return command cannot be found. Postgreql has now been completely removed.
Install new Postgresql
I use Homebrew as my package manager of choice. Before installing Postgresql, we want to ensure that Homebrew is completely up-to-date.
This is simple. Run:
sudo brew update
Postgresql can now be installed. I want to use the additional GIS features available with Postgresql and fortunately Homebrew has a postgis package that bundles all the dependancies we need.
sudo brew install postgis
If you only require Postgresql without the trimmings, you can run the following command instead:
sudo brew install postgresql
Once the script has successfully finished running, there will be some finishing up instructions to finalise the setup of the database. Before running the first initdb command, ensure the terminal is pointing to the correct, newly installed binary and not the old, default one by entering:
which psql
If the output shows /usr/bin/psql then the Terminal is looking at the old installation so we need to fix it so it is pointing to our new Homebrew installed version. To fix, simply open up the .bash_profile and export the PATH as follows:
Open ~/.bash_profile with your preferred editor. I use vim:
vim ~/.bash_profile
Add the following path and save/quit:
export PATH=/usr/local/pgsql/bin:$PATH
Reload the bash_profile
source ~/.bash_profile
Recheck the installation with which psql. It should show similar to the path you have just entered. If it does, carry on with initdb finishing up instructions.














