---
title: "The Firebase CLI: now with database commands"
description: "The Firebase CLI: now with database commands"
published: "2015-11-06"
last_updated: "2015-11-06"
authors:
  - "David East (Developer Advocate)"
categories:
  - "Firebase CLI"
  - "Realtime Database"
  - "Launch"
canonical_url: "https://firebase.blog/posts/2015/11/the-firebase-cli-now-with-database_82"
---

# The Firebase CLI: now with database commands

*Published November 6, 2015 by David East (Developer Advocate)*

---
With the latest Firebase CLI release, it can do much more than just hosting. The latest release gives you the power to read and write data from your Firebase database.

These new data commands simplify tasks like seeding, exporting, and even transferring data from one Firebase database to another. Now you can spend less time writing data seeding scripts and more time developing your app.

This article will cover a few tricks to do some common data operation tasks.

### Data seeding
To save data from the command-line use the `data:set` command.

```js
firebase data:set /messages messages.json -f my-firebase-db
```

The first argument is the path you want to write the data to and the second is the JSON file to read from. The last argument is the Firebase to execute the operation against. If you use a `/` it will save to the root and overwrite existing data. This is perfect for seeding your database.

```js
firebase data:set / seed.json -f my-firebase-db
```

When your database loses its test data or things get out of whack, you can reseed your database with a simple JSON file.

You'll get asked if you _really_ want to overwrite the data in your database. To skip this message and move with confidence you can provide the `-y` flag.

```js
firebase data:set / seed.json -f my-firebase-db -y
```

### -f is for Firebase
Most developers have a healthy fear of [flags labeled -f](https://twitter.com/heathercmiller/status/526770571728531456). But with Firebase, `-f` is your friend.

The `-f` flag enables you to specify which Firebase database you’re running the data command against.

```js
firebase data:set /status status.json -f other-firebase-db
```

This command saves the contents of `status.json` into the status path of the `other-firebase-db` Firebase database.

The `-f` flag opens up a larger set of possibilities, like transferring data to another Firebase database.

### Data exports
Reading data from the CLI is also a simple one-liner.

```js
firebase data:get /messages -f my-firebase-db
```

The `data:get` command works just like the `data:set` command. You can also provide a JSON file to store the data locally.

```js
firebase data:get /messages > messages.json -f my-firebase-db
```

If your Firebase database is under 256mb, this is a great way to create a seed to work from.

```js
firebase data:get / > seed.json -f my-firebase-db
```

### Formatting JSON
You'll notice that your JSON comes back unformatted which isn't the best for human eyes. The `data:get` command allows you to pipe the result to another command like Python’s `json.tool` (which comes installed on standard OSX instances).

```js
firebase data:get /messages -f my-firebase-db | python -m json.tool
```

The `|` is the symbol for _piping_ the output to another source. In this case the `json.tool` module consumes the piped results from the `data:get` command.

If you’re looking for something even more readable, try using the [npm module prettyjson](https://www.npmjs.com/package/prettyjson). This module formats JSON into a colored YAML format that’s easy to read from the command-line.

```js
davideast:
  age:   27
  happy: true
  name:  David East
erlichbachman:
  age:   34
  happy: false
  name:  Erlich Bachman
```

The `prettyjson` module isn't bundled with the CLI so you'll have to install it on your machine. But if you prefer another formatting module, the CLI will pipe the results there too.

```js
npm install -g prettyjson
```

### Data transfers
Transferring data from one Firebase database to another is again a simple one-liner.

```js
firebase data:get / -f my-firebase-db | firebase data:set / -f another-firebase -y
```

This command is especially helpful if you need to move data to another environment. See [our previous blog post](https://www.firebase.com/blog/2015-10-29-managing-development-environments.html) for more tips on managing multiple environments with the CLI.

### Default Firebase database
If the project directory contains a `firebase.json` file, the commands will default to the Firebase database in the JSON file. To create a `firebase.json` file just use the command:

```js
firebase init
```

Now when you run commands within that directory you can omit the `-f` flag.

```js
firebase data:get /messages messages.json
```

### bash functions
If you find yourself repeating a set of commands it’s probably time to make a bash function. Save your function to your `.bash_profile` and you’ll be able to access them from anywhere in your console.

If you commonly transfer data between Firebase databases the function below makes it simple.

```js
function transfer_to() {
  local master_db="${1}"
  local dest_db="${2}"
  local path="${3:-/}"
  firebase data:get "$path" -f "$master_db" | firebase data:set "$path" -f "$dest_db" -y
}
```

To use the function just call the `transfer_to` command with the destination Firebase database.

```js
transfer_to dev-firebase-db staging-firebase-db
```

Another useful function is for formatting data.

```js
function formatted() {
  local db="${1}"
  local path="${2:-/}"
  firebase data:get "$path" -f "$db" | python -m json.tool
}
```

The `formatted` function takes in a Firebase database and the specified path. The outputted JSON is piped to Python’s json.tool.

```js
formatted my-firebase-db /users
```

You can check out the [community project firebase-dot-files](https://github.com/davideast/firebase-dot-files) on GitHub to contribute your tricks.

_The Firebase CLI is more than just hosting. How do you seed your Firebase database? Drop us a comment and let us know if you’re using the new CLI features in your workflow._
