Making the Change From SQLite3 to PostgreSQL – Ruby on Rails

While going through bootcamp, I always noticed on Slack how at least once a week someone had mentioned having issues deploying their projects to Heroku. It definitely made the act of deploying one of my projects to Heroku, seem incredibly intimidating. I put off doing it for so long, but this morning I decided I was going to overcome my fears and get it done.
What issues could these people have been dealing with, you may ask? Well, the reoccurring issue that I saw over and over again on Slack, was in regards to switching the projects database from SQLite3 to PostgreSQL. This is something that is required to be done in order to deploy to Heroku, and, at least to me, the idea of completely changing my database was intimidating.
Well, I’m now here to tell you that my fears were completely unwarranted, because changing the database in my Ruby on Rails project was very simple.
I’m going to go ahead and give y’all a quick step by step tutorial of how to accomplish switching databases. For this tutorials purposes, you should already have a pre-existing project that you are wanting to switch the database from SQLite3 to PostgreSQL, so I’m going to skip over the steps of creating a new RoR application.

Installing PostgreSQL

The first step to switching databases is to ensure that you have Postgres.app installed on your machine. Once you’ve installed this, you can keep reading on.

Preparing the Gemfile

Since you are looking at this tutorial, I assume you currently have the sqlite3 gem in your Gemfile. In order to switch your database over, you’ll need to remove the line
gem ‘sqlite3’

and you’ll need to replace it with
gem ‘pg’

Once you’ve done this, make sure to re-install your dependencies by running $ bundle install in the terminal.

Updating config/database.yml

Changing the Gemfile is, unfortunately, not enough to get the database switched. The next step to accomplishing this is making sure that your config/database.yml file is using the postgresql adapter instead of the sqlite3 one.
Your original config/database.yml should look like this:

You’ll need to change this document to look like the following:

Please note that the database is supposed to be your app name, in my case it’s pint-trackr-rails-js_development. For test, instead of adding _development at the end of my app name, I add _test. I also added my Mac username to the postgresql adapter because I had seen it in another tutorial (linked at the bottom of this article) and it worked for me.

Setup and Migration

Now in order to get the PostgreSQL database up and running in your app, you will need to run the following commands
$ rake db:setup


$ rake db:migrate

Please note that at this point you can remove your SQLite3 database files from the project directory.
Now, you should check to make sure that the database is up and running by starting the rails server. You can do this by running the command $ rails s in the terminal.

Committing to Git/Github

The one thing that caused me issues when I tried deploying to Heroku, was that I forgot to push my code to it’s master branch on Github. I kept getting an error that said I was trying to deploy a project using SQLite3. I was so confused and didn’t understand what was happening until I googled and easily found the missed step.
So, once you know your database is up and running, be a good developer and commit, commit, COMMIT. Obviously, at this point, once you feel comfortable with your code, push it to the master branch.

Final Comments

By this point, you are ready to dive into deploying your project with Heroku. There are still a few steps in regards to this, but I’ll get into that during another tutorial specifically geared towards Heroku deployment. If you’re reading this before I’ve gotten that out, please feel free to click the link above for Heroku’s official tutorial on deployment of a Ruby on Rails project.
Happy coding!!

Sources

Getting Started on Heroku with Rails 5.x
Ruby on Rails Switch From SQLite3 to Postgres

Link: https://dev.to/torianne02/making-the-change-from-sqlite3-to-postgresql-ruby-on-rails-2m0p