I cannot access a Google Firestore database other than one named "(default)". I looked other other solutions online, and added a key to my credential "databaseId" but that doesn't work. Here is my current script
def 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)
It still only puts data in the (default) data base and if that database doesn't exist, throws me an error. I checked through the Google CLI and the other database does exist.
I cannot access a Google Firestore database other than one named "(default)". I looked other other solutions online, and added a key to my credential "databaseId" but that doesn't work. Here is my current script
def 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)
It still only puts data in the (default) data base and if that database doesn't exist, throws me an error. I checked through the Google CLI and the other database does exist.
Share Improve this question edited Jan 17 at 19:29 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges asked Jan 17 at 19:21 tomtom 1,0774 gold badges16 silver badges34 bronze badges 1- Did you see stackoverflow/questions/78533417/…? – Frank van Puffelen Commented Jan 17 at 19:33
1 Answer
Reset to default 1Based 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:
db = firestore.client(database_id="your database id")