Dan*_*ams 3 sqlite postgresql ruby-on-rails heroku heroku-cli
我有一个现有的 Ruby on Rails 应用程序,其中已加载数据。
我使用了默认的 SQLite 数据库设置,因此这是我所有数据所在的位置,但我需要将所有数据放入 Heroku 上的 Postgres 数据库中。
我怎么做?
假设您在 sqlite 中有一个开发数据库,并且您希望将结构和数据移动到 heroku。您将首先将本地环境更改为 postgres,然后将其全部移动。
为什么要改变?您应该始终让您的开发环境反映您的生产环境。Heroku 上默认使用 Postgres。
您需要首先使用具有您用户名的用户在本地安装和配置 Postgres
所需软件:postgresql、pgloader、heroku-cli
在您的开发环境中从 SQLite 迁移到 Postgres
gem 'pg'到 Gemfile 的主要部分pgloader ./db/development.sqlite3 postgresql:///[name of postgres dev db]gem 'sqlite3'rails server在heroku上设置新应用程序
将数据移动到heroku
heroku pg:infoheroku pg:reset DATABASE_URL --app [name of app]heroku pg:push [name of postgres dev db] DATABASE_URL --app [name of app]注意:如果该数据库的行数超过 10k,您还需要在 Heroku 上升级到爱好基本层
将 Heroku 升级到 Hobby Tier Basic
heroku pg:infoheroku maintenance:on --app [name of app]heroku pg:copy DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]heroku pg:promote [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]如果您遇到问题或边缘情况,这里有一些资源可以提供帮助。
数据库样本.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
port: 5432
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: [name of app]_dev
test:
<<: *default
database: [name of app]_test
staging:
<<: *default
database: [name of app]
production:
<<: *default
database: [name of app]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3308 次 |
| 最近记录: |