24th
Lessons Learned PostgreSQL on Mac OS X
Like many Rails shops out there, we are moving all our servers into the cloud. A few legacy projects, and certainly all new ones. Our cloud of choice: Heroku. I’ve been a Heroku fan since the first time I tried deploying an app on their system… and it just worked. They’ve been a fantastic solution, with almost no hiccups, until this past week when I ran into problems with the inconsistencies between MySQL (on my machine) and PostgreSQL (on Heroku).
Here are some of the lessons learned while trying to install PostgreSQL 8.3.x on my Mac OS X 10.5.8 machine:
Out of the Box
I went for the low hanging fruit and tried using http://www.postgresqlformac.com/. Everything seemed to run smoothly, no errors, no warnings. But there was also no confirmation, no clear next step, no information to test the installation and get started with postgresql. For all I know, I had PostgreSQL up and running on my machine, but because there was no documentation on how to ‘get started’, I had no clue what to do next.
To the wonderful people at postgresqlformac.com: your product may be wonderful, but self installers are geared towards postgres noobs (like myself) so treat me like a noob and walk me through starting/stopping the server, creating a db, etc…
Back in the Box
I had to follow (roughly) this shell script (http://www.postgresqlformac.com/news/archived_news/uninstall_835.html) to try to uninstall the system. The only thing that wasn’t working was getting rid of the ‘postgres’ user the system had generated. This wasn’t an apparent problem until I tried the next method of installing (below), which called for me to add the ‘postgres’ user through System Preferences. Despite not having a ‘postgres’ user in the account list, the system was prompting me with “name is used by another user”. So apparently there was some kind of hidden user that OS X was not picking up on.
Quick fast forward to the solution: http://www.oreillynet.com/mac/blog/2006/04/deleting_mac_os_x_users_remote.html
I had to use the ‘dscl’ command line tool to remove the postgres user and group.
By the Book
For attempt number 2, I decided to follow the official Apple Developer documentation: http://developer.apple.com/internet/opensource/postgres.html.
Everything went smoothly and now i can start up a postgresql server with relative ease from terminal:
$ su postgres
$ cd ~
$ export PATH=$PATH:/usr/local/pgsql/bin
$ pg_ctl -D /usr/local/pgsql/data -l postgres_logfile start
Or without a log
$ pg_ctl -D /usr/local/pgsql/data start
Look at all those happy processes:
$ ps waux | grep postgres
And stop it:
$ pg_ctl -D /usr/local/pgsql/data stop
Creating a database
$ createdb railsapp_development
Gem Please
I must have lucked out big time because all i had to do was run:
$ sudo gem install pg
And I got “Successfully installed pg-0.9.0”. I don’t think that has ever happened on any complex gem issue for me before.
Rails Now Too
Gemfile:
gem ‘pg’
A little bit of this:
$ bundle install
database.yml (using the same ‘postgres’ user and password i created in System Preferences)
development:
adapter: postgresql
database: application_development
username: postgres
password: secret
And whadya know:
$ rake db:migrate
It works! Now all I’ve got to do is repopulate the database.