Migrating from Firebase to PocketBase Cloud
A comprehensive guide on moving your data, authentication, and logic from Firebase to PocketBase Cloud. Save costs without losing functionality.
Migrating from Firebase to PocketBase Cloud
Firebase has been the go-to backend-as-a-service for years, but its usage-based pricing and vendor lock-in have led many developers to look for alternatives. PocketBase offers a compelling open-source alternative with a similar feature set but without the unpredictable costs.
In this guide, we’ll cover how to migrate your application from Firebase to PocketBase Cloud.
Feature Comparison
First, let’s map Firebase features to their PocketBase equivalents:
| Firebase Feature | PocketBase Equivalent |
|---|---|
| Firestore/Realtime DB | SQLite (with Real-time) |
| Authentication | Built-in Auth (Email/Pass, OAuth) |
| Storage | File Storage (Local/S3) |
| Cloud Functions | Go/JS Hooks |
| Hosting | PocketBase Cloud Hosting |
| Analytics | N/A (Use Plausible/Google Analytics) |
Migration Strategy
The migration process involves three main steps:
- Schema Replication: Recreating your data structure
- Data Transfer: Exporting from Firebase and importing to PocketBase
- Code Migration: Updating your frontend SDK calls
Step 1: Schema Replication
PocketBase uses strict schemas, unlike Firestore’s NoSQL flexibility. This is actually a good thing for data integrity.
Firestore vs. PocketBase Collections
Firestore:
// users/123
{
"name": "John Doe",
"email": "[email protected]",
"preferences": {
"theme": "dark"
}
}
PocketBase:
- Create “users” collection (built-in)
- Add “preferences” field (JSON type)
You’ll need to define your schema for each collection in the PocketBase dashboard.
Step 2: Data Transfer
We’ve created a simple migration script to help move your data. You can find it in our GitHub repository.
The general process is:
- Export Firestore data to JSON
- Map JSON fields to PocketBase schema
- Use PocketBase API to insert records
Handling Relationships
Firestore often uses subcollections or array of IDs. In PocketBase, you should
use Relation fields.
- 11 Relationships: Use a
Relationfield (Max Select = 1) - 1N Relationships: Use a
Relationfield on the child record - NM Relationships: Use a
Relationfield (Max Select = Unlimited)
Step 3: Code Migration
The PocketBase SDK is remarkably similar to Firebase’s, making the switch intuitive.
Authentication
Firebase:
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
await signInWithEmailAndPassword(auth, email, password);
PocketBase:
import PocketBase from "pocketbase";
const pb = new PocketBase("https://your-app.pocketbasecloud.com");
await pb.collection("users").authWithPassword(email, password);
Fetching Data
Firebase:
import { collection, getDocs, query, where } from "firebase/firestore";
const q = query(collection(db, "posts"), where("published", "==", true));
const querySnapshot = await getDocs(q);
PocketBase:
const records = await pb.collection("posts").getList(1, 50, {
filter: "published = true",
});
Real-time Updates
Firebase:
import { doc, onSnapshot } from "firebase/firestore";
const unsub = onSnapshot(doc(db, "posts", "sf"), (doc) => {
console.log("Current data: ", doc.data());
});
PocketBase:
pb.collection("posts").subscribe("RECORD_ID", function (e) {
console.log(e.action);
console.log(e.record);
});
Special Considerations
Security Rules
Firebase uses firestore.rules. PocketBase uses “API Rules”.
Firebase Rule:
allow read: if request.auth != null && request.auth.uid == resource.data.authorId;
PocketBase Rule:
@request.auth.id != "" && @request.auth.id = author.id
PocketBase rules are generally more concise and easier to read (SQL-like syntax).
Conclusion
Migrating to PocketBase Cloud gives you:
- Predictable pricing
- Full data ownership
- Better performance for relational data
- A simplified developer experience
Start your migration today and break free from vendor lock-in!