Firebase Tutorial

A written tutorial on Google Firebase's Realtime Database — what a NoSQL, real-time store is, how it compares to PostgreSQL, and how to connect to one from Python.

Resources

What is BaaS?

What is Firebase?

How the Realtime Database works

Firebase Realtime Database vs. PostgreSQL

Feature Firebase Realtime Database PostgreSQL
Type NoSQL (JSON-based, hierarchical) SQL (relational)
Structure Schema-less; data stored as JSON Structured; tables, rows, and relationships
Querying Limited; hierarchical traversal Full SQL querying, joins, and aggregations
Scalability Built for real-time sync on mobile/web apps Scales for complex queries and enterprise workloads
Transactions Basic, with limited capabilities ACID-compliant, for complex operations
Data consistency Event-driven; eventual consistency Strong consistency with transactional integrity
Offline support Built-in offline mode for mobile apps Needs additional configuration
Security Rules-based, with Firebase Authentication Fine-grained roles, permissions, and encryption
Use cases Real-time chat, live updates, IoT Enterprise apps, financial systems, analytics
Table generated by Microsoft Copilot.

A couple of terms

Diagram of a hierarchical database structure

Where Firebase fits

Since it's built around a real-time database, Firebase is a strong fit for:

Getting started with Firebase

All you need to begin is a Firebase app with a Realtime Database attached.

  1. Go to firebase.google.com
  2. Click "Get started in console"
  3. Create an app and follow the prompts
  4. Under "Build" in the sidebar, choose Realtime Database and create one (use Test Mode for tutorials)

Connecting from your terminal

  1. Install the Firebase Admin SDK: pip install firebase_admin
  2. Create a project directory and a Python file (e.g. main.py)
  3. Copy the template below and swap in your own credentials:
import firebase_admin

cred_obj = firebase_admin.credentials.Certificate('....path to file')
default_app = firebase_admin.initialize_app(cred_obj, {
    'databaseURL': databaseURL
})

from firebase_admin import db
ref = db.reference("/")

import json
with open("objects.json", "r") as f:
    file_contents = json.load(f)
ref.set(file_contents)

Adding objects to the database

{
    "Object": {
        "Attribute1": "Beep",
        "Attribute2": "Boop"
    }
}

Run the Python script, and the objects should appear in your Firebase Realtime Database console.

← Back to portfolio