Postgres connection string builder gem with construction through a prompt or Ruby method.


Add this line to your application's Gemfile:

gem 'pg-url'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pg-url



  • (alias PG::URL[])

Constructs and returns a connection string built from the parameters given.


The parameters indicated with a check mark are required.

  • [ ] host - The host address that the database is hosted on. If no host is specified, then localhost is used as the host.
  • [ ] port - The port of the host address. If no port is specified (and no host is specified), then the default Postgres port 5432 will be used on localhost.
  • [x] database - The name of the Postgres database.
  • [x] username - The username of a user on the Postgres table
  • [ ] password - The password for the user specified by the username parameter. If the user does not have a password, this parameter can be left empty.


require 'pg/url' '', database: 'db', username: 'root', password: 'test')
#=> "postgres://root:[email protected]/db"

PG::URL[host: '', port: 3755, database: 'db', username: 'root']
#=> "postgres://[email protected]:3755/db"

conn = {port: '4321', database: 'users', username: 'K&fB2y'}
#=> "postgres://K&[email protected]:4321/users"


  • PG::URL.prompt (aliases PG::URL.>>, PG::URL.request)

Constructs and returns a connection string built from details specified by the user in a prompt.


To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at