Monday, April 12, 2010

Getting PostgreSQL to run on a Mac...

So I decided to switch to PostgreSQL from MySQL. First off, it's what's used by default at Heroku, and I'm so far loving Heroku, its great integration with git, and the resulting ease of deployment. Secondly, I'm a tad concerned about what might happen in the future since Oracle now owns MySQL: could be 'interesting'.

Anyway, I'd always heard that installing PostgreSQL was more challenging than installing MySQL: "MySQL put more effort into making things easier for first-time users" is what my friends have told me. But I also heard that PostgreSQL had better and more standard SQL support (for example, support for foreign keys).

Anyway, with a bit of searching, I finally found this RubyOnRails page which seemed to do a great job of laying things out for all 3 platforms. Seemed pretty straight-forward: 

Install the server:
sudo port install postgresql83-server

Start the server:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist

Setup the 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'

Two problems:
  1. It failed installing the database -- complaining that it couldn't find the header file libpq-fe.h. 
  2. It installed version 8.3 and version 8.4 is now current.

More searching led me to the MacPorts site which has the required library:
sudo port install libpqxx

However, it also wound up installing the 8.4 version of PostgreSQL, but version 8.3 still is the one that gets launched... seems to work, but certainly not as clean as I would like.

Given the problems I had in the past before I cleaned my machine and installed Ruby & Rails "from scratch" with MacPorts (see my previous post on this issue) as I could easily get PostgreSQL with relatively little hassle.

So far, so good...

PS Check out "Enterprise Rails" (Dan Chak, O'Reilly). He does a really nice job of discussing Rails' scaling and reliability issues and does it all in the context of PostgreSQL. Good book!

Monday, March 22, 2010

MacPorts headaches... solved!

So I finally found a way to solve all those MacPorts headaches, thanks to Erik Michaels-Ober, a friend of a friend. As I and many folks have learned, Mac OS X is less than standard *Nix when it comes to setting up their directories, and it doesn't take kindly to co-creation of a parallel directory tree. MacPorts puts things in the (*Nix) standard /opt directory, but once that's done, you get path and name clashes.

I've seen bits and pieces of a solution as noted in my previous posts, but they really didn't fix the problem. And of course, there's the issue that's been reported elsewhere that upgrading to Snow Leopard can wreak havoc on a development setup.

So following Eriks' instructions, here's what I did:
  1. I wiped my system clean and installed Snow Leopard from scratch. You may be able to just do an upgrade, but I chose to go with a clean system.
  2. Install the XCode Developer Tools that come with Snow Leopard (or download the latest version from the Apple website. Warning: it’s a big file).
  3. Remove all the pre-installed rubygems from your system. You can do this using the “gem uninstall” command, or brute force. I chose the brute force method, but only after creating a copy before I did the deletion, just in case. Once I knew it worked, I got rid of my copies.
    rm -rf /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/*

    rm -rf /Library/Ruby/Gems/1.8/*
  4. Install MacPorts (see the MacPorts installation page).
  5. Make sure you’re running MacPorts version 1.8.2 (type: port version). If not, run the command:
    sudo port selfupdate
  6. Install the MacPorts version of ruby itself:
    sudo port install ruby
  7. Install the MacPorts version of rubygems:
    sudo port install rb-rubygems
  8. Install the MacPorts version of git:
    sudo port install git-core
  9. Upgrade to the latest version of rubygems:
    sudo gem update --system
  10. Make sure you’re running rubygems version 1.3.5 (type: gem -v). If not, run the command: update_rubygems
  11. Install all the gems you want (rails, heroku, etc.). They will be installed into /opt/local/lib/ruby/gems/
I set up my machine about a month ago and it's been working fine ever since.

Enjoy...