Before you go, check out these stories!

Hackernoon logoHow To Connect Ruby on Rails API, PostgreSQL And Heroku by@ermin

How To Connect Ruby on Rails API, PostgreSQL And Heroku

Author profile picture

@erminErmin Cahtarevic

Full Stack Developer

Creating a new Rails app is super easy by itself. And, creating a Rails app set up as an API is not much different.

Because we are planning on deploying our app to Heroku we need to set it up with PostgreSQL as the database.

The default database in Rails is SQLite, but it is not supported by Heroku

Let’s start!

1) Make sure your Rails version is up to date

Run the following line in your terminal to update Rails

gem update rails

and then this line to get the current version of Rails

rails -v

2) Create the Rails API with PostgreSQL

rails new <<APP_NAME>> --api --database=postgresql
Replace the 
 with the actual name of your app
  • rails new  create a new Rails app
  • - -api — initialize the new app as an API. This means the newly created app will not have any views. And the ApplicationController will inherit from ActionController::API instead of ActionController::Base
  • -- database=postgresql — And, as you can probably guess, this last part sets up the app to use PostgreSQL as the database

3) Create a new GitHub repository for your app

Keep track of the development of your API by storing your code on GitHub.

Check this link to see how to add an existing project to a Github repository.

4) Sign up for free on Heroku

Follow this link to the Heroku Sign up page.

5) Install Heroku CLI

Next, we will need to install the Heroku CLI to be able to deploy our little project.

Check this link to see how to install the Heroku CLI.

Once installed the

command is available from your terminal.

6) Log in using your Heroku account details

Run the following command in your terminal and enter your Heroku credentials when prompted

heroku login
Press Enter at the prompt to upload your existing ssh key or create a new one, used for pushing code later on

7) Create a new Heroku app

Run the following command in your terminal to create a new app on Heroku

heroku create

After that, we are all set up to deploy our app to Heroku!

8) Deploy the project

We are finally here!

Run the following commands in your terminal to deploy your app to Heroku

git add .
git commit -m "Deploy my super awesome Rails API"

git push heroku master

Side note — if you are working on a different branch from the master branch. You can run the following command to deploy your code

For example, if you are working on the 


git push heroku feature-branch:master

9) Our app is deployed!

That’s it, our Rails API is now live!

It’s an API-only application, it’s using PostgreSQL as the database and it’s deployed to Heroku.

Congratulate yourself. And then, get back to work, you have only begun.

Thanks for reading. And hopefully, this article helps you out.


Join Hacker Noon

Create your free account to unlock your custom reading experience.