This project is archived and is in readonly mode.

#102 ✓resolved
Steve Purcell

[PATCH] Add "script/dbconsole"

Reported by Steve Purcell | May 3rd, 2008 @ 08:33 PM

There's no handy database analog of "script/console"; this patch addresses this by adding Rake tasks for quickly connecting to each defined database with its standard console tool, e.g. psql, mysql etc.

This exists as a separate plugin (see, but a number of people have suggested I submit it for inclusion in the core, since it's a tiny patch, and provides a feature useful to almost every Rails developer.


% rake db:console # Connect to your RAILS_ENV database

% rake db:console:production # Connect to the production database

% rake db:console:test # Connect to the test database

% rake db:console:some_other_db # Connect to some_other_db defined in database.yml

Comments and changes to this ticket

  • alpinegizmo

    alpinegizmo May 3rd, 2008 @ 08:52 PM

    This is one of those great little improvements that makes sense instantly, and you use several times a day. It's especially sweet when your project has lots of environments.

  • Gravis
  • Ben Nolan

    Ben Nolan May 3rd, 2008 @ 11:07 PM

    I use this regularly. If it's a small patch I'd love having it in core.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2008 @ 12:46 AM

    I'd use a script instead of a rake task. Here's the script/mysql I add to most apps:

    #!/usr/bin/env ruby
    require 'yaml'
    configs = YAML.load_file("#{File.dirname(__FILE__)}/../config/database.yml")
    rails_env = ARGV.shift || ENV['RAILS_ENV'] || 'development'
    unless configs.include?(rails_env)
      abort "Missing database.yml config for #{rails_env.inspect} environment."
    db = configs[rails_env]
    command = "mysql #{db['database']}"
    { 'host'      => '--host',
      'port'      => '--port',
      'socket'    => '--socket',
      'username'  => '--user',
      'password'  => '--password'
    }.each do |key, opt|
      if value = db[key]
        command << " #{opt}=#{value}"
    puts "[#{rails_env}] #{command}"
    exec command
  • Steve Purcell

    Steve Purcell May 4th, 2008 @ 11:56 AM

    Fair enough. I like the convenience of being able to execute the command from any working directory in my rails project, but nonetheless, here's a patch that adds the functionality as a script instead, using some cues from your script above.

  • Stefan Kaes

    Stefan Kaes May 5th, 2008 @ 07:45 AM

    +1. I use this all the time.

  • Steve Purcell

    Steve Purcell May 7th, 2008 @ 03:38 PM

    • Title changed from “Add db:console tasks” to “[PATCH] Add "script/dbconsole"”
  • Jeremy Kemper

    Jeremy Kemper May 7th, 2008 @ 08:21 PM

    Is find_cmd needed? AFAIK Windows doesn't require the .exe suffix and PATH resolution is handled by the shell already.

  • Steve Purcell

    Steve Purcell May 7th, 2008 @ 08:28 PM

    Yeah, good question - I think it's needed. Note that it's used both for choosing a preferred binary ("mysql5" vs "mysql") and for handling the .exe thing.

    The former case affected me personally (with either MacPorts or Debian; I forget which). The latter (.exe) case surprised me - Stefan Kaes provided a patch for an issue he'd seen. I think he uses Cygwin.

    Probably safer to leave find_cmd as-is, but by all means go with what makes the most sense to you.

  • Stefan Kaes

    Stefan Kaes May 7th, 2008 @ 09:45 PM

    without the .exe magic the correct executable will sometimes not be found on windows. the problem is that some installations add a program.bat file alongside program.exe. invoking the .bat however, will not work.

  • Repository

    Repository May 7th, 2008 @ 09:54 PM

    • State changed from “new” to “resolved”

    (from [2561732a08ae97fa44706a8eca4db147c4a7c286]) Some dbconsole tweaks. [#102 state:resolved]

  • Steve Purcell

    Steve Purcell May 7th, 2008 @ 10:09 PM

    Awesome - thanks Jeremy.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

Referenced by