This is a supplement to the dbi module, allowing you to avoid hard-coding
passwords in your programs that make database connections. It can also
be used as a general password storage mechanism for other types of
connections, e.g. ssh, ftp, etc.


MS Windows users have these additional requirements:

* sys-admin
* win32-file-attributes
* win32-dir
* win32-process


gem install dbi-dbrc


require 'dbi/dbrc'
include DBI

dbrc ='mydb')


dbrc ='mydb', 'someUser')

puts dbrc.db
puts dbrc.user
puts dbrc.driver
puts dbrc.timeout
puts dbrc.max_reconn
puts dbrc.interval
puts dbrc.dsn

Notes on the .dbrc file

This module relies on a file in your home directory called ".dbrc", and it
is meant to be analogous to the ".netrc" file used by programs such as
telnet. The .dbrc file has several conditions that must be met by the
module or it will fail:

* Permissions must be set to 600 (Unix only).
* Must be hidden (MS Windows only).
* Must be owned by the current user.
* Must have database, user and password. Other fields are optional.
* Must be in the following space-separated format (in the 'plain' version):

database user password driver timeout maximum_reconnects interval

e.g. mydb     dan    mypass     oracle   10        2         30

You may include comments in the .dbrc file by starting the line with a
"#" symbol.

A failure in any of the rules mentioned above will result in a DBRC::Error
being raised. In addition, the file may also be encrypted on MS Windows
systems, in which case the file will automatically be (temporarily)

The format for XML (using the example above) is as follows:

  <database name="mydb">

The format for YAML is as follows:

- mydb:
   user: dan
   password: mypass
   driver: oracle
   interval: 30
   timeout: 10
   max_reconn: 2



The current version of this library, returned as a String.

Class Methods, user=nil, dir=nil)

The constructor takes one to three arguments. The first argument is the
database name. This *must* be provided. If only the database name is
passed, the module will look for the first database entry in the .dbrc
file that matches.

The second argument, a user name, is optional. If it is passed, the
module will look for the first entry in the .dbrc file where both the
database *and* user name match.

The third argument, also optional, specifies the directory where DBRC will
look for the .dbrc file. By default, it looks in the pwuid (present
working user id) home directory. The rules for a .dbrc file still apply.

MS Windows users should read the "Notes" section for how your home directory
is determined.

Instance Methods


The name of the database. Note that the same entry can appear more than
once, presumably because you have multiple user id's for the same


An alias for DBRC#database.


Sets the database to +database+.  This is generally discouraged because
it does not automatically reset the dsn.


An alias for DBRC#database=.


A valid user name for that database.


Sets the user name to +user+.


The password for that user.


An alias for DBRC#password.


Sets the password to +password+.


An alias for DBRC#password=.


The driver type for that database (Oracle, MySql, etc).


Sets the driver to +driver+.  This use is discouraged because it does
not reset the dsn.


The timeout period for a connection before the attempt is dropped.


An alias for DBRC#timeout, provided purely for the sake of backwards


Sets the timeout value to +int+.


The maximum number of reconnect attempts that should be made for the the
database.  Presumablly, you would use this with a "retry" within a rescue


An alias for DBRC#maximum_reconnects.


Sets the maximum number of reconnect attempts to +max+.


An alias for DBRC#maximum_reconnects.


The number of seconds to wait before attempting to reconnect to the database
again should a network/database glitch occur.


Sets the interval seconds between connection attempts.


Returns a string in "dbi:<driver>:<database>" format.


Sets the dsn string to +dsn+.  This method is discouraged because it does
not automatically reset the driver or database.

Canonical Example

# This is a basic template for how I do things:

require 'dbi/dbrc'
require 'timeout'

db ="somedb")
n = db.max_reconn

    DBI.connect(db.dsn, db.user, db.passwd)
rescue DBI::Error
  n -= 1
  if n > 0
    sleep db.interval
rescue TimeoutError
  # handle timeout error

Notes for MS Windows Users

The 'home' directory for Win32 users is determined by ENV['USERPROFILE'].
If that is not set, ENV['HOME'] is used. If that is not set, then
the directory found by the sys-admin library is used.

To make your file hidden, right click on the .dbrc file in your Explorer
window, select "Properties" and check the "Hidden" checkbox.

I was going to require that the .dbrc file be encrypted on MS Windows,
but that may require an official "certificate", assigned to you by a third
party, which is a bit much to expect. However, if the file is encrypted,
DBRC will attempt to decrypt it, parse it, and encrypt it again when done

Notes on running the test suite

I cannot guarantee that the .dbrc files under the +examples+
subdirectories maintain the appropriate properties. This can cause
failures for the test suite (which uses these files).

The only solution is to perform a 'chmod 600 .dbrc' (on Unix) or set
the properties to 'hidden' (on MS Windows) manually, for the file in


These "methods" don't really do anything. They're simply meant as a
convenience mechanism for you dbi connections, plus a little bit of
obfuscation (for passwords).

Adding your own configuration

If you want to add your own type of configuration file, you can still use
the dbi-dbrc library. All you need to do is:

* subclass DBRC
* redefine the +parse_dbrc_config_file+ method (a private method).

Take a look at the XML and YML subclasses in dbrc.rb for two examples that
you can work from.

Future Plans


Known Bugs

I'm not positive about the dsn strings for databases other than Oracle.
If it's not correct, please let me know.
(C) Copyright 2002-2015, Daniel J. Berger, all rights reserved.


This package is provided "as is" and without any express or
implied warranties, including, without limitation, the implied
warranties of merchantability and fitness for a particular purpose


Daniel J. Berger