`initialize': the scheme postgres does not accept registry part: postgres:@ (or bad hostname?) (URI::InvalidURIError) with Docker

后端 未结 3 980
Happy的楠姐
Happy的楠姐 2021-02-13 12:38

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         


        
3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 13:30

    The accepted answer is wrong. There's nothing wrong with using DATABASE_URL in your database.yml, although doing so explicitly is probably redundant. The problem is with the value of the URL.

    ActiveRecord uses URI::RFC2396_Parser to parse database URLs. The error message above indicates that it's not able to parse the URL, probably because the hostname is missing, cf.

    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?))
    

    Other invalid hostnames, such as hostnames with underscores (common in docker-compose setups) will cause similar errors:

    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?))
    

    What can be especially confusing is that URI.parse() uses URI::RFC3986_Parser, which is much more forgiving, and will accept either of these "bad" URLs.

提交回复
热议问题