Reputation: 21
So I have Firebase project using Cloud Firestore service. So far I used the (default) database but since it is the same used in live environment I need to create another native database, associated to the same project, for development purposes.
Apparently this operation (with native databases) is possible just from July 2023, see here: https://cloud.google.com/blog/products/databases/manage-multiple-firestore-databases-in-a-project
However, the link above provides an example on how to access a specific database using Firebase Admin SDK for Java but not for Python, which is the one I need.
Anyone knows which is the corresponding Python code?
At the bottom of the page they say "For examples in additional languages, see 'link'." but I wasn't able to find anything.
I tried modifying the "options" and "name" parameter in the method firebase_admin.initialize_app() but it doesn't work (sometimes it doesn't even throw errors but then it still targets the default database and not the new one).
Thanks
Upvotes: 2
Views: 2393
Reputation: 28
@Zambrella's answer is correct, but I want to expand on it by noting that you do not need to pass the initialized app to firestore.client()
if you did not define the 'name' parameter when calling initialize_app()
.
default_app = firebase_admin.initialize_app(cred, {'databaseURL': db_url})
firestore = firestore.client()
firestore._database_string_internal = (
"projects/{project-id}/databases/{database-name}"
)
This effectively allows you to manage multiple Firestore databases within the Firebase default app.
Upvotes: 1
Reputation: 91
I was looking for a way to connect to different Firestore databases using the Python Admin SDK as well. It seems like it is in the works and there are a couple of PRs for the GitHub issue.
I did find a workaround:
staging_app = firebase_admin.initialize_app(
cred,
name="staging",
)
staging_db = firestore.client(app=staging_app)
staging_db._database_string_internal = (
"projects/{project-id}/databases/{database-id}"
)
Using firebase-admin
version 6.5.0
.
Hope this helps.
Upvotes: 5
Reputation: 31
You are probably using the firestore.client() function from the admin-sdk, which takes the firestore application as an argument, returning an instance of the API client. You can directly instantiate the client that takes the project, credentials and database name as parameters:
client = firestore.Client(project, credential, database)
Pay attention to the first capital "C".
Make sure you have updated the admin sdk to >=6.2.0 or the google-cloud-firestore client >= 2.12.0
Just for reference: https://github.com/googleapis/python-firestore/blob/1547351efe14f0675a4ca43d90a4d1e8363373fa/tests/system/test_system.py#L71
Upvotes: 3