44

In older versions of Firebase, we could add a rules section to our firebase.json file, and upload new security rules on every deploy.

How do we use the firebase-tools v3 command-line tools to deploy database security rules?

This page says that it's possible: "Rules for Firebase Storage"

This page hints that the command line tools can do it, but firebase --help and firebase deploy --help don't seem to hint at how to do it? (Apologies if I missed it...)

(related: where is the canonical doc for everything that can go into firebase.json? I found it on the old Firebase site, but can't find it via search on the new docs.)

Thanks!

4 Answers 4

57

You can use firebase deploy --only database if you only want to update database rules. It will overwrite your existing rules.

You can check out Firebase CLI Reference for more info

1
  • 13
    if using Firestore: firebase deploy --only firestore Dec 15, 2017 at 16:25
30

You can use firebase deploy or firebase deploy --only database from the command line, BUT most important:

Please note hereunder firebase.json format: The "rules" entry is under "database" entry.

It was taken from Firebase Sample code.

{
  "database": {
    "rules": "database-rules.json"
  },
  "hosting": {
    "public": "./",
    "ignore": [
      "firebase.json",
      "database-rules.json",
    ]
  }
}
26

To deploy a new set of security rules with firebase cli

firebase deploy --only firestore:rules
1
  • I prefer this over the ... -- only database Thanks @ChadlyM Apr 19, 2022 at 20:34
10

To deploy a new set of security rules, add a rules top-level key to your firebase.json.

Example firebase.json file:

{
  "rules": "firebase_rules.json",
  "hosting": {
    "public": "doc",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

The firebase_rules.json is a JSON file that contains the security rules. Here's an example:

{
  "rules": {
    ".read": false,
    ".write": false
  }
}

When you run firebase deploy, it will send the contents of firebase_rules.json to the server, replacing/updating any rules configurations.

2
  • 1
    This syntax didn't work for me, please see my answer here
    – LiorK
    Nov 27, 2016 at 20:01
  • 1
    Yes, @LiorK seems to have the correct, latest way to specify the database rules files.
    – Venryx
    Mar 10, 2017 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.