Changing your Rails database from sqlite to postgresql for Heroku deploy.

If you were like me a few months ago, you just finished your bootcamp and have a bunch of projects that you want to deploy as quickly as possible to get started on your job search. You try pushing your backend to your newly created Heroku page and keep getting a database error. You look back into your notes about deployment and see something you somehow missed at the top of your notes in big lettering…

“YOU NEED TO USE PG FOR YOUR DATABASE OR THIS WONT WORK”

Lucky for us, changing your database isn’t as complicated as it sounds, and I’m going to walk you through step by step on how to get your db ready to deploy to Heroku. Make sure you have the Postgres.app installed before you get started. Once you’re finished with switching the database, you’ll need to open the Postgres.app and initialize the server to the port you’re running on.

Changing your Gemfile
Check out lines 8 + 9

The first thing you’ll want to do is check out your gemfile. A few lines in you should see that you’ve enabled sqlite as your database. All you need to do here is change the gem from ‘sqlite3’ to ‘pg’. Might be a good idea to change the comment as well to keep it tidy. Once you’ve done this, bundle install. Note: If you have sqlite inside your gemlock file, you should remove it from there to make sure your bundle works properly.

The next step is the biggest change we need to make before we’re pretty much done. For this change, we’re headed inside of our .config folder to our database.yml file. Currently it should look like the following image…

And once we’ve made our changes, it will look like this…

The top 14 lines are all commented install options for ‘pg’ that I couldn’t include in this large photo.

Note that the database/usernames included in the above are from my personal code. Make sure when you change the database section to end your naming choices with _development, and _test in their respective sections.

At this point, you’re ready to delete your old database/schema and then re-seed and re-migrate your database. Once that’s finished, start your local Rails server and check to see if things are working as intended. If they are, you have successfully changed your database and you should now be all set to push up to Heroku!

Special thanks to Dave Ferrera and his blog that helped me figure this out!

--

--

--

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Day 34. BRM Framework

Difference between iTerm2, zsh and oh-my-zsh

My Journey towards Certified Kubernetes Security Specialist (CKS)

12 Unique Python Project Ideas for Your Resume

Typing on a laptop

Cudos Labs: development update! 🧠 (21/10/2021) — CUDOS

Why chaos is sometimes needed — Chaos Engineering — Royal Cyber

AWS Tools for Rightsizing Instances

Why You Shouldn’t Always Use an IDE

Image of male coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Gleeson

Kevin Gleeson

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.

More from Medium

Fixing Postgres Connection to Server on Mac

Rails User Session Handling using Redis

Run your Ruby on Rails application in AWS from scratch

Improve App Performance with Serverless Threading