`initialize':方案 postgres 不接受 Docker 的注册表部分:postgres:@(或错误的主机名?)(URI::InvalidURIError)

2024-03-15

我正在使用带有 Postgres DB Docker 容器的 Rails。看起来我运行时遇到了以下错误rails c:

/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/uri/generic.rb:204:in `initialize': the scheme postgres does not accept registry part: postgres:@ (or bad hostname?) (URI::InvalidURIError)

这不起作用有什么原因吗?

My database.yml is:

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

$DATABASE_URL被定义为

有趣的是,直到昨天它还工作了几天,今天又停止工作了。

下面是 Rails 4.2.1 生成的内容!

# As with config/secrets.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 as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#   production:
#     url: <%= ENV['DATABASE_URL'] %>

他们到底为什么会推荐保留的环境变量!?

UPDATE: 其实这个问题还没有解决。看起来环境变量没有被获取database.yml运行时的文件rails runner它由 cron 任务运行来调用 Sidekiq 工作作业(使用每当)。我不得不把url:静态值而不是让它现在工作但考虑到dockerip每次从我喜欢的图像运行时都会改变rails runner正确获取环境变量。这可能与https://github.com/rails/rails/issues/19256#issuecomment-102980786 https://github.com/rails/rails/issues/19256#issuecomment-102980786但解决方案也不起作用。


接受的答案是错误的。使用起来没有什么问题DATABASE_URL在你的database.yml, 虽然明确这样做可能是多余的 https://guides.rubyonrails.org/configuring.html#configuring-a-database。问题出在 URL 的值上。

ActiveRecord uses URI::RFC2396_Parser解析数据库 URL。上面的错误消息表明它无法解析 URL,可能是因为主机名丢失,参见。

URI::RFC2396_Parser.new.parse('postgres://postgres:@/mydb')
# Traceback (most recent call last):
#       1: from (irb):30
# URI::InvalidURIError (the scheme postgres does not accept registry part: postgres:@ (or bad hostname?))

其他无效的主机名,例如带下划线的主机名(在 docker-compose 设置中常见)将导致类似的错误:

URI::RFC2396_Parser.new.parse('postgres://postgres:postgres@my_postgres/mydb')
# Traceback (most recent call last):
#        1: from (irb):34
# URI::InvalidURIError (the scheme postgres does not accept registry part: postgres:postgres@my_postgres (or bad hostname?))

特别令人困惑的是URI.parse() uses URI::RFC3986_Parser,它更加宽容,并且会接受这些“坏”URL 中的任何一个。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

`initialize':方案 postgres 不接受 Docker 的注册表部分:postgres:@(或错误的主机名?)(URI::InvalidURIError) 的相关文章

随机推荐