在 Rails 应用程序中更改默认的数据库并不困难,只需要进行一些简单的配置即可。
首先,你需要在应用程序的 Gemfile
中添加 pg
库,并运行 bundle install
安装它:
gem 'pg'
接下来,你可以在 config/database.yml
中更改默认的数据库连接设置。将以下内容替换为你的 PostgreSQL 数据库的详细信息:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: your_username
password: your_password
host: your_host
port: your_port
然后,运行 rails db:migrate
迁移数据库,以使更改生效。
如果你想将 PostgreSQL 作为默认数据库并禁用 SQLite3,则可以在 Gemfile
中添加以下行:
gem 'sqlite3', group: :development
gem 'pg', group: :production
这将禁用 SQLite3 在开发环境中,而启用 PostgreSQL 在生产环境中。
最后,你可以在 config/environments/development.rb
或 config/environments/production.rb
文件中启用 PostgreSQL 数据库,并设置数据库配置:
config.database_configuration_for_development = {
'adapter' => 'postgresql',
'encoding' => 'unicode',
'pool' => 5,
'username' => 'your_username',
'password' => 'your_password',
'host' => 'your_host',
'port' => your_port
}
config.database_configuration_for_production = {
'adapter' => 'postgresql',
'encoding' => 'unicode',
'pool' => 5,
'username' => 'your_username',
'password' => 'your_password',
'host' => 'your_host',
'port' => your_port
}
完成上述步骤后,你的 Rails 应用程序将使用 PostgreSQL 作为默认数据库,SQLite3 将被禁用。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云