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

Pas*_*per 13 ruby postgresql ruby-on-rails ruby-on-rails-4 docker

我正在使用附加了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)
Run Code Online (Sandbox Code Playgroud)

有没有理由不这样做?

database.yml是:

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
Run Code Online (Sandbox Code Playgroud)

$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'] %>
Run Code Online (Sandbox Code Playgroud)

为什么他们会推荐保留的env变量!?

更新:实际上这仍然没有修复.看起来环境变量database.yml在运行时没有从文件中获取,rails runner这是由cron任务运行以调用Sidekiq工作者作业(使用Whenever).我不得不把url:静态值改为现在让它工作但是考虑到docker每次从图像运行时ip都会改变,我宁愿rails runner正确地选择环境变量.这可能与https://github.com/rails/rails/issues/19256#issuecomment-102980786有关,但解决方案也无效.

Til*_*ilo 12

重命名您的环境变量!

DATABASE_URL由Rails保留,由ActiveRecord本身解释.我遇到了同样的问题.

相反,使用这样的东西:

    host:     <%= ENV['DB_HOST'] %>
    password: <%= ENV['DB_PWD'] %>
Run Code Online (Sandbox Code Playgroud)


Dav*_*les 9

接受的答案是错误的。DATABASE_URL在您的 中使用没有任何问题database.yml,尽管明确地这样做可能是多余的。问题在于 URL 的值。

ActiveRecord用于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?))
Run Code Online (Sandbox Code Playgroud)

其他无效的主机名,例如带下划线的主机名(在 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?))
Run Code Online (Sandbox Code Playgroud)

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

  • 这应该是公认的答案!我所要做的就是将数据库容器从“my_db”重命名为“my-db”并调整 URL。就是这样! (2认同)

dno*_*zay 7

“配置 Rails 应用程序”指南来看,它似乎DATABASE_URL用于配置数据库访问。另一种配置数据库访问的方法是文件的存在config/database.yml。尝试使用这两种方法来配置数据库访问会导致您面临的问题。

如果您的密码包含不安全字符

用于的连接字符串DATABASE_URL不能包含特殊字符(除[a-zA-Z0-9_~-\.], eg之外的任何字符@都是容易混淆的)。解决方案是进行 url-encode。

如果您使用的是 dokku

如果您使用的是 pghero