How to install PostgreSQL on a Mac with Homebrew


This is a quick guide for installing PostgreSQL (Postgres for short) on a Mac with Homebrew, and starting and stopping it with Homebrew Services. If you’re working on a Ruby on Rails project that requires Postgres, this tutorial will get you up and running in no time.

The easiest way to install Homebrew, Postgres, and all the other tools necessary for Ruby on Rails development, is to run the Ruby on Mac script that will set everything up for you.

If you prefer to do things manually, follow the steps below.

Step 1: Install Homebrew

Follow the instructions on their site.

Step 2: Update Homebrew

Before you install anything with Homebrew, you should always make sure it’s up to date and that it’s healthy:

$ brew update
$ brew doctor

If you already had Homebrew installed, and brew doctor is reporting errors that you can’t fix on your own, Ruby on Mac can automatically clean up your dev setup and reinstall everything from scratch.

Step 3: Install Postgres

$ brew install postgresql

When you install Postgres, Homebrew will provide useful information in your Terminal that you should read. Homebrew also helpfully creates a default database cluster. You can confirm that if you see the following output:

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres

Step 5: Start the Postgres service

$ brew services start postgresql

At this point, you should be all set to run the Rails commands to create and use the database in your app.

To stop Postgres:

$ brew services stop postgresql

To see a list of all services and their state:

$ brew services list