Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Deploying security rules

I have created my first non (default) database but I'm struggling with updating it's security rules.

I'm using the firebase cli and I've tried:
firebase deploy --only firestore
firebase deploy --only firestore:<databaseid>

But in both cases the (default) database gets updated but not my new database.

Help? 🙂

Solved Solved
0 3 2,623
1 ACCEPTED SOLUTION

Thanks for this, I'm afraid I made a stupid typo in my sample commands.  I meant to type:
firebase deploy --only firestore

I eventually figured out the problem.  I needed to manually add entries in my firebase.json file so that the "firestore" section was an array with an object for each database. e.g.

   "firestore": [
      {
        "database": "(default)",
        "rules": "firestore.rules",
        "indexes": "firestore.indexes.json"
      },
      {
        "database": "newdb",
        "rules": "firestore.newdb.rules",
        "indexes": "firestore.newdb.indexes.json"
      }
    ],
 
After doing this and creating separate rules and index files for my new db, using:
firebase deploy --only firestore 
correctly updated the rules for both my default and new database.

Hopefully this helps someone else!
 

View solution in original post

3 REPLIES 3

It seems you're facing some challenges with updating security rules for a non-default Firebase Realtime Database using the Firebase CLI. Let's clarify the correct commands and process:

Current Commands:

  • firebase deploy --only firebase: This command is not specifically designed for deploying Realtime Database rules. It's more commonly used for deploying Firebase functions or hosting configurations.
  • firebase deploy --only firebase:<databaseId>: This syntax is not correct for deploying rules to Realtime Database instances. The <databaseId> format is generally used for deploying specific Firebase functions or hosting sites, not for database rules.

Correct Commands for Realtime Database:

  • firebase deploy --only database: This command deploys the rules defined in database.rules.json for the default Realtime Database instance.
  • firebase deploy --only database:<targetName>: To deploy rules to a non-default Realtime Database, you first need to define a target using the Firebase CLI and then deploy to that target. The <targetName> is a custom name you assign to your database instance when setting up deployment targets.

Setting Up Deployment Targets:

  1. Run firebase use --add to set up an alias for your Firebase project.
  2. Define a deployment target for your non-default database using firebase target:apply database <targetName> <databaseInstanceName>, where <targetName> is a custom name for your target, and <databaseInstanceName> is the instance name of your non-default database.

Deploying to a Non-Default Database:

  • After setting up the target, use firebase deploy --only database:<targetName> to deploy your rules to the specified non-default database.

Additional Points:

  • Ensure you are using the latest version of the Firebase CLI.
  • Verify the name and location of your rules file. It should be correctly referenced in your firebase.json.
  • If issues persist, consider using the Firebase Console for deploying your rules.
  • Deployments can take a few minutes to propagate. Wait for the deployment to complete before testing the updated rules.

Resources:

This should help you correctly update the security rules for your non-default Firebase Realtime Database.

Thanks for this, I'm afraid I made a stupid typo in my sample commands.  I meant to type:
firebase deploy --only firestore

I eventually figured out the problem.  I needed to manually add entries in my firebase.json file so that the "firestore" section was an array with an object for each database. e.g.

   "firestore": [
      {
        "database": "(default)",
        "rules": "firestore.rules",
        "indexes": "firestore.indexes.json"
      },
      {
        "database": "newdb",
        "rules": "firestore.newdb.rules",
        "indexes": "firestore.newdb.indexes.json"
      }
    ],
 
After doing this and creating separate rules and index files for my new db, using:
firebase deploy --only firestore 
correctly updated the rules for both my default and new database.

Hopefully this helps someone else!
 

That's for documenting this, I spent hours pouring over the Firebase docs with no success. Your help saved me, thanks so much for sharing.