• Rails使用PostgreSQL在Heroku上部署


    一. 本地

    安装PostgreSQL

    Ubuntu安装postgresql,libpg-dev,并自动安装相关依赖库

    安装图形化客户端pgadmin3

    创建用户

    $ sudo su postgres

    postgres@scige:/home/xxxxxx$ createuser -P sanbaoyuan

    这里需要设置密码,不设置密码不能链接成功

    修改配置文件Gemfile

    group :development do
    gem 'sqlite3'
    end

    group :production do
    gem 'pg'
    end

    安装依赖包

    $ bundle install

    修改配置文件config/database.yml

    production:
      adapter: postgresql
      encoding: unicode
      database: sanbaoyuan_production
      pool: 5
      host: localhost
      username: sanbaoyuan
      password: 123456
      timeout: 5000

    创建数据库

    使用pgadmin3用sanbaoyuan帐号登录,创建sanbaoyuan_production

    需要手动创建数据库,Rails不会创建数据库

    这里使用postgresql的命令也可以完成

    迁移production环境数据库

    $ rake db:migrate RAILS_ENV=production

    编译production环境静态文件

    修改配置文件config/environments/production.rb

    config.serve_static_assets = false --> config.serve_static_assets = true

    编译完成后,文件会生成在public/asserts/

    $ rake assets:precompile

    启动production环境服务器

    $ rails server -e production

    二. Heroku

    重新clone出代码到部署目录,避免开发时影响线上效果

    $ git clone git@github.com:scige/sanbaoyuan.git

    $ heroku create sanbaoyuan

    $ git push heroku master

    访问http://sanbaoyuan.herokuapp.com/,服务已经起来了,但是数据库有关的还不能用

    设置PostgreSQL

    获取数据库地址

    $ heroku config

    DATABASE_URL:                 postgres://XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXX.compute-1.amazonaws.com:5432/XXXXXXX

    再次修改配置文件config/database.yml

    production:
      adapter: postgresql
      encoding: unicode
      database: postgres://XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXX.compute-1.amazonaws.com:5432/XXXXXXX
      pool: 5
      timeout: 5000

    其他的都不需要了,这些要在原来的开发目录里修改,然后提交到git

    迁移数据库

    $ heroku run rake db:migrate

    $ heroku run rake db:seed

    马上就能看到PostgreSQL已经生效了

    编译静态文件

    $ heroku run rake assets:precompile

    这个命令已经整合到 git push heroku master 中了,不需要单独执行

    Heroku部署完成!

     

    PS: 以后每次部署新功能时

    $ git pull

    $ git push heroku master

    重置数据库,seed数据只能这样更新

    $ heroku run rake db:reset

  • 相关阅读:
    EasyUI treegrid 加载checked
    html 文字垂直居中
    SQLSERVER 2008 查询数据字段名类型
    EasyUI TreeJson
    win7 网站发布备注
    LVS Nginx HAProxy 优缺点
    快速安装laravel和依赖
    Whoops, looks like something went wrong
    View.findViewById()和Activity.findViewById()区别
    ListView下拉刷新,上拉自动加载更多
  • 原文地址:https://www.cnblogs.com/scige/p/2687329.html
Copyright © 2020-2023  润新知