如何将现有的Rails应用程序移至heroku? (从sqlite到postgres)

时间:2018-06-27 17:41:17

标签: ruby-on-rails postgresql sqlite heroku heroku-cli

我有一个现有的Ruby on Rails应用程序,已经在其中加载了数据。

我使用默认的SQLite数据库设置,因此所有数据都位于该位置,但是我需要所有数据才能进入heroku上的Postgres数据库。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

10分钟从本地SQLite移至Heroku Postgres

-一路将您的本地开发者更新为postgres-

这是假设您在sqlite中有一个开发数据库,​​并且想要将结构和数据移至heroku。您将首先将您的本地环境更改为postgres,然后将其全部上移。

为什么要更改?您应该始终使开发环境与生产环境相同。在Heroku上,默认使用Postgres。

您首先需要使用具有您的用户名的用户在本地安装和配置Postgres


所需的软件:postgresql,pgloader,heroku-cli


步骤

在您的开发环境中从SQLite迁移到Postgres

  1. 安装heroku / pgloader / postgres,并确保postgresql在您的系统上运行
  2. 备份sqlite-将development.sql复制到development_old.sql
  3. gem 'pg'添加到Gemfile的主要部分
  4. 捆绑安装
  5. 更新config / database.yml(请参见下面的示例)
  6. rake db:setup
  7. cd [应用程序根目录]
  8. 使用数据加载Postgres数据库-pgloader ./db/development.sqlite3 postgresql:///[name of postgres dev db]
  9. 删除gem 'sqlite3'
  10. 捆绑安装
  11. 启动服务器-rails server
  12. test by visiting app at localhost:3000

在heroku上设置新应用

Follow these instructions from heroku

将数据移动到heroku

  1. 找到heroku数据库信息-heroku pg:info
  2. 删除并重置远程数据库-heroku pg:reset DATABASE_URL --app [name of app]
  3. 将本地数据推送到heroku-heroku pg:push [name of postgres dev db] DATABASE_URL --app [name of app]

注意:如果该数据库的行数超过1万,则还需要升级到heroku上的嗜好基础层

将Heroku升级到Hobby Tier Basic

  1. 创建新层-`heroku插件:创建heroku-postgresql:hobby-basic --app [应用名称]
  2. 获取新的数据库网址-heroku pg:info
  3. 开启维护-heroku maintenance:on --app [name of app]
  4. 复制数据-heroku pg:copy DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
  5. 升级新数据库-heroku pg:promote [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
  6. 关闭维护
  7. 通过访问heroku应用进行测试

如果您遇到问题或极端情况,这里有一些资源可以帮助您。

资源:

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]

答案 1 :(得分:0)

嘿伙计,您在下面的链接中拥有了所需的一切

How to change from SQLite to PostgreSQL and deploy on heroku

让我知道您是否还有任何疑问 问候

相关问题