Postgres on Rails

Posted by Elliott Golden
header line
I have been wanting to give Heroku a try for a while. Their platform uses Postgres for data persistence. This was the motivation I needed to finally set up the Postrgres database on my Snow Leopard dev box.

The Postgres one-click installer makes things pretty simple, but I thought a record of the steps I took may prove useful in the future.

To begin, I grabbed the OS X version of the Postgres one-click installer for v.9.0.1-1. Once it downloaded and the .dmg mounted, I just ran the installer. It prompted me with a few options; I just stuck with the defaults and didn't opt for any of the post install apps that were offered.

Once everything was installed, I could open up the DB's GUI client (pgAdmin3) from /Library/PostgreSQL/9.0/pgAdmin3 and login as the default postgres user that the installer created. Depending on how you ran through the installer prompts, your postgres user may or may not have a password. Playing around with the GUI was proof that the DB was now setup. 

Rails requires a database adaptor Gem to be installed so that ActiveRecord can interface with Postgres. There are several available, but pg seems to be the library of choice. This gem and the included Postgres command line tools require access to the Postgres binary (The adaptor requires the binary so that native extensions can be built out during installation). To facilitate these requirements, my solution was to add the Postgres binary to the shell's $PATH variable. I ran which postgres to get the binary's location, then prepended the result to $PATH from within Bash's .profile. 

export PATH="/Library/PostgreSQL/9.0/bin:$PATH"
 

All that was left to do was set up a fresh Rails 3 app configured for Postgres integration.

rails new simpleapp -d postgresql
cd simpleapp
bundle install

After the app was created and the pg gem installed, the freshly generated database.yml needed editing. I used the default postgres user for username (another user can be created and used if desired) and set its password value accordingly. Finally, I fired off rake db:create:all to create the Postgres dbs for my new app, and I was ready to go.

The pgAdmin3 GUI is great, however if you care to use some of the command line tools the installer provides, you'll need to get down with their syntax and relation to Postgres centric users. Postgres and Rails – for MySQL people by Mike Williamson proved to be very useful in this department.
Comments
Showing 1 Post


top All content in this site is copyright protected by either Simple Circle llc or its respective clients.   © 1999-2010
Validate XHTML