`initialize': the scheme postgres does not acc

2020-07-02 12:32发布

I'm using Rails with Postgres DB Docker container attached. It looks like I'm getting below error when I run 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)

Is there a reason why this isn't working?

My database.yml is:

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

$DATABASE_URL is defined

Interestingly it was working for a few days until yesterday and stopped working again today.

Below is what Rails 4.2.1 generates!

# 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'] %>

Why on earth would they recommend env variable that is reserved!?

UPDATE: Actually this is still not fixed. It looks like environment variable is not being picked up from database.yml file when running rails runner which is run by cron task to invoke Sidekiq worker job (using Whenever). I had to put url: static value instead to get it working for now but given that docker ip changes every time its run from the image I would prefer rails runner to pick up environment variables properly. This may be related to https://github.com/rails/rails/issues/19256#issuecomment-102980786 but the solution didn't work either.

2条回答
走好不送
2楼-- · 2020-07-02 13:05

Rename your environment variable!

DATABASE_URL is reserved by Rails, and interpreted by ActiveRecord itself. I ran into the same issue.

Instead, use something like this:

    host:     <%= ENV['DB_HOST'] %>
    password: <%= ENV['DB_PWD'] %>
查看更多
Juvenile、少年°
3楼-- · 2020-07-02 13:15

From the "Configuring Rails Applications" guide, it would appear that DATABASE_URL is used for configuring database access. Another way to configure database access is the presence of the file config/database.yml. Trying to use both methods to configure database access causes the issue you are facing.

if your password contains unsafe characters

Connection string used for DATABASE_URL cannot contain special characters (anything other than [a-zA-Z0-9_~-\.], e.g. @ is a common one to mess up). Solution is to url-encode.

if you are using dokku

if you are using pghero

查看更多
登录 后发表回答