> For the complete documentation index, see [llms.txt](https://docs.matestack.io/matestack-ui-core/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.matestack.io/matestack-ui-core/1.5.0/spa-like-apps/tutorial/12_heroku_deployment.md).

# Essential Guide 12: Heroku Deployment with Postgres

Demo: [Matestack Demo](https://demo.matestack.io)\
Github Repo: [Matestack Demo Application](https://github.com/matestack/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](/matestack-ui-core/1.5.0/spa-like-apps/tutorial/11_authentication_devise.md).

* Heroku CLI ([view installation details](https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up))
* Postgresql ([view installation details](https://devcenter.heroku.com/articles/heroku-postgresql#local-setup))

## 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>

```yaml
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
```

## Deployment

To set up a new heroku project, run

```bash
heroku create
```

followed by

```bash
git push heroku master
```

to trigger a deployment. When we have new migrations or didn't initialize the database yet, we need to run

```bash
heroku run rails db:migrate
```

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

```bash
heroku run rake db:seed
```

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.

```
heroku run rails console
```

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.

```ruby
Admin.create(email: 'admin@example.com', password: 'OnlyForSuperMates', password_confirmation: 'OnlyForSuperMates')
```

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](/matestack-ui-core/1.5.0/spa-like-apps/tutorial/13_wrap_up.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.matestack.io/matestack-ui-core/1.5.0/spa-like-apps/tutorial/12_heroku_deployment.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
