|
| 1 | +# Essential Guide 12: Heroku Deployment with Postgres |
| 2 | + |
| 3 | +## Introduction |
| 4 | +In this guide, we will |
| 5 | +- install and use PostgreSQL instead of SQLite3 |
| 6 | +- deploy our application to heroku |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | +- Heroku CLI ([view installation details](https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up)) |
| 10 | +- Postgresql ([view installation details](https://devcenter.heroku.com/articles/heroku-postgresql#local-setup)) |
| 11 | + |
| 12 | +## Adding Postgres |
| 13 | + |
| 14 | +In the Gemfile, replace the line starting with `gem 'sqlite3'` with `gem 'pg'`. |
| 15 | + |
| 16 | +Make sure to run `bundle install` afterwards and replace the contents of `config/database.yml` with |
| 17 | + |
| 18 | +<details> |
| 19 | +<summary>Note for Linux/Ubuntu users</summary> |
| 20 | +You may need to install additional libraries by running <br/> |
| 21 | +<code>sudo apt-get -y install postgresql postgresql-contrib libpq-dev</code> |
| 22 | +instead of only running <br/> |
| 23 | +<code>sudo apt-get install postgresql</code>. |
| 24 | +</details> |
| 25 | +<br/> |
| 26 | + |
| 27 | +<details> |
| 28 | +<summary>Note for postgres role error</summary> |
| 29 | +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. <br/> |
| 30 | +<code>sudo su - postgres && createuser -s -r postgres</code> |
| 31 | +</details> |
| 32 | +<br/> |
| 33 | + |
| 34 | +```yaml |
| 35 | +default: &default |
| 36 | + adapter: postgresql |
| 37 | + encoding: unicode |
| 38 | + # For details on connection pooling, see Rails configuration guide |
| 39 | + # https://guides.rubyonrails.org/configuring.html#database-pooling |
| 40 | + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> |
| 41 | + |
| 42 | +development: |
| 43 | + <<: *default |
| 44 | + database: myapp_development |
| 45 | + |
| 46 | +test: |
| 47 | + <<: *default |
| 48 | + database: myapp_test |
| 49 | + |
| 50 | +production: |
| 51 | + <<: *default |
| 52 | + database: myapp_production |
| 53 | + username: myapp |
| 54 | + password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %> |
| 55 | +``` |
| 56 | +
|
| 57 | +## Deployment |
| 58 | +
|
| 59 | +To set up a new heroku project, run |
| 60 | +
|
| 61 | +```sh |
| 62 | +heroku create |
| 63 | +``` |
| 64 | + |
| 65 | +followed by |
| 66 | + |
| 67 | +```sh |
| 68 | +git push heroku master |
| 69 | +``` |
| 70 | + |
| 71 | +to trigger a deployment. |
| 72 | +When we have new migrations or didn't initialize the database yet, we need to run |
| 73 | + |
| 74 | +```sh |
| 75 | +heroku run rails db:migrate |
| 76 | +``` |
| 77 | + |
| 78 | +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 |
| 79 | + |
| 80 | +```sh |
| 81 | +heroku run rake db:seed |
| 82 | +``` |
| 83 | + |
| 84 | +After the deployment, our database migrate and seeds task successfully finished we can visit our deployed application by running `heroku open`. |
| 85 | + |
| 86 | +If you have used assets like we did in our application remember to run `heroku run rails assets:precompile`. |
| 87 | + |
| 88 | +## Creating an admin |
| 89 | + |
| 90 | +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. |
| 91 | + |
| 92 | +``` |
| 93 | +heroku run rails console |
| 94 | +``` |
| 95 | + |
| 96 | +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. |
| 97 | + |
| 98 | +```ruby |
| 99 | +Admin.create( email: '[email protected]', password: 'OnlyForSuperMates', password_confirmation: 'OnlyForSuperMates') |
| 100 | +``` |
| 101 | + |
| 102 | +Now we can close the console and run `heroku open` again. We can now login to our admin app with the above specified credentials. |
| 103 | + |
| 104 | +## Recap & outlook |
| 105 | + |
| 106 | +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. |
| 107 | + |
| 108 | +While the application is good as it is right now, go ahead and check out the [last part of the essential guide](/docs/guides/2-essential/13_wrap_up.md). |
0 commit comments