【问题标题】:Sqlite to Postgres - existing Rails projectSqlite 到 Postgres - 现有的 Rails 项目
【发布时间】:2017-02-26 09:05:12
【问题描述】:

我是 Rails 新手。

我已经准备好部署一个项目。

我正在使用 Sqlite3,但我想切换到 Postgres。

我学习了很多教程,但没有任何效果,所以我需要你的帮助。

当我按照 Railscast 的说明进行操作时,它不起作用:[http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast][1]

当我跑步时:

rake db:migrate

返回:

rake aborted! ActiveRecord::NoDatabaseError: FATAL:  database "development" does not exis

当我跑步时..:

$ taps server sqlite://db/development.sqlite3 User password

..和我在database.yml中设置的用户,

我将“SECRET_KEY_BASE=mypassword”设置为 .env.development 文件。

这是我的 database.yml :

development:
  adapter: postgresql
  encoding: utf8
  database: development
  pool: 5
  username: FC
  password: 

test: &TEST
  adapter: postgresql
  encoding: utf8
  database: test
  pool: 5
  username: FC
  password: 

返回:

/Users/fc/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `require': cannot load such file -- rack/showexceptions (LoadError)
    from /Users/fc/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `rescue in require'
    from /Users/fc/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
    from /Users/fc/.rvm/gems/ruby-2.3.0/gems/sinatra-1.0/lib/sinatra/showexceptions.rb:1:in `<top (required)>'

PG 版本:

psql (PostgreSQL) 9.4.4

哪个 psql :

/usr/local/bin/psql

我现在迷路了,因为没有任何效果。

如何一步一步轻松完成迁移?

【问题讨论】:

  • 解决database "development" does not exist 先运行rake db:create 然后迁移
  • “rake db:create”如何知道我需要创建“开发数据库”?为什么我必须创建一个数据库,即使我已经从 sqlite3 获得了?我只想将 sqlite db 迁移到 postgres。
  • 您说您正在从 sqlite 迁移到 postgres,因此您需要先在 postgres 创建数据库然后迁移,它会知道它需要创建开发数据库,​​因为您在 database.yml提到database: development,关于保留数据,您需要将sqlite转储提供给psql
  • 当我运行 rake db:create 它返回:PG::InsufficientPrivilege: ERROR: permission denied to create database : CREATE DATABASE "development" ENCODING = 'utf8'
  • 这意味着您的 postgres 用户 FC 没有创建数据库的权限。检查此stackoverflow.com/a/31669921/4587148 以创建新用户并授予权限并更改 database.yml 中的用户

标签: ruby-on-rails ruby postgresql sqlite


【解决方案1】:

我遇到了同样的问题

 kernel_require.rb:133:in `require': cannot load such file -- rack/showexceptions

我的 ruby​​ 版本是 2.3.0,我只是临时切换到 ruby​​ 1.9.3,这个错误消失了

taps server sqlite://db/development.sqlite3 user password

现在上面的命令工作正常,所以我可以使用 taps gem

将 sqlite 迁移到 postgresql

你需要安装这些 gems

tilt '1.4.1'
rack '1.0.1'

因为当你点击下面的命令时

taps pull postgres://admin:password@localhost/db_development http://user:password@localhost:5000

你可以看到这样的错误

 ERROR: Rack::Utils::OkJson::Error: cannot encode Symbol: :schema_migrations
An error occurred but Hoptoad was not notified. To use Hoptoad, please
install the 'hoptoad_notifier' gem and set ENV["HOPTOAD_API_KEY"]

【讨论】:

    【解决方案2】:

    您可以使用此命令执行此操作。 (如果是导轨 6)

    rails db:system:change --to=postgresql
    

    查看下面的其他数据库类型列表:

    rails db:system:change --to=mysql, 
    rails db:system:change --to=sqlite3, 
    rails db:system:change --to=oracle, 
    rails db:system:change --to=frontbase, 
    rails db:system:change --to=sqlserver, 
    rails db:system:change --to=jdbc
    

    【讨论】:

      【解决方案3】:

      当我将应用程序从 sqlite3 迁移到 postgres 时,我遵循了这些步骤

      1. Remove or comment sqlite3 gem
      2. Add gem "pg" to your gem file and bundle
      3. Add your database.yml 
      4. bundle exec rake db:setup
      

      您应该启动并运行您的 postgres 服务器。

      【讨论】:

      • 我不想丢失存储在数据库中的所有数据。当您的 sqlite3 数据库中已有数据时,它是否有效?
      • 我不明白你如何通过这个过程将数据从 sqlite3 传输到 Postgres ?您如何以及何时为 Postgres 数据库设置用户/密码?
      • 您的数据库中有生产数据还是应用程序种子数据?如果不是大量数据,我建议为它编写种子,以便您可以根据需要即时重建该数据。如果是生产数据,我很抱歉你这样做了。 sqlite3 真的不适合实际生产数据。检查此链接以获取可能的工具/脚本来迁移数据本身。 wiki.postgresql.org/wiki/…
      • @Florian_CEO 当我进行此更改时,应用程序正在开发中。所以我不关心数据。是的,当我进行此更改时,数据丢失了。如果您在生产中,我建议不要采用这种方法。
      【解决方案4】:

      错误的意思是您的数据库开发不存在。如果您想在开发模式下使用 pg 启动 rails 应用程序,您必须先创建数据库:

      rake db:create
      

      但是,如果您不想部署您的应用程序并仍然在本地使用它而不会丢失任何旅游数据,只需将 sqlite3 设置为开发,将 postgres 设置为生产:

      首先,您需要将 pg gem 添加到 gemfile 中的生产组,并将 sqlite3 添加到开发组,这样您就不会丢失任何数据: 从以下位置更改您的 Gemfile:

      source 'https://rubygems.org'
      .....
      gem 'sqlite3'
      .
      .
      .
      

      到:

      source 'https://rubygems.org'
      ...
      group :development, :test do
          gem 'sqlite3'
      
      end
      
      group :production do
       gem 'pg'
      end
      .
      .
      .
      

      运行

      bundle install
      

      其次,取消注释包含 sqlite3 配置的 config.yml 生产部分并添加 postgresql 配置,如下所示:

      production:
        adapter: postgresql
        pool: 5
        timeout: 5000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 2013-10-19
        • 2021-01-30
        • 2012-04-28
        • 1970-01-01
        相关资源
        最近更新 更多