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.
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…
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!