A brief note on how to install and setup PostgreSQL for Ruby on Rails on Mac OS.
- Install PostgreSQL
- Install pgAdmin
As shown in the official Postgres download instructions here, there are few ways of installing PostgreSQL on Mac OS. Homebrew or Postgres.app are the common ones that often recommeded by other Mac users.
However, as I also use Linux and Windows machines for development, installing PostgreSQL using grapichal installer from EnterpriseDB would be a more widely used solution that keeps everything consistent across all my environment. This grapichal installer provides an easy and straightforward wizard to get Postgres installed with few simple clicks.
- Go to https://www.enterprisedb.com/software-downloads-postgres
- Select a version. (For example, the latest installer version is
- Click 'Mac OS X' to download for Mac.
- Install from the downloaded file
postgresql-9.4.0-1-osx.dmgjust like any other Mac installers.
- Follow through the installation wizard with the default options.
- Installing Stack Builder is optional and can be omitted.
Add to PATH
Locate where PostgreSQL's binary is. By default, it should be
/Library/PostgreSQL/9.4/bin/psql, where 9.4 is the PostgreSQL version number. Otherwise, use the following
findcommand to find the path.
sudo find / -name "psql"
~/.bash_profilewith following command.
Add the following line to
.bash_profileusing the PostgreSQL's binary path.
pgAdmin is the most popular and feature rich Open Source administration and development platform for PostgreSQL. It helps users manage PostgreSQL databases through graphical interfaces.
- Download Mac OS dmg installer from https://www.pgadmin.org/download/macos4.php.
- Install it (e.g. the latest is
Connect to server
- Open up pgAdmin III from the applications.
- The local DB should be already shown up in Obeject Browser -> Server Groups -> Servers -> PostgreSQL 9.4 (localhost:5432). If not, manually add a server pointing to
localhost:5432or the port number specified when installing PostgreSQL.
Install pg gem
- Open up a terminal window
Find out where
pg_configis using the command below. By default, it should be
/Library/PostgreSQL/9.4/bin/pg_configfor PostgreSQL 9.4.
sudo find / -name "pg_config"
Install the gem with
pg-configpath explicitly specified.
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.4/bin/pg_config
Update Ruby on Rails project
- Update 'Gemfile'. Replace
config/database.ymlfile and update it like the following:
Original (for SQLite):
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3
New (For PostgreSQL):
default: &default adapter: postgresql encoding: unicode pool: 5 host: localhost username: [Your username for DB] password: [Your password for DB] development: <<: *default database: [Your dev database name] # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: [Your test database name]
Create DB using PostgreSQL now.
rake db:create && rake db:migrate