create a new Rails API with a PostgreSQL database

Creating a New Rails API with a PostgreSQL Database

Ethan Ryan
Mar 31, 2018 · 2 min read

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 -vgem 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.

celebrate!

Thanks for reading, nerds!

Ethan Ryan

Written by

// I dream in JavaScript // https://www.ethanryan.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade