Essential Guide 12: Heroku Deployment with Postgres
Demo: Matestack Demo Github Repo: Matestack Demo Application
Introduction
In this guide, we will
install and use PostgreSQL instead of SQLite3
deploy our application to heroku
Prerequisites
We expect you to have successfully finished the previous guide.
Heroku CLI (view installation details)
Postgresql (view installation details)
Adding Postgres
In the Gemfile, replace the line starting with gem 'sqlite3'
with gem 'pg'
.
Make sure to run bundle install
afterwards and replace the contents of config/database.yml
with
Note for Linux/Ubuntu users You may need to install additional libraries by running
sudo apt-get -y install postgresql postgresql-contrib libpq-dev
instead of only running
sudo apt-get install postgresql
. </details>
Note for postgres role error If you get an error from postgres stating that your role is missing add it by creating a user. To do so run below codesnippet.
sudo su - postgres && createuser -s -r postgres
</details>
Deployment
To set up a new heroku project, run
followed by
to trigger a deployment. When we have new migrations or didn't initialize the database yet, we need to run
In an earlier guide we added some persons with seeds to our local database. In order to also seed some persons into our production database on heroku we need to run
After the deployment, our database migrate and seeds task successfully finished we can visit our deployed application by running heroku open
.
If you have used assets like we did in our application remember to run heroku run rails assets:precompile
.
Creating an admin
In order to be able to login, we need to create an admin. We could add one in our seeds but we wouldn't recommend doing it. Instead we can use another of herokus features. Opening a rails console connected with the production database of our live application.
After the console opened we can now create an admin using the create
method of our Admin
model with an email, password and password confirmation as parameters.
Now we can close the console and run heroku open
again. We can now login to our admin app with the above specified credentials.
Recap & outlook
We successfully deployed our application to heroku and learned what are the necessary steps to do this. We also switched our application database from sqlite to postgres, because heroku doesn't support sqlite.
While the application is good as it is right now, go ahead and check out the last part of the tutorial.
Last updated