System Specs:
The information provided to setup Ruby on Rails with PostgreSQL is based off the below specs. While these console commands may be similar for other versions Ruby and macOS some tweaking and additional googling might be required.
- macOS 10.15 Catalina
- Ruby 2.6.1
- Rails 6.0.3.4
- Homebrew
Installing PostgreSQL via Homebrew:
Let’s begin by installing PostgreSQL on our local machine. Run the following command with homebrew:
brew install postgresql
With PostgreSQL installed we can move on to creating a new rails app.
Creating A New Rails Project:
The command to make a new Rails app with PostgreSQL as the default database should be written like this.
rails new [app-name] --database=postgresql
So for my example postgres-selection rails project the create new command is written out and can be shortened to the following:
rails new postgres-selection -d postgresql
When Rails finishes initializing the project you then need to move into the project directory. Since I named my project postgres-selection I run the following command:
cd postgres-selection
Now open your project files in your chosen editor. VS Code and Atom are two popular editors and their commands are listed here.
VS Code: code .
Atom: atom .
Now that you’re in your project directory you need to run a bundle install to make sure all the required gems are installed.
bundle install
In order to check your rails server and database setup you’ll need to run the following command to create the empty database:
rake db:create
If you’ve made it this far you’re one step away from confirming that your PostgreSQL database is setup. Run the command below and load up localhost:3000. There are a few easy errors you may hit and I’ve tried to collect them towards the end of this article. If you run into any errors not referenced here when starting the server a quick google search will be your friend.
rails server
Benefits of PostgreSQL over SQLite3:
DigitalOcean.com has a great overview of the benefits and disadvantages of the MySQL, PostgreSQL, and SQLite3 relational database management systems (RDBMSs).
Common Errors:
Error : PG::ConnectionBad: could not connect to server: No such file or director
Try: brew postgresql-upgrade-database