Hello Community,
I'm encountering a persistent and baffling error trying to access Firestore from a Python Flask application running on GKE Autopilot, and I've exhausted the standard troubleshooting steps I can think of. I'm hoping someone might have encountered something similar or can suggest other avenues to investigate.
Goal: Connect to our Firestore Native database (ID: (default), Region: us-central1) from a Python Flask app using the google-cloud-firestore library, authenticating via Workload Identity running on GKE Autopilot in us-central1. Project ID is hello-heylo.
Problem: All calls to Firestore (both read .get() and write .set() operations initiated via HTTP requests to the Flask app) consistently fail with the following error and traceback:
google.api_core.exceptions.NotFound: 404 The database (default) does not exist for project hello-heylo Please visit https://console.cloud.google.com/datastore/setup?project=hello-heylo to add a Cloud Datastore or Cloud Firestore database.
[2025-04-20 02:06:34 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2025-04-20 02:06:34 +0000] [1] [INFO] Using worker: sync
[2025-04-20 02:06:34 +0000] [7] [INFO] Booting worker with pid: 7
INFO:root:Firestore client initialized successfully (ADC, explicit database='(default)').
INFO:root:Storage client initialized successfully (ADC, implicit project).
INFO:root:Attempting to write document 'test_write_doc_v09' to collection 'gke_test_entries'...
ERROR:root:Error writing to Firestore: 404 The database (default) does not exist for project hello-heylo Please visit https://console.cloud.google.com/datastore/setup?project=hello-heylo to add a Cloud Datastore or Cloud Firestore database.
ERROR:root:--- Full Traceback ---
ERROR:root:Traceback (most recent call last):
File "/app/main.py", line 62, in test_firestore_write
doc_ref.set(test_data) # The actual write operation
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/cloud/firestore_v1/document.py", line 167, in set write_results = batch.commit(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/cloud/firestore_v1/batch.py", line 61, in commit commit_response = self._client._firestore_api.commit(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/cloud/firestore_v1/services/firestore/client.py", line 1430, in commit
response = rpc(
^^^^
File "/usr/local/lib/python3.11/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
return wrapped_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
return retry_target(
^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
_retry_error_helper(
File "/usr/local/lib/python3.11/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
raise final_exc from source_exc
File "/usr/local/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
result = target()
^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/api_core/timeout.py", line 130, in func_with_timeout
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.NotFound: 404 The database (default) does not exist for project hello-heylo Please visit https://console.cloud.google.com/datastore/setup?project=hello-heylo to add a Cloud Datastore or Cloud Firestore database.
ERROR:root:----------------------
Notably, the Firestore client initialization itself (db = firestore.Client(...)) within the Flask app succeeds without errors. The error only occurs when an actual read/write operation is attempted.
What Has Been Verified (Checks Performed): I have confirmed the following configurations are correct:
Relevant Code Snippet (main.py extract):
# Client Initialization (Example from v0.9/v0.10)
try:
db = firestore.Client(database="(default)") # Also tried without explicit DB
logging.info("Firestore client initialized successfully...")
except Exception as e:
logging.error(f"CRITICAL: Failed to initialize Firestore client: {e}")
# ...
# Example Failing Read Route (Write route fails identically)
@App.route('/test-firestore-read', methods=['GET'])
def test_firestore_read():
if db is None: return jsonify({"error": "Firestore client not initialized."}), 500
try:
collection_name = u'gke_test_entries'
doc_id = u'console_test_doc' # Document known to exist from UI test
doc_ref = db.collection(collection_name).document(doc_id)
logging.info(f"Attempting to read document '{doc_id}'...")
# --- THIS LINE FAILS with 404 DB Not Found ---
doc_snapshot = doc_ref.get()
# --- Code below is never reached ---
if doc_snapshot.exists:
logging.info(f"Successfully read document '{doc_id}'...")
return jsonify({"success": True, "data": doc_snapshot.to_dict()}), 200
else:
# ... doc not found handling ...
return jsonify({"success": False, "message": f"Document {doc_id} not found."}), 404
except Exception as e:
error_message = f"Error reading from Firestore: {e}"
full_traceback = traceback.format_exc()
logging.error(error_message)
logging.error("--- Full Traceback ---")
logging.error(full_traceback) # This shows the 404 DB not found error
logging.error("----------------------")
return jsonify({ "error": error_message, "exception_type": str(type(e)), }), 500
Question: Given that all standard configurations appear correct and have been meticulously verified, why would the Firestore backend persistently return 404 The database (default) does not exist for this service account identity when accessed via the client library from GKE Autopilot? Are there any other less common configurations, project settings, potential backend inconsistencies, or diagnostic steps I might be missing?
I'm unable to open a direct support case, so any insights or suggestions from the community would be greatly appreciated! Thank you!
Hi everyone,
Checking in on this issue as I'm still completely blocked by this very unusual Firestore behavior.
Quick Recap: My Python app on GKE Autopilot (us-central1) using Workload Identity (authenticating as GSA hello-heylo-app-sa@hello-heylo.iam.gserviceaccount.com) can successfully call Google Cloud Storage, Secret Manager, and Vertex AI APIs. However, all calls to the Cloud Firestore API (read or write) using the exact same identity/environment consistently fail with:
google.api_core.exceptions.NotFound: 404 The database (default) does not exist for project hello-heylo
The Puzzle: As detailed in my original post above ^[Link to original post if possible/needed]^, I've verified exhaustively that:
Since all standard configurations appear correct and other Google Cloud APIs work fine with the same identity setup, I'm wondering if anyone has encountered Firestore behaving this way or has suggestions for less common things to check?
Any pointers or ideas would be greatly appreciated
Thanks!
Hi @amus3dprints,
Welcome to Google Cloud Community!
It looks like you’ve covered most of the checks, but have you verified that your Google Service Account (GSA) has the roles/datastore.user
permission? Even with the Project Editor role, the GSA might still lack the specific Firestore permissions. You can refer to the Security for server client libraries documentation for the full list of required roles and permissions for Firestore.
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.
Thanks for the suggestion!I had already verified the IAM roles for the service account (hello-heylo-app-sa@hello-heylo.iam.gserviceaccount.com). It currently has both the project Editor role and the specific Cloud Datastore User (roles/datastore.user) role granted at the project level.
So, the necessary permissions granted by roles/datastore.user should definitely be present (and are also covered by the Editor role).
The puzzle remains: why does the Firestore API return '404 Database not found' for this correctly permissioned service account when called from the GKE pod (via Workload Identity), especially when the same identity mechanism works successfully for calls to GCS, Secret Manager, and Vertex AI from the same pod?
Does anyone have thoughts on other potential causes or diagnostic steps, given that the standard permissions (roles/datastore.user) are confirmed to be in place?
Thanks again!
i was having this issue too, so find out that you have to delete your current firestore database and create new one but with id of : (default)
https://github.com/firebase/firebase-admin-node/issues/2563