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


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

  • 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):


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


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:

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 Your configuration should look something like this (for development, as well as, test and production databases):

  adapter: postgresql
  encoding: unicode
  database: <insert_your_desired_db_name_here>
  host: localhost

Now run:

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:

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

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