【问题标题】:Change from SQLite to PostgreSQL in a fresh Rails project在新的 Rails 项目中从 SQLite 更改为 PostgreSQL
【发布时间】:2011-10-06 08:12:23
【问题描述】:

我有一个 Rails 应用程序,其数据库位于 SQLite(开发和生产)中。由于我要迁移到 heroku,我想将我的数据库转换为 PostgreSQL。

反正听说SQLite本地的开发数据库不用改,所以不用改,但是生产环境从SQLite改成PostgreSQL怎么办呢?

以前有没有人这样做过并且可以提供帮助?

附:我不确定这个过程到底叫什么,但我听说过将数据库从 SQLite 迁移到 PostgreSQL,需要这样做吗?

【问题讨论】:

  • 您是否有需要使用的实时生产数据,或者它是一个新的/新鲜的应用程序?
  • 我建议您也将开发环境更改为 PostgreSQL。 SQLite 和 PostgreSQL(以及所有其他数据库)对“有效 SQL”的含义有不同的看法,并且没有 ORM 可以将您与数据库的所有特性隔离开来。

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


【解决方案1】:

您可以将您的 database.yml 更改为此,而不是使用开箱即用的 sqlite 之一:

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

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

production:
  adapter: postgresql
  encoding: utf8
  database: project_production
  pool: 5
  username: 
  password:

cucumber:
  <<: *TEST

【讨论】:

  • 我应该放 project_test 还是我的数据库名称?
  • 您可以随意命名。如果您的项目名称是“计算器”,我会将它们命名为calculator_production、calculator_test、calculator_development
  • @mmichael 这真的取决于你如何设置你的 postgres。如果您使用的是 MacOS X Lion+,则使用 postgres.app、brew 或 native 对其默认设置有不同的限制。因此,如果用户名和密码不适用,您可以将它们排除在外,或者不输入任何值。这更像是一种“包罗万象”的配置。
  • '&TEST' 在里面做什么(第 9 行)?
  • "&TEST" 将 TEST 设置为默认选项集。它们以后可以被覆盖或被忽略。 "的方式
【解决方案2】:

以下步骤对我有用。它使用由 Heroku 创建并在 Ryan Bates 的 Railscast #342 中提到的 taps gem。有几个步骤,但效果很好(甚至日期也被正确迁移),而且比我过去所做的 Oracle -> DB2 或 SQL Server -> Oracle 迁移要容易得多。

请注意,SQLite 没有用户 ID 或密码,但 taps gem 需要一些东西。我只是使用了文字“用户”和“密码”。

为新数据库创建 Postgres 数据库用户

$ createuser f3
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

编辑 - 下面更新了命令 - 改为使用此命令

$ createuser f3 -d -s

创建所需的数据库

$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test

更新 Gemfile

gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle

更新 database.yml

#development:
#  adapter: sqlite3
#  database: db/development.sqlite3
#  pool: 5
#  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: f3_development
  pool: 5
  username: f3
  password:

#test:
#  adapter: sqlite3
#  database: db/test.sqlite3
#  pool: 5
#  timeout: 5000

test:
  adapter: postgresql
  encoding: unicode
  database: f3_test
  pool: 5
  username: f3
  password:

在 sqlite 数据库上启动 taps 服务器

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

迁移数据

$ taps pull postgres://f3@localhost/f3_development http://user:password@localhost:5000

重启 Rails 网络服务器

$ rails s

清理 Gemfile

#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle

【讨论】:

    【解决方案3】:

    现在只需一个命令即可轻松完成

    bin/rails db:system:change --to=postgresql
    

    【讨论】:

    • 这是一个很好的答案,它用所需的值更改了 database.yml。您仍然可以进入那里并根据您的项目更改数据库名称。
    【解决方案4】:

    由于您要迁移到 heroku,因此您可以使用点击来执行此操作:

    heroku db:push
    

    这会将您的本地开发 sqlite 数据推送到生产环境,heroku 会自动为您转换为 postgres。

    这也应该可以将生产 sqlite db 推送到 heroku,但它没有经过测试。

    RAILS_ENV=production heroku db:push
    

    【讨论】:

    【解决方案5】:

    您还需要将“gem 'pg'”行添加到您的 gemfile,'pg' 是 Rails 的当前 postgres gem。

    【讨论】:

      【解决方案6】:

      只需更新 config/database.yml 文件:

      default: &default
        adapter: postgresql
        encoding: unicode
        pool: 5
      
      development:
        <<: *default
        database: projectname_development
      
      test:
        <<: *default
        database: projectname_test
      
      production:
        <<: *default
        database: projectname_production
        username: 
        password: 
      

      上面是你运行时生成的:

      $ rails new projectname --database=postgresql --skip-test-unit
      

      将其添加到您的 Gemfile 中:

      gem 'pg'
      

      【讨论】:

        【解决方案7】:

        只需更新你的datatbase.yml

        development: &development
          adapter: postgresql
          database: Your_database_name
          username: user_name
          password: password
          host:     localhost
          schema_search_path: public
          min_messages: warning
        
        test:
          <<: *development
          database: test_database_name
        
        production:
          <<: *development
          database: production_db_name
        

        我们正在使用 Rails,应该遵循基本标准,如 DRY、Convention over Configuration 等。所以在上面的代码中,我们不会一次又一次地重复相同的代码。

        【讨论】:

          【解决方案8】:

          我上面已经提到过它,但我作为潜伏者的声誉不足以支持它。希望能引起 Rails 新手阅读此答案的更多关注:

          您还需要将“gem 'pg'”行添加到您的 gemfile,'pg' 是 Rails 的当前 postgres gem。

          ^^^ 这是除了所选答案中描述的 database.yml 文件之外的关键部分,用于将 Rails 应用程序迁移到 Postgres。

          【讨论】:

            【解决方案9】:

            在 gemfile 中用 gem pg 替换 gem 'sqlite3 后,我在推送到 Heroku 主服务器时不断收到 sqlite3 error,因为我忘记提交更新的 gemfile。只需执行以下操作即可解决此问题:

            git add .
            git commit -m 'heroku push'
            heroku create 
            git push heroku master
            

            【讨论】:

              【解决方案10】:

              这就是我的设置。如果您只使用 MRI 而不是 Jruby,则可以跳过适配器设置中的逻辑。

              defaults: &defaults
                adapter: <%= RUBY_ENGINE == 'ruby' ? 'postgresql' : 'jdbcpostgresql' %>
                encoding: unicode
                pool: 5
                timeout: 5000
              
              development:
                database: project_development
                <<: *defaults
              
              test:
                database: project_test
                <<: *defaults
              
              production:
                database: project_production
                <<: *defaults
              

              【讨论】:

                【解决方案11】:

                您可以尝试以下操作: sqlite3 development.db .dump | psql dbname username

                或尝试使用 sqlitetopgscript: http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg

                【讨论】:

                  【解决方案12】:

                  一个可能的解决方案(不适用于 heroku)是使用 yaml.db 来自:

                  http://www.railslodge.com/plugins/830-yaml-db

                  【讨论】:

                    【解决方案13】:

                    今天我遇到了同样的问题。我正在使用 Rails 4.2.8。解决方案是指定 pg gem 版本,在我的例子中,0.18.4

                    【讨论】:

                      猜你喜欢
                      • 2018-10-22
                      • 2019-05-29
                      • 2020-02-02
                      • 2015-05-03
                      • 2018-07-04
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-06-03
                      • 2011-09-30
                      相关资源
                      最近更新 更多