Rails 生产环境 FATAL:数据库 "db/production.postgresql" 不存在。

3

我正在按照Digitalocean的教程来设置我的Rails生产服务器。然而,在cap production deploy:initial处出现错误。

我的设置包括Ubuntu 14.04、Capistrano、Nginx和Puma在Digitalocean上。

在教程中,他们没有提到编辑database.yml文件,但我注意到需要将其编辑,因为默认的是sqlite3,而我的生产数据库是postgresql。

SSHKit::Runner::ExecuteError: Exception while executing on host      159.203.107.215: Exception while executing on host 159.203.107.215: rake     exit status: 1
rake stdout: rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "db/production.postgresql" does not exist

Database.yml

default: &default
adapter: postgresql
pool: 5
timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
<<: *default
 database: db/test.sqlite3

production:
<<: *default
database: db/production.postgresql

你有查看过这个链接吗?https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-14-04 - denys281
是的,我已经正确设置了它。 - Wraithseeker
1个回答

2

Sqlite和PostgreSQL连接所需的参数非常不同。Sqlite只需要一个指向磁盘数据库文件的路径。而PostgreSQL需要连接信息:主机名、数据库名称、用户名、密码。我建议去掉default,并将每个参数都明确指定。可以像这样:

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

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

production:
  adapter: postgresql
  pool: 5
  encoding: unicode
  database: your_db_name_here
  host: your_db_host_here
  username: your_db_username_here
  password: your_db_password_here

我建议不要在这个文件中存储实际的用户名和密码,而是通过环境获取它(可以使用dot_env gem或其他几个类似的工具)。例如:
username: <%= ENV.fetch('DATABASE_USERNAME', '') %>
password: <%= ENV.fetch('DATABASE_PASSWORD', '') %>

我遇到了 Psych::BadAlias 错误:无法加载 Rails.application.database_configuration: 未知别名:default。 - Wraithseeker
这意味着你留下了 <<: *default,但是删除了 default: 块。 - Philip Hallstrom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接