16
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cloud9でPostgreSQLを利用しHerokuにRails5.xをデプロイした時の手順

Last updated at Posted at 2016-10-08

HerokuにCloud9からRails5.0.0.1をデプロイする時に行った方法です。

ワークスペース作成

Cloud9に新規でワークスペースを作成
blankを選択
スクリーンショット 2016-10-08 15.31.39.png

Railsのインストール・アプリの作成

gem install rails --no-ri --no-rdoc
rails new myapp --database=postgresql
cd myapp
bundle install

Getting Started with Rails 5.x on Heroku

Hello World

rails generate controller welcome

app/views/welcome/index.html.erb

<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

config/routes.rb

root 'welcome#index'

この時点ではPostgreSQLの設定をしていないので rails s -p $PORT -b $IP をやってもエラーになります。

Cloud9でPostgreSQLを使えるようにする

PostgreSQL起動

sudo service postgresql start

ユーザー作成+α

sudo sudo -u postgres psql
postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
postgres=# DROP DATABASE template1;
postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
postgres=# \c template1
postgres=# VACUUM FREEZE;
postgres=# \q

Cloud9の設定にユーザー名とパスワードを保存

echo "export USERNAME=username" >> ~/.profile
echo "export PASSWORD=password" >> ~/.profile
source ~/.profile

dababase.ymlを編集

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  host:     <%= ENV['IP'] %>
  
development:
  <<: *default
  database: myapp_development

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

Cloud9で動作確認

DB作成

rails db:create
rails db:migrate

起動

rails s -p $PORT -b $IP

Herokuにデプロイ

ログイン

heroku login

コミット

git init
git add .
git commit -m "init"

デプロイ

heroku create
git push heroku master

以上となります。
これでうまくいかない場合や不明点などありましたらお知らせ下さい。

16
20
12

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?