Translate

April 2, 2013

How to Install PostgreSQL on a Mac

This is a quick guide for installing PostgreSQL (Postgres) on a Mac with Homebrew. If you are planing to use Postgres in your Ruby on Rails App, this tutorial will get you up and running in no time.
I am assuming that you have Homebrew, Ruby on Rails, Xcode, git, rvm etc installed in your mac.

Step 1: Update Homebrew
Before you install anything with Homebrew, you should always make sure it's up to date and that it's healthy by executing following command
brew update
brew doctor

Step 2: Install Postgres

brew install postgresql

When you install Postgres, you will see a bunch of output in your Terminal. There are few instructions like, creating first database, migrating existing database, start/stop PostgreSQL etc. Keep this instructions aside for future reference if you want.

Step 3: Create/Upgrade a database
If this is fresh installation of Postgres with Homebrew, you’ll need to create a database with:

initdb /usr/local/var/postgres -E utf8

I copied this from Terminal output.

Step 4: Create a user
If you want a new user for your new rails app, you can create new user by using createuser command. This will ask few questions.

createuser username 

Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n

Step 5: Create a database for new user
Create the two databases you will need, development and test. With option -O we can specify the owner of the database.
createdb -Ousername -Eutf8 newapp_development
createdb -Ousername -Eutf8 newapp_test

Done.....
To Verify the installation and database users we can use shell similar to MySQL
psql -U username newapp_development

You will get following prompt

newapp_development=>

Type "help" for help.
Post a Comment