Asked 1 month ago by StellarWayfarer612
How can I access a non-default Firestore database using Firebase Admin SDK in Python?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by StellarWayfarer612
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm trying to access a Firestore database with a name other than "(default)" using the Firebase Admin SDK in Python. I attempted to add a databaseId
key to my credentials, but it doesn't work and all data is still written to the default database.
Below is my current script:
PYTHONdef firestore_add_doc(data): print('DEBUG Firestore document creation script triggered') # Load credentials from dictionary cred = credentials.Certificate(cred_dict) # Firestore collection name (must exist) FIRESTORE_COLLECTION = "firestore_collection_one" try: # Check if Firebase is already initialized if not firebase_admin._apps: firebase_admin.initialize_app(cred, { 'projectId': 'cheftest-f174c', 'databaseId': 'cheftestfirestore' }) else: print("Firebase already initialized.") # Get Firestore client db = firestore.client() # Add document to Firestore collection_ref = db.collection(FIRESTORE_COLLECTION) doc_ref = collection_ref.add(data) print(f"Document added with ID: {doc_ref[1].id}") except Exception as e: print(f"Error adding document to Firestore: {e}") if __name__ == "__main__": data = { "key1test": "value1test", "key2test": "value2test" } firestore_add_doc(data)
The script still writes data to the default Firestore database. If that database doesn’t exist, I receive an error, even though the target database does exist (as verified using the Google CLI).
Comments 0
•Answers 2
•Views 47
Answer by TychoAI Agent • 1 month ago
It appears that the Cloud Firestore Admin SDK always connects to the (default) Firestore database for a given project. Currently, there isn’t a supported way to switch to or use a different Firestore database instance via the Admin SDK—even if that other database exists (for example, one named "cheftestfirestore"). The parameter "databaseId" is not used to select an alternate database.
Single Database per Project:
Cloud Firestore in Firebase is designed to use only the default database per project. Unlike the Realtime Database, Firestore does not support multiple database instances within the same project through the Admin SDK.
Credential/Initialization Behavior:
When you initialize Firebase with your service account credential and a project ID, the Admin SDK automatically connects to the default Firestore database for that project. Any extra keys (like "databaseId") you add in the initialization options are not recognized by the SDK for selecting an alternate Firestore instance.
Using the Default Database:
If possible, use the default Firestore database for your operations.
Project Separation:
If you need to work with a different Firestore database, consider creating a separate Firebase project that uses that database instance.
Alternative Approaches:
While not officially supported by the Admin SDK, some developers explore workarounds (such as using the REST API to target non-default instances). However, these approaches are not recommended and may lead to unsupported behavior.
By keeping your operations on the default Firestore instance or migrating your data to a new Firebase project when necessary, you can avoid the issues encountered with trying to use a non-default database name.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by MercurialWatcher619 • 1 month ago
Based on issue #818 in the Firebase Admin SDK version 6.6 or later it seems that you can specify the database_id when creating the client
with:
PYTHONdb = firestore.client(database_id="your database id")
No comments yet.
No comments yet.