`初始化':schemepostgres不接受registry部分:postgres:@(或坏主机名?)(URI :: InvalidURIError)与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)
有没有原因,这是行不通的?
我的database.yml
是:
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'] %>
为什么他们推荐envvariables保留!?
更新 :其实这仍然是不固定的。 它看起来像环境variables不是从
database.yml
文件中运行时,运行由cron任务运行的rails runner
来调用Sidekiq工作(使用Whenever)。 我不得不把url:
静态值,而不是现在它的工作,但考虑到docker
IP每改变一次从图像的运行,我宁愿rails runner
select适当的环境variables。 这可能与https://github.com/rails/rails/issues/19256#issuecomment-102980786有关,但解决scheme也没有工作。
重命名你的环境variables!
DATABASE_URL
由Rails保留,由ActiveRecord本身解释。 我遇到了同样的问题。
相反,使用这样的东西:
host: <%= ENV['DB_HOST'] %> password: <%= ENV['DB_PWD'] %>
在“configurationRails应用程序”指南中 ,似乎DATABASE_URL
用于configuration数据库访问。 configuration数据库访问的另一种方法是存在文件config/database.yml
。 尝试使用这两种方法来configuration数据库访问会导致您遇到的问题。
如果你的密码包含不安全的字符
用于DATABASE_URL
连接string不能包含特殊字符( [a-zA-Z0-9_~-\.]
以外的任何字符,例如@
是常见的字符)。 解决scheme是对url进行编码。
如果你正在使用dokku
- https://github.com/Kloadut/dokku-pg-plugin/issues/76 – 主机和端口信息丢失
- https://github.com/dokku/dokku-postgres/issues/28 – 重写的链接命令生成无效的主机
如果你正在使用pghero
- https://github.com/ankane/pghero – 如果密码包含@