ParsaLabs | Blog

A publication about the web and more.

Installing PostgreSQL on Mac OS X, and Using It as Your Rails App's Database

| Comments

Installation:

The easiest way, I have found so far, to install PostgreSQL database on Mac OS X is to use the postgres app. Here are the steps:

  • Log on to http://postgresapp.com/.

  • Download the latest version.

  • Unarchive the downloaded zip file (usually by just double clicking on it). This will give you back the Postgres app.

  • Finally, drag and drop the Postgres app to your Applications folder.

Now, simply double click to launch the app (i.e. to run the world’s most advanced open source database on your Mac). It just couldn’t get any easier.

You should be able to see a tiny elephant icon in your menubar. That means your database is up and running.

Tip: I suggest setting up your database to run/start automatically at Login. To do that, click on the elephant icon in your menubar and select ‘Automatically Start at Login’.

Next, you are going to want to add the /bin directory that ships with Postgres app to your PATH. Open your .bash_profile (in your home directory) and add the following line to the bottom of it (if .bash_profile doesn’t exist, simply create it by running touch ~/.bash_profile):

1
PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"

Restart the terminal, and run which psql. You should get the following result:

1
/Applications/Postgres.app/Contents/MacOS/bin/psql

Working directly with the database:

Now if you type psql at a terminal prompt, you will be connected to the database server and will be able to interact with it through various commands. A list of all psql’s commands can be found in this tutorial.

Tip: running rails db command, in the context of your rails app, will do the same thing as psql.

Personally, my preferred way to explore the db structure, tables and data is through a GUI client. At the time of this writing, my favourite client is PG Commander, but other alternatives are pgAdmin and Induction app.

Using Postgres with your Rails app:

Alright, with Postgres app successfully setup, we can now create a new Rails app, and instruct it to use PostgreSQL as database:

1
rails new myapp -d postgresql

When the application is created, Bundler will install the pg gem. Looking at the database.yml file, you can see the application is already configured to use Postgres. However, you need to make some minor changes to make it work with postgres.app. Your configuration should look something like this (for development, as well as, test and production databases):

1
2
3
4
5
development:
  adapter: postgresql
  encoding: unicode
  database: <insert_your_desired_db_name_here>
  host: localhost

Now run:

1
rake db:create:all

at the root of your rails app to create the database.

To verify that your Rails app is able to connect to your newly created database, and is able to interact with it, we are going to create a post scaffold, run the migration, and check if we can create, retrieve, update and delete posts through the browser:

1
2
3
rails g scaffold post title:string body:text
rake db:migrate
rails server

Browse to http://0.0.0.0:3000/posts and try creating some posts. If you can retrieve, update and delete them successfully, you are all set!

Comments