Rails API backed by Postgres

I’ve recently decided to move my Rails API backend from MongoDB to PostgreSQL. Mongo served me very well during the early stages of development, when I was rapidly iterating the schema, but now that the app is being released, PostgreSQL is a much better option. It is way easier to administer (using a managed instance as described below), and it has the flexibility of JSON-type fields that allows me to easily model my data.

Here are the step for setting up a Rails-API project with a PostgreSQL.

Development

Install PostgreSQL using Homebrew and create a user for your application.

$: brew install postgresql
$: psql -d postgres
posgres=#: create role 'username' with createdb login password 'password';
posgres=#: ctrl+d

Follow the instructions from Homebrew to cause Postres to run at start up, otherwise run:

$: postgres -D /usr/local/var/postgres

Create the Rails-API project:

$: gem isntall rails-api
$: gem install pg
$: rails-api new app

Edit config/database.yml with username and password. And set the adapter to be ‘postgresql’. Then use Rails generators and ActiveModel as usual.

$: cd app
$: rails g model Organisation name:string
$: rake db:migrate
$: rake db:setup 

Production

Amazon Web Services makes it very easy to host Rails applications using OpsWorks and RDS.

Simply create a Postgres instance in RDS for use by your API. I’ve found it helpful (although I don’t believe it should be strictly necessary) to set the database name at creation time.

Then in OpsWorks create a Rails App Server Layer. Add an App with your API code and connect it to your RDS instance with appropriate authentication settings.

At the time of writing there is a bug in OpsWorks that requires you to set the database adapter explicitly at deploy time. In the OpsWorks console, when deploying the Rails app, add the following “Custom JSON”:

{"deploy":{"api":{"database":{"adapter":"postgresql","type":"postrgesql"}}}} 

Where api is the name you’ve given to your app in OpsWorks.