使用Rails无法创建PostgreSQL数据库。

6

我是一名初级 Web 开发者,正在尝试开发我的第一个 Ruby on Rails 项目。 我正在按照这个指南将应用程序部署在 render.com 上。

然而,在安装 Rails 并执行以下命令以创建数据库之后(我使用的是 Ubuntu):

rails db:create

我在命令行窗口中遇到了这个问题:


We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create
Couldn't create 'mysite_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create


Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

它说找不到您的数据库。但我已经在config/database.yml中设置了:


# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: mysite_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user running Rails.
  #username: mysite

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# 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: mysite_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
#   production:
#     url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
  <<: *default
  database: mysite_production
  username: mysite
  password: <%= ENV["MYSITE_DATABASE_PASSWORD"] %>

我该怎么办?这件事让我快疯了。

我试了一个不同的命令:

        bin/rails db:create

同样也没有起作用。


我猜你在 Docker 容器中运行应用程序,是吗?如果是这样,你可能错过了安装 postgres 镜像并在容器中启动 postgres 服务所需的步骤。 - max
这可能是Rails的一个bug。有其他人可以确认吗?我已经在Rails项目的Issues选项卡中打开了它。https://github.com/rails/rails/issues/46577 - Monarch Wadia
3个回答

0

该错误信息表明服务器未运行。

您是否:

  • 安装了PostgreSQL软件包?
  • 启动了服务器?尝试运行: sudo systemctl start postgresql

/var/run/ 暗示该应用程序正在 Docker 容器中运行,因此我不确定 sudo systemctl start postgresql 是否是正确的解决方案。 - max
@max为什么不把它做成一个答案呢 :) - Luckyfella
因为这只是一个假设。@Luckyfella - max
我实际上尝试过,所以我可以发表一下意见。我的Postgres正在运行。其他的rake命令也正常工作。然而,这个问题仍然特别出现在db:create中。鉴于这个问题的最近性和我自己对此感到惊讶的事实,有一种很小的可能性,即这是Rails本身的一个bug。 - Monarch Wadia

0

默认情况下,您以默认用户身份登录,因此会发生此错误。要解决此错误,您必须以postgres用户身份登录,并使用您的用户名创建用户。

sudo su -postgres

然后按回车键。 然后您将以postgres用户登录

creatuser -s -r username

然后你可以切换回默认浏览器,按下ctrl+D即可切换回来,然后你就可以创建数据库了。

rails db:create

访问此链接以了解有关此错误的更多信息https://www.udemy.com/course/professional-rails-5-development-course/learn/lecture/10575320#overview


-1

编辑:对于大多数人来说可能不是正确的答案,因为这似乎是OP的数据库连接问题。但这确实让我困惑了一下。希望有相同情况的其他人在未来能够在这个线程中找到帮助。

首先,欢迎来到这个行业,祝你在初级旅程中好运!

最近我也遇到了完全相同的问题,并深入研究了宝石来调试此问题。

根本原因

解决问题后我没有仔细查看,但对我来说,Rails 7可能会与之前版本的Rails(?)略有不同地处理database.yml。或者我们都使用的database.yml源自错误的来源。老实说,我不确定。

无论如何,问题就在这里:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

ActiveRecord将default视为一个独立的环境,与developmenttestproduction处于同一级别。

这意味着,当您运行rails db:create时,会有一个连接用于developmenttestproduction... 然后再次用于default

由于default上未设置database名称,因此ActiveRecord智能地使用postgres的默认数据库... 这恰好被称为postgres

解决方案

只需删除default配置即可。我有点喜欢&default/<<: *default模式,所以我将defaultdevelopment环境组合在一起,如下所示:

development: &default
  <<: *default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  database: mysite_development

这使得一切都正常工作。

最终文件

(注意:未经测试。我正在使用自己的配置。)

# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#

development: &default
  <<: *default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  database: mysite_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user running Rails.
  #username: mysite

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# 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: mysite_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
#   production:
#     url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
  <<: *default
  database: mysite_production
  username: mysite
  password: <%= ENV["MYSITE_DATABASE_PASSWORD"] %>


抱歉,但这个答案完全是胡说八道。在Rails 7中,YAML解析器或Rails如何处理环境都没有任何变化。default:只是数据库配置中的哈希,实际上并没有用于任何事情,而&:default是一个YAML锚点。然后,<<: *default将该锚点扩展到其他哈希中。我不知道你的应用程序出了什么问题,但肯定不是你想象的那样,并且让每个环境都继承自dev只是错误的建议。我的意思是,如果你真的想使用一个不是default的键,你可以选择任何其他东西。 - max
1
如果你认为这个回答不友善,我很抱歉,但实际上这个回答经不起审查。 - max
@max 这并不能解释我在本地看到的情况。而且,你的开头让我感到不悦。"抱歉,但这个答案纯属胡说八道"很可能会引起任何人的防御反应;在互联网上更是如此。 - Monarch Wadia
这可能是一个 bug。有其他人可以确认一下吗?我已经在 Rails 项目的 Issues 标签中打开了它。https://github.com/rails/rails/issues/46577 - Monarch Wadia
不,这不是一个可重现的错误。您可以在源代码中查看Rails如何加载数据库配置,这使得轻松驳斥某些未指定更改使Rails将“default”视为环境的假设成为可能。这将破坏大量的Rails应用程序并引起骚动。至于您的应用程序实际上出了什么问题 - 可能是您设置了ENV["DATABASE_URL"]ENV["RAILS_ENV"]导致了意外行为。 - max
显示剩余4条评论

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