Home > Enterprise >  Cant find my firebase real-time database when trying to import data in python
Cant find my firebase real-time database when trying to import data in python

Time:09-21

import pandas as pd
import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate("crt")
firebase_admin.initialize_app(cred,{
'databaseURL': 'url'
})

db = firestore.client()
actor_ref = db.collection('actors')

# Import data
df = pd.read_csv("./hw1_datasets/actor.csv")
tmp = df.to_dict(orient='records')
list(map(lambda x: actor_ref.add(x), tmp))

I'm running this script to import csv to my Firebase Real-time Database and it keeps saying the project does not exist or it does not contain an active Cloud Datastore or Cloud Firestore database. I created a firebase real-time database by using the same google account and I'm not sure why is it saying no database. Does Google Cloud not support Firebase's real-time database? Any help would be strongly appreicated

CodePudding user response:

Your question seems to be mixing up both Firebase Realtime Database and Firestore. While you have mentioned that you are using Firebase Realtime Database your python script is for importing data into Firestore. Please note that Firestore and Firebase Realtime Database are two different Databases.

The error message you are getting suggests that the project doesn’t have a Firestore Database. So to resolve the error please go to Firebase Console and create a Database in Firestore. After creating the Firestore database add a collection named ‘actors’. You can follow the steps mentioned here to create a Firestore database in Firebase Console.

If you want to use Firebase Realtime database you have to initialize it in the python script differently. You may look at the following as a reference to know how to initialize Firebase Realtime Database.

First you have to import ‘db’ from firebase_admin as follows

from firebase_admin import db

Then you have to create a credentials object by taking the serviceAccountKey.json file which you can generate in the Project Overview > Project Settings page in Firebase console

cred = credentials.Certificate('path/to/serviceAccountKey.json')

Next you have to initialize the database as follows -

firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://databaseName.firebaseio.com'
})

Now to access the Firebase Realtime Database you have to create a reference as follows

ref = db.reference('databaseName')

More details on how to initialize Firebase Realtime Database is here.

To read and save data to the Firebase Realtime Database you may refer to this document and this document respectively.

CodePudding user response:

The problem is in:

firebase_admin.initialize_app(cred,{
'databaseURL': 'url'
})

The databaseURL is a reference to the Realtime Database, and it seems that is not enough for your code to find the Firestore database of the project. As shown in the documentation on setting up Python access to Firestore, you will (also) need to pass the project ID before you can access Firestore:

firebase_admin.initialize_app(cred, {
  'projectId': project_id,
})

db = firestore.client()
  • Related