How do I deploy Firebase Database Security rules using the command line?

15,532

Solution 1

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

Solution 2

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",
    ]
  }
}

Solution 3

To deploy a new set of security rules with firebase cli

firebase deploy --only firestore:rules

Solution 4

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.

Share:
15,532
Seth Ladd
Author by

Seth Ladd

Product Manager at Google.

Updated on June 02, 2022

Comments

  • Seth Ladd
    Seth Ladd almost 2 years

    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!

  • LiorK
    LiorK over 7 years
    This syntax didn't work for me, please see my answer here
  • Venryx
    Venryx about 7 years
    Yes, @LiorK seems to have the correct, latest way to specify the database rules files.
  • Nick Franceschina
    Nick Franceschina over 6 years
    if using Firestore: firebase deploy --only firestore
  • PhillipJacobs
    PhillipJacobs about 2 years
    I prefer this over the ... -- only database Thanks @ChadlyM