Monday, August 6, 2012

InvalidURIError For pg Gem - Connection String Issue

While working on my Sinatra & PostgreSQL-powered web-site (using Sequel 3.37.0 and pg 0.14.0), I encountered an odd URI-related error message. When connecting to the server, the resulting error message looked like this:

Error: URI::InvalidURIError: the scheme postgres does not accept registry part:

The root cause was that part of the connection string has a question mark in it. A question mark is used to separate the query string part of a URL from the rest of the URL. In the above connection string example, Ruby's URI-related code seems to treat the part of the connection string past the question mark as an invalid query string.
To resolve the problem, call CGI.escape to fix issues with the conection string.

Thanks

Stack Overflow - In Ruby, how do I replace the question mark character in a string?

This SO question (and the accepted answer by kmkaplan) suggested the use of CGI's escape method.