Skip to main content
Collections are the fundamental building blocks of your PocketBase application. They define the structure, validation rules, and behavior of your data.

What are collections?

A collection in PocketBase is similar to a table in a traditional database. It defines:
  • The fields (columns) that records will have
  • Validation rules for each field
  • API rules for accessing and modifying records
  • Type-specific configurations
Every record in your PocketBase application belongs to a collection, and the collection determines what data the record can store and how it can be accessed.

Collection types

PocketBase supports three types of collections, each designed for different use cases:

Base collections

Standard collections for storing any type of data

Auth collections

Special collections with built-in authentication features

View collections

Read-only collections based on SQL queries

Core properties

All collections share these core properties:

Name and identity

Name string   // The collection name (used in API endpoints)
Id   string   // Unique stable identifier
Type string   // "base", "auth", or "view"
The collection name is used in API endpoints (/api/collections/{name}/records), while the ID is a stable identifier that doesn’t change even if you rename the collection.

API rules

Collections have five API rules that control access to records:
Controls who can fetch multiple records from the collection.
ListRule *string // nil = deny all, "" = allow all
Rules use PocketBase’s filter syntax. Set to nil to deny all access, "" to allow all, or a filter expression like "@request.auth.id != ''" for conditional access.

Fields

The Fields property contains a list of all fields in the collection:
Fields FieldsList // Ordered list of field definitions
See the Fields documentation for detailed information about available field types and their configuration.

Indexes

You can define database indexes to improve query performance:
Indexes types.JSONArray[string] // SQL index definitions
Example index definitions:
collection.AddIndex("idx_email", true, "`email`", "")
collection.AddIndex("idx_status_active", false, "`status`", "`active` = true")

Working with collections

Creating a new collection

Use the factory functions to create collections with proper defaults:
// Create a base collection
collection := core.NewBaseCollection("posts")

// Create an auth collection
users := core.NewAuthCollection("users")

// Create a view collection
stats := core.NewViewCollection("stats")

Adding fields

After creating a collection, add fields to define its structure:
collection.Fields.Add(
    &core.TextField{
        Name:     "title",
        Required: true,
        Max:      200,
    },
    &core.TextField{
        Name:     "content",
        Required: true,
        Max:      10000,
    },
    &core.BoolField{
        Name: "published",
    },
)

Saving a collection

Use the app’s DAO methods to persist collections:
if err := app.Save(collection); err != nil {
    return err
}
When you save a collection, PocketBase automatically creates or updates the corresponding database table and applies any schema changes.

System collections

PocketBase includes several system collections that start with an underscore:
  • _collections - Stores collection definitions
  • _superusers - Admin users
  • _externalAuths - OAuth2 authentication data
  • _mfas - Multi-factor authentication data
  • _otps - One-time passwords
System collections cannot be renamed or deleted, and their core fields cannot be modified. You can add custom fields to system collections, but exercise caution.

Next steps

Explore the different collection types to understand which one fits your needs:

Base collections

Learn about standard collections

Auth collections

Add user authentication

Fields reference

Explore all field types