Exploring PocketBase: A Compact Database Solution with API
Written on
Chapter 1: Overview of PocketBase
PocketBase is an intriguing project that offers a single-file database packed with features such as an administrative UI, REST API, and activity logs. While SQLite is the most widely used single-file database, alternatives like DuckDB also provide noteworthy capabilities. These databases excel in mobile applications, local machines, and edge devices due to their compact size and easy management, all while enabling developers to utilize familiar SQL tools without demanding extensive resources or setup.
Download & Installation
After downloading, launch PocketBase by executing the command .pocketbase.exe serve in your command line. Once it's up and running, you can access the admin UI at http://127.0.0.1:8090/_/ to configure the admin user. A pb_data folder will be created in the directory containing the pocketbase.exe file, which will store all your SQLite database files.
Users Collection
PocketBase features an integrated users collection that allows for user creation. It includes default fields, but you can modify the table to suit your needs by adding fields such as phone numbers or first/last names, as well as relational fields for roles and permissions. This flexibility enables you to customize the database to meet your specific requirements.
By configuring an SMTP server and sender email in the settings, you can easily send out account verification and password reset emails, which is a convenient feature.
Custom Collections
Creating new collections is a simple process, with options for Base, Auth, or View types. Each collection includes several built-in system fields along with any number of custom fields. PocketBase supports common data types and provides validation and format constraints rather than individual types like CHAR or VARCHAR.
The option to have fields with select or multi-select choices is particularly useful, simplifying scenarios where a relation table would otherwise be cumbersome. Relation fields can be easily configured through the UI, allowing for references to entries in other tables or self-referencing for recursive parent-child setups.
Notably, since the database is composed of multiple SQLite .db files, you can utilize SQLite scripts or viewers to access and modify the database directly. The layout of the tables in the PocketBase .db file is user-friendly, making queries straightforward.
Built-in API
PocketBase supports SDKs for JavaScript and Dart. You can find code examples for your collections by clicking "API Preview" in the top right of the admin UI, which provides code for CRUD operations on tables. API endpoints can also be secured with permissions or custom conditions using API Rules.
For those interested in Python, a compatible SDK is available, which could work seamlessly with Streamlit applications. I plan to explore this combination further in a future post. For reference, here's a sample code snippet:
from pocketbase import PocketBase # The client works similarly
from pocketbase.client import FileUpload
client = PocketBase('http://127.0.0.1:8090')
# Authenticate as a regular user
user_data = client.collection("users").auth_with_password(
"[email protected]", "basicpassword")
# Authenticate as an admin
admin_data = client.admins.auth_with_password("[email protected]", "adminpassword")
# List and filter records in the "example" collection
result = client.collection("example").get_list(
1, 20, {"filter": 'status = true && created > "2022-08-01 10:00:00"'})
Logs
PocketBase includes an efficient logging system that allows you to monitor activities within your application and search logs using various filtering options. You can also set the retention period for logs and enable IP logging for troubleshooting REST API endpoints.
Settings
A variety of settings can be adjusted to customize database behavior, including application name, host and port configurations, import/export options, email notifications, and token settings.
Admins
You can define admin users for your database, distinct from those in the users collection. Admins have full access to the database and can bypass all API rules.
Auth Providers
PocketBase supports a range of single sign-on options, including federated options like OpenID Connect. Once these providers are enabled, they can be assigned to specific users, facilitating SSO if an OAuth app is registered with the third-party service.
Backups
The platform also offers a backup feature, with backups stored in the pg_data folder as zip files containing all database contents. These backups can be created manually through the admin UI or set to automate on a cron schedule, limited to a specified number.
In Summary
PocketBase is an exciting solution for application projects that need a fast and efficient method for data storage at the edge. Built on SQLite, it includes a REST API, backup capabilities, SSO & user authentication management, activity logging, and various SDKs for developers. PocketBase stands out as a top choice for local applications needing a user-friendly database with an intuitive admin interface. It's an excellent tool for quickly bringing ideas to fruition, utilizing well-established technologies.
Keep Up with Short Bits!
👏 If you found this article helpful or interesting, please give it a clap!
💬 If you have questions or suggestions, feel free to leave a comment! I'm always eager to engage with readers!
❤️ To stay updated on more programming, AI, technology, and Python products and news, follow me or Shortbits!
Robby Boney — Medium
Short Bits — Medium
Sign Up for Newsletter — Just a Short Bit of Tech — Medium
✨ Thank you for reading! ✨