LoginSignup
24
22

More than 3 years have passed since last update.

heroku上でのDB(postgres)リセットからのmigration実行方法

Posted at

毎回いちいち探して確認しているので、備忘録としてアウトプット

実行環境

  • Rails: 5.2
  • PostgreSQL: 11.5

実際行った手順

まず、下記でmigrateしたいので, DBリセットしようと思い下記を実行

# ローカルから
$ heroku rake db:migrate:reset

ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1

上のエラーで言われた通り実行

$ heroku rake db:migrate:reset DISABLE_DATABASE_ENVIRONMENT_CHECK=1

FATAL:  permission denied for database "postgres"
DETAIL:  User does not have CONNECT privilege.
Couldn't drop database '○○○○○○○○○○'
rake aborted!
PG::ConnectionBad: FATAL:  permission denied for database "postgres"
DETAIL:  User does not have CONNECT privilege.

権限がないとのことでRailsからDBリセットできない。
しかたないので、

# DBに直アクセスしてDBリセット
$ heroku pg:reset DATABASE_URL

 ›   Warning: heroku update available from 7.19.4 to 7.33.3
 ▸    WARNING: Destructive action
 ▸    postgresql-polished-○○○○○ will lose all of its data
 ▸    
 ▸    To proceed, type AAAAAAAAA or re-run this command with --confirm AAAAAAAAA

# 指示されたように下記を入力
> AAAAAAAAA

resetting postgresql-polished-○○○○○... done

DBがリセットされたので、migration実行。

$ heroku run rails db:migrate

Running rails db:migrate on ⬢ AAAAAAAAA... up, run.6493 (Free)
# いつものmigrationの実行時のログ

成功!

以上。

24
22
0

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
24
22