Google are excited to announce support for multiple Firestore databases in a Google Cloud project. You can now create multiple databases in a project to isolate customer data, microservices, or dev/test/staging environments. 

The multiple databases feature includes support for the following:

  • Firestore database CRUD management: Firestore now exposes a new API endpoint, Terraform resource, gcloud CLI command and Firebase CLI command to manage the lifecycle of one or more Firestore databases.
  • Conditional Identity Access Management control: You can apply different security policies to different Firestore databases with IAM Conditions or Firebase Security Rules.
  • Support for both Firestore database modes: You can create new databases, in the same project, in either Firestore Native Mode or Datastore Mode.
  • Support for all Firestore regions: You can create new databases, in the same project, in any supported Firestore region.
  • Billing: You can now track the cost of your databases separately through Cloud Billing and BigQuery. This makes it easier to both observe consumption and budget for each databases’ usage.
  • Cloud Monitoring: Firestore’s Cloud Monitoring Metrics and Stats are now aggregated at the database-level. 

Example walkthrough

Sound like a good candidate for your application? Let’s take an end-to-end example to see how it works. 

To create a Firestore database, you need to specify a database identifier, mode, and location. There are multiple ways to create a Firestore database. Here is an example of how to create a Firestore native database named reviews in the us-west1 location using the gcloud CLI:

$ gcloud firestore databases create --database=test --location=us-west1 --type=firestore-native

Once your database is created, you can access it using the Firestore/Datastore console.

When you no longer need a Firestore database, you can delete the Firestore database by running.

$ gcloud alpha firestore databases delete --database=test

That’s everything you need to do. Once a database is deleted, Firestore performs the data deletion on your behalf.

When using the Firestore client libraries with your newly created databases, specify the database name to get the appropriate Firestore database instance. If you don’t specify a database name, the client library falls back to the (default) database. Here’s an example for writing to a database named “test” with the Firebase Admin SDK for Java:

// Initialize Firebase App
FirebaseApp app = FirebaseApp.getInstance();
// Initialize Firestore Database with database id `test`
FirebaseFirestore db = FirebaseFirestore.getInstance(app, “test”)
// Document Reference 
DocumentReference docRef = db.collection(“col”).document(“doc”);
// Write Document
waitFor(docRef.set(Collections.singletonMap(“foo”, “bar”)));

For examples in additional languages, see Accessing your database.

Next steps

For more information on how to set up and configure multiple databases on Firestore, check out the documentation.