Good news! Whether you are using PostgreSQL for general development, or need a version to match your setup on Heroku, getting PostgreSQL 8.3 installed on Snow Leopard is fairly straight forward. However, you’ll want to make some changes so that it works right for you.
Installing PostgreSQL 8.3
First, you’ll need to install Xcode if you haven’t already. This is available on the Snow Leopard DVD in the Optional Installs directory.
Second, if you aren’t already using it, download Mac Ports for Snow Leopard and install it. Mac Ports has come a long way in the last few years and will make your life much easier.
Once those are installed, run the following command:
sudo port install postgresql83 postgresql83-server
Setup Your First Database
At the very end of the install it tells you how to setup your first database:
sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'
You’ll also want to setup Postgres to auto-run as a server on start up.
sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist
If you want to start it right now, you can either reboot or do the following:
sudo su postgres -c '/opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb'
Make psql Available from the Command Line
The executable files for PostgreSQL get shoved into a non-standard place (just like MySQL), so you’ll need to edit the default profile.
sudo vi /etc/profile
You can also do this using
sudo mate /etc/profile if you aren’t comfortable in VI.
PATH= line needs to be changed to include the PostgreSQL bin directory.
PATH="/opt/local/bin:$PATH" and is now:
If you open a new terminal window you can now type
psql and it will find it.
Create a New User and Database
By default, PostgreSQL creates a postgres user for you. However, it’s not good practice to use the default and it’s a pain in the ass. Let’s just create a new database user to make it easier.
createuser --superuser macusername -U postgres
You need to change
macusername to your mac username. This will make your life ALOT easier. Trust me here.
Next, create your database:
Installing the PostgreSQL Ruby Gem
Unlike the MySQL driver, we don’t need to pass the ARCHFLAGS variable as 64 bit. PostgreSQL comes with both 32 and 64-bit versions. Yeah!
sudo gem install postgres-pr
Per Tom’s comment below, we should be using the native driver for better performance.
sudo env ARCHFLAGS="-arch x86_64" gem install pg
Configuring your Rails Application
Inside your Ruby on Rails application, open up config/database.yml and change your development adapter to be similar to the following:
You can change
defaultdb to the name you need for your application.