如果我不能使用'rails db:system:change --to=postgresql'命令,我需要采取哪些手动步骤?



我不明白更新database.yml.需要做什么

  1. 我必须在postgresql控制台内做任何事情吗
  2. 我需要在我的database.yml中更新什么

到目前为止,我一直在使用sqlite3运行我的开发应用程序。

使用Rails 5.0.7.2,这在当时我的机器上是默认的。

我有一种感觉,这与这个构建错误有关:

Running: rake assets:precompile
rake aborted!
Psych::BadAlias: Cannot load `Rails.application.database_configuration`:
Unknown alias: default
/tmp/build_6f080167/vendor/bundle/ruby/3.1.0/gems/railties-5.0.7.2/lib/rails/application/configuration.rb:137:in `database_configuration'

更新:

这就是我以前拥有的:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3

当我尝试转到:

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default #<--- psych error should be fixed using &default
adapter: postgresql #<--- from sqlite
encoding: utf8
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.psql
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.psql
production:
<<: *default
database: db/production.psql

我得到一个错误:

PG::ConnectionBad:致命:角色"ubuntu";不存在

我需要用户名和密码吗??我知道我在做什么

假设您不需要从当前数据库中获取任何数据:

  1. Gemfile中从sqlite3宝石切换到pg宝石

如果需要,请卸载sqlite3-gem:gem uninstall sqlite3

安装pggem:gem install pg(或将gem添加到gemfile后的bundle install)

  1. 更新您的database.yml文件:
default: &default #<--- psych error should be fixed using &default
adapter: postgresql #<--- from sqlite
encoding: utf8
pool: 5
timeout: 5000
... # each database specific settings
  1. 创建数据库:

rails db:create

如果这不起作用,它会告诉你哪个迁移失败了,你可以看到哪些表或列数据是sqlite特定的(但这是罕见的情况)

  1. 删除机器上的旧sqlite数据库文件

数据库文件的位置确实取决于您安装sqlite的方式,但请检查/usr/bin,并注意只删除该应用程序的测试和开发数据库(Mac在某些方面使用sqlite,所以不要意外删除这些文件)。这篇SO文章还有其他地方可以找到你的SQlite数据库。

最新更新