【发布时间】:2018-12-06 16:38:39
【问题描述】:
我有一个现有的 Ruby on Rails 应用程序,其中已经加载了数据。
我使用了默认的 SQLite 数据库设置,所以这是我所有数据所在的位置,但我需要将所有数据放入 heroku 上的 Postgres 数据库。
我该怎么做?
【问题讨论】:
标签: ruby-on-rails postgresql sqlite heroku heroku-cli
我有一个现有的 Ruby on Rails 应用程序,其中已经加载了数据。
我使用了默认的 SQLite 数据库设置,所以这是我所有数据所在的位置,但我需要将所有数据放入 heroku 上的 Postgres 数据库。
我该怎么做?
【问题讨论】:
标签: ruby-on-rails postgresql sqlite heroku heroku-cli
这是假设您在 sqlite 中有一个开发数据库,并且您希望将结构和数据移动到 heroku。您将首先将本地环境更改为 postgres,然后将其全部升级。
为什么要改变?您应该始终让您的开发环境反映您的生产环境。使用 Postgres 是 heroku 上的默认设置。
您需要先使用具有您的用户名的用户在本地安装和配置 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 上设置新应用
Follow these instructions from heroku
将数据移至heroku
heroku pg:info
heroku 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:info
heroku 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]
如果您遇到问题或边缘情况,这里有一些资源可以提供帮助。
database_sample.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]
【讨论】:
Couldn't create '..._dev' database. Please check your configuration. rake aborted!
pgloader ./db/development.sqlite3 postgresql:///[username]:[password]@[name of postgres dev db]
【讨论】: