Before we begin install postgresql onto your device:
$ brew install postgresqlCheck if the postgresql service is running via brew :
$ brew servicesIf not run the rollowing command :
$ brew services start postgresqlWhen Deploying an existing application to Heroku first
You must change your Gemfile:
gem 'sqlite3' to:
gem 'pg' SQLite3 is not supported by Heroku and therefore we must use Postgres to deploy our database.
Next you will need to convert your config/database.yml.
We will be changing the adapter to postgresql.
adapter: sqlite3To:
adapter: postgresqlWe will also be naming our database for production,test,and development for postgres to use as well. That change will look something similar to...
From this:
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
To this:
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: my_database_development
test:
<<: *default
database: my_database_test
production:
<<: *default
database: my_database_production
Once all that is changed go ahead and save your changes.
If you haven't already run bundle install to update the Gemfile to use gem 'pg' .
Now we will create our postgres database and run our migrations.
rails db:create
rails db:migrateAt this point you are ready to deploy to heroku.
Note that your rails api will not have a home page so you will have to add a controller for your root page and add
root 'controller_name#index' in your config/routes.rb to have home page.
Before we begin make sure to save all changes and push to the github repo you will be saving your Rails app to.
Also if you don't have Heroku cli installed do so now with the code below
w/ Homebrew
$ brew tap heroku/brew && brew install heroku##Step 1
Now that we have the Heroku cli installed... First we will run heroku create to create our heroku app. Make sure you are in the directory that contains your Rails app. You may be prompted to login to heroku at this point.
$ heroku createOnce our heroku app is created we can deploy our code to heroku with the following command:
$ git push heroku masterNow we run our migrations but this time via heroku:
$ heroku run rake db:migrateAnd if you have seeds to add :
$ heroku run rake db:seed
A few additional random notes I found incase you run into these errors:
Be sure to add the
<%= %>erb tags to this line, as we found today:pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>if your database isn't seeding or migrating try running
heroku run:detached rake db:migrateand if you're starting a new project you can also use
rails new myapp --database=postgresqland also if you're getting Cannot run more than 1 free size dynos
run
heroku pswhich lists out any open session.
The following is an example of what could be listed after running “heroku ps”
run.5656 (Free): up 2016/01/12 21:28:41 (~ 7m ago): rails cthen you would run
heroku ps:stop run.5656random references