create a new Rails API with a PostgreSQL database

Creating a New Rails API with a PostgreSQL Database

Every time I create a new Rails API with a PostgreSQL database, I find myself googling “create a new Rails API with a PostgreSQL database”. So I’m leaving these notes here for myself, and anyone else who finds this helpful.

I like using PostgreSQL for my database because Heroku likes PostgreSQL, and I like making my Rails app an API-only application because I’ll be creating a separate React frontend for my app, so I don’t need a bunch of empty Views folders in my Rails app, since I won’t be using them.

For the purpose of this tutorial, let’s imagine we’re creating a super awesome app, and we’re going to call it Super Awesome App.

1) create folder

mkdir super-awesome-app

2) cd into that folder

cd super-awesome-app

3) make sure your Rails version is up to date

For the hell of it, make sure your Rails version is up to date before creating a new Rails app. This seems like a good best practice, so why not.

rails -v
gem update rails

4) create new rails API with a PostgreSQL database

rails new super-awesome-api --api --database=postgresql

This will create a Rails project directory named super-awesome-api.

Note 1: you can give your Rails project any name you want. In other words:

rails new project-name-here --api --database=postgresql

Note 2: I plan on creating another directory later, called super-awesome-client, where I’ll be creating my Super Awesome App’s React frontend.

5) cd into your new Rails folder

cd super-awesome-api

6) create a Git repo for your Rails app

Don’t forget to keep track of your Rails app’s development on GitHub! Now’s a good time to create a new Git repo for your Rails app.

7) celebrate for ten seconds, then get to work

That’s it! You just built a new Rails app! It’s an API-only application, and it’s all set for a PostgreSQL database. Pump your fist and nod your head and maybe scream: “I DID IT!” Then get to work, you’ve only just begun.


Thanks for reading, nerds!