LinuxCommandLibrary

rails-db

Manage Rails application database connections

TLDR

Create databases, load the schema, and initialize with seed data

$ rails db:setup
copy

Access the database console
$ rails db
copy

Create the databases defined in the current environment
$ rails db:create
copy

Destroy the databases defined in the current environment
$ rails db:drop
copy

Run pending migrations
$ rails db:migrate
copy

View the status of each migration file
$ rails db:migrate:status
copy

Rollback the last migration
$ rails db:rollback
copy

Fill the current database with data defined in db/seeds.rb
$ rails db:seed
copy

SYNOPSIS

rails-db [OPTIONS]
or, more commonly:
bundle exec rails-db [OPTIONS]

PARAMETERS

--host <address>, -h <address>
    Specifies the IP address or hostname the web server should bind to. Defaults to localhost (127.0.0.1).

--port <number>, -p <number>
    Defines the port number on which the web server will listen. Defaults to 3000.

--environment <name>, -e <name>
    Sets the Rails environment (e.g., development, production) in which the application should load. This affects database connections and configurations.

--no-assets
    Prevents the command from serving static assets (JavaScript, CSS). Useful in environments where assets are served by a separate web server.

--version, -v
    Displays the installed version of the rails-db gem.

--help
    Shows a help message with available command-line options and usage information.

DESCRIPTION

The rails-db command is not a native Linux utility, but rather an executable provided by the rails-db Ruby gem. Its primary function is to launch a local web-based graphical user interface (GUI) for interacting with the database of a Ruby on Rails application.

Once started, this GUI allows developers and data analysts to conveniently browse database tables, view and edit individual records, execute custom SQL queries, inspect schema details, and perform basic data manipulation operations directly within their browser. It acts as an in-application database management tool, simplifying tasks that would otherwise require external database clients or complex command-line interactions. It aims to provide a user-friendly way to explore and manage application data during development and debugging.

CAVEATS

The rails-db command requires a Ruby on Rails application and the rails-db gem to be installed within that application's Gemfile. It is primarily designed for development and debugging purposes.

Due to its direct database access capabilities, exposing the rails-db interface publicly in production environments without robust authentication and authorization mechanisms is highly discouraged and poses significant security risks. It's recommended to run it only in a trusted local or private network context.

INSTALLATION AND USAGE CONTEXT

To use rails-db, you must add gem 'rails-db' to your Rails application's Gemfile and then run bundle install. The command is typically executed from the root directory of your Rails project. After launching, it will provide a URL (e.g., http://localhost:3000/rails/db) which you can open in your web browser to access the GUI.

SECURITY CONSIDERATIONS

While highly convenient for development, exercising caution is paramount when using rails-db. It bypasses ActiveRecord's security layers and directly accesses the database. Always ensure it's not accessible from untrusted networks, and consider adding authentication if it must be used outside of a strictly private environment.

HISTORY

The rails-db gem, specifically the popular version by Sebastian Grebe, emerged around 2016-2017. It was created to address a common developer need for a straightforward, in-app web interface to manage Rails application databases without resorting to external, often generic, database clients (like phpMyAdmin or Adminer). Its development aimed to streamline the process of browsing and manipulating application data directly from a browser, integrating seamlessly with the Rails environment and its database configurations.

SEE ALSO

rails(1), rails server, rails console, bundle(1), psql(1), mysql(1)

Copied to clipboard