如何将现有的rails应用程序移动到heroku? (sqlite到postgres)
我有一个已经存在数据的Ruby on Rails应用程序。
我使用了默认的SQLite数据库设置,因此这是我所有数据所在的位置,但我需要将所有数据都放到heroku上的Postgres数据库中。
我怎么做?
10分钟从本地SQLite转移到Heroku Postgres
– 沿途更新您当地的开发人员到postgres –
假设您在sqlite中有一个开发数据库,并且您希望将结构和数据移动到heroku。 您将首先将您的本地环境更改为postgres,然后将其全部移动。
为什么改变? 您应始终将开发环境镜像到生产环境中。 使用Postgres是heroku的默认设置。
您需要首先在本地安装和配置Postgres,并使用具有您用户名的用户
需要的软件:postgresql,pgloader,heroku-cli
脚步
在您的开发环境中从SQLite移动到Postgres
- 安装heroku / pgloader / postgres,并确保postgresql正在您的系统上运行
- backup sqlite – 将development.sql复制到development_old.sql
- 将gem’pg
gem 'pg'
添加到Gemfile的主要部分 - 捆绑安装
- update config / database.yml(参见下面的示例)
- rake db:setup
- cd [应用程序根目录]
- 用数据加载postgres db –
pgloader ./db/development.sqlite3 postgresql:///[name of postgres dev db]
- 删除
gem 'sqlite3'
- 捆绑安装
- 启动服务器 –
rails server
- 通过访问localhost:3000的应用程序进行测试
在heroku上设置新的应用程序
请遵循heroku的这些说明
将数据移动到heroku
- 找到heroku db info –
heroku pg:info
- 擦除并重置远程db –
heroku pg:reset DATABASE_URL --app [name of app]
- 将本地数据推送到heroku –
heroku pg:push [name of postgres dev db] DATABASE_URL --app [name of app]
注意:如果该数据库的行数超过10k,则还需要升级到heroku上的业余爱好基本层
将Heroku升级到Hobby Tier Basic
- 创建新层 – `heroku addons:创建heroku-postgresql:hobby-basic –app [app of app]
- 获取新的数据库url –
heroku pg:info
- 打开维护 –
heroku maintenance:on --app [name of app]
- 复制数据 –
heroku pg:copy DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
- 推广新的db –
heroku pg:promote [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
- 关闭维护
- 通过访问heroku应用程序进行测试
如果您遇到问题或边缘情况,这里有一些资源可以提供帮助。
资源:
- https://pgloader.io
- postgres安装文档
- heroku new rails安装
- heroku cli信息
- 使用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]
嘿老兄你在下面的链接中有你需要的一切
如何从SQLite更改为PostgreSQL并在heroku上部署
如果你有任何疑问,请告诉我
- 预编译资产失败ExecJS :: ProgramError:意外的标记:operator(=)(行:10770,col:0,pos:300859)
- 不能通过我的应用程序运行rake数据库迁移
- Heroku推被拒绝,铁路依赖冲突? Ruby on Rails教程第1.4.2节
- Firefox不会从CloudFront渲染字体
- Heroku推送拒绝:在源代码中找不到jquery-rails-2.0.0
- Heroku是否支持`rails dbconsole`?
- 如何确定在Heroku Performance dyno上运行的Puma工作者和线程的正确数量?
- Errno :: ECONNREFUSED(连接被拒绝 – 连接(2)为nil端口587):对于Heroku上的Rails
- Rails资产预编译只是不起作用