I’ve been working on making a Rails back end for a project that I hope to deploy some time in the near future. Additionally, I would like to deploy that project via Heroku. In order to make that happen, with setting things up on my computer with PostgreSQL. When creating a Rails project, the program will default to creating a SQLite database, however in order to deploy to Heroku we gotta go with Postgres (see resources below for reasons why). In order to go against the default behavior, and initialize my Rails project with Postgres, it is as simple as adding an additional tag to my “rails new” terminal command:
rails new project-name --database=postgresql
Running the above command will result in the creation of a new Rails project with a Postgres database. It is certainly possible, and I don’t believe that difficult, to create the project with the default SQLite database and then later switch over to Postgres when time to deploy — but I generally prefer to do any sort of nitty-gritty configuration details up front, that way if there are any project-breaking issues that I haven’t wasted time on something that I can’t get to work.
Now I had run the command, and after spending a significant amount of time getting Ruby and the Rails gem set up on my computer, was able to successfully create my project. However, I was not out of the woods yet. Different from the default SQLite, in order to begin using the Postgres database, the first command to enter is “rails db:create”. Side note — I can’t quite remember at this point, but I believe that with SQLite, it’s not necessary to use this command, because when the first migration is invoked, if there is no database yet, one will simply be created on the spot.
I was proud of myself for remembering that I needed the “rails db:create” command before running any migration, and that pride quickly faded as the command didn’t work! Having since fixed the issue I no longer have access to the actual error message, but it was many many lines of code, and the main message went something like:
PG::Bad Connection Fatal: role "jacobkenny05" does not exist
Of course me being the neanderthal that I am, rather than simply copy/pasting this error message into Google, I immediately jumped to the conclusion that I had made some sort of mistake somewhere along the line while I was either installing Ruby, Rails, or Postgres. Not really much to go on, but I spent a lot of time messing around with Ruby configuration files, creating test Rails applications, and uninstalling and reinstalling Postgres. I really wish that I had just googled the error message at the beginning, because once I found the solution, I facepalmed with the force of 1000 dying suns.
It turns out that I simply needed to use the postgres cli to create this user: jacobkenny05. See the stackoverflow link at the bottom for more details on how to do this, but after running the command:
sudo -u postgres createuser -d -e jacobkenny05
I was immediately able to move over to my project and create the new database, as well as begin the process of generating my database schema. I found it to be another learning experience, or maybe reinforcement of a concept that I should have already learned — READ THE ERROR MESSAGES.
SQLite on Heroku
Ruby on Rails and some other web based frameworks ship with support for a small database called sqlite3 by default…
Ruby on Rails: Switch from sqlite3 to Postgres
Have you built a Rails application on top of the default sqlite3 database and now want to deploy it to Heroku? Well I…