Help us understand the problem. What is going on with this article?

Heroku PostgresをRailsアプリで利用する手順

More than 1 year has passed since last update.

もくじ

Heroku Postgres
- URL取得
- 補足:アドオン追加
Rails
- database.yml
- Gemfile
デプロイ
マイグレーション

Heroku Postgres

URL取得

heroku configコマンドでDATABASE_URLを確認する

$ heroku config
=== xxxxx-xxxxx-xxxxx Config Vars
DATABASE_URL:             postgres://<username>:<password>@<host>:<port>/<database>

補足:アドオン追加

DATABASE_URLが表示されない場合、アドオンの追加を行う。

参考:Heroku Postgres | Heroku Dev Center

$ heroku config
=== xxxxx-xxxxx-xxxxx Config Vars

# => DATABASE_URLが表示されない場合は、以下のコマンドを実行してアドオンを追加

$ heroku addons:create heroku-postgresql:hobby-dev

Creating heroku-postgresql:hobby-dev on ⬢ xxxxx-xxxxx-xxxxx... free
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-xxxxx-xxxxx as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

$ heroku config
=== xxxxx-xxxxx-xxxxx Config Vars
DATABASE_URL:             postgres://<username>:<password>@<host>:<port>/<database>

# => アドオンが追加され、DATABASE_URLが表示されるようになる

Rails

database.yml

database.ymlで環境変数DATABASE_URLを参照するように設定する

参考:Rails アプリケーションを設定する | Rails ガイド

config/database.yml
production:
  url: <%= ENV['DATABASE_URL'] %>

Gemfile

GemfileでProduction環境でpgを利用するように設定する

Gemfile
# Use sqlite3 as the database for Active Record
gem 'sqlite3', group: [:development, :test]
gem 'pg', group: :production

デプロイ

Herokuにアプリをデプロイする。

参考:HerokuにRailsアプリをデプロイする手順 - Qiita

マイグレーション

デプロイしたアプリでマイグレーションを実施する。

# DBのマイグレーション
$ heroku run rake db:migrate

# dynoの再起動
$ heroku ps:restart

# アプリを開く
$ heroku open
NaokiIshimura
Ruby on Rails Engineer.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした