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

问题描述:

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

I have an existing Ruby on Rails app that already has data loaded into it.

我使用默认的SQLite数据库设置,因此所有数据都存放在这里位置,但我需要所有数据才能进入heroku上的Postgres数据库。

I used the default SQLite database setup, so that is where all my data is located, but I need all my data to go into my Postgres database on heroku.

我该怎么做?

10分钟从本地SQLite迁移到Heroku Postgres



-在此过程中将本地开发人员更新为postgres- -



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

10 Minutes Move from Local SQLite to a Heroku Postgres

-- updates your local dev to postgres along the way --

This is assuming you have a development database in sqlite and you want to move the structure and data to heroku. You will be first changing your local environment to postgres, then moving it all up.

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

Why change? You should always have your development environment mirror your production environment. Using Postgres is the default on heroku.

您首先需要使用具有用户名的用户在本地安装和配置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 db- pgloader ./db/development.sqlite3 postgresql:/// [postgres dev名称db]

  9. 删除 gem'sqlite3'

  10. 捆绑安装

  11. 启动服务器-铁路服务器

  12. 通过访问位于localhost:3000的应用进行测试

  1. install heroku / pgloader / postgres, and make sure postgresql is running on your system
  2. backup sqlite - copy development.sql to development_old.sql
  3. add gem 'pg' to main section of your Gemfile
  4. bundle install
  5. update config/database.yml (see sample below)
  6. rake db:setup
  7. cd [application root]
  8. load postgres db with data - pgloader ./db/development.sqlite3 postgresql:///[name of postgres dev db]
  9. remove gem 'sqlite3'
  10. bundle install
  11. start server - rails server
  12. test by visiting app at localhost:3000

在heroku上设置新应用

从heroku中遵循以下说明

将数据移至heroku


  1. 查找heroku数据库信息- heroku pg:info

  2. 擦除并重置远程数据库- heroku pg:重置DATABASE_URL --app [应用程序名称]

  3. 将本地数据推送到heroku- heroku pg:push [postgres dev db的名称] DATABASE_URL --app [应用程序的名称]

  1. find heroku db info - heroku pg:info
  2. erase and reset remote db - heroku pg:reset DATABASE_URL --app [name of app]
  3. push local data to heroku - heroku pg:push [name of postgres dev db] DATABASE_URL --app [name of app]

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

将Heroku升级到Hobby Tier B asic


  1. 创建新层-`heroku addons:create heroku-postgresql:hobby-basic --app [应用程序名称]

  2. 获取新的数据库URL- heroku pg:info

  3. 打开维护- heroku维护:--app [应用名称]

  4. 复制数据- heroku pg:复制DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [应用名称]

  5. 升级新数据库- heroku pg:升级[HEROKU_POSTGRESQL_COLOR_URL] --app [应用程序名称]

  6. 关闭维护

  7. 通过访问heroku应用程序进行测试

  1. create new tier - `heroku addons:create heroku-postgresql:hobby-basic --app [name of app]
  2. get the new database url - heroku pg:info
  3. turn on maintenance - heroku maintenance:on --app [name of app]
  4. copy data - heroku pg:copy DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
  5. promote new db - heroku pg:promote [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
  6. turn off maintenance
  7. test by visiting heroku app






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


In case you run into issues or edge cases, here are some resources to help.

  • https://pgloader.io
  • postgres install docs
  • heroku new rails install
  • heroku cli info
  • using the heroku cli

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]