> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/pocketbase/pocketbase/llms.txt
> Use this file to discover all available pages before exploring further.

# Collections overview

> Learn about PocketBase collections and how they structure your application data

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:

<CardGroup cols={3}>
  <Card title="Base collections" icon="database" href="/collections/base-collections">
    Standard collections for storing any type of data
  </Card>

  <Card title="Auth collections" icon="user-lock" href="/collections/auth-collections">
    Special collections with built-in authentication features
  </Card>

  <Card title="View collections" icon="eye" href="/collections/view-collections">
    Read-only collections based on SQL queries
  </Card>
</CardGroup>

## Core properties

All collections share these core properties:

### Name and identity

```go theme={null}
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:

<Tabs>
  <Tab title="List rule">
    Controls who can fetch multiple records from the collection.

    ```go theme={null}
    ListRule *string // nil = deny all, "" = allow all
    ```
  </Tab>

  <Tab title="View rule">
    Controls who can fetch individual records.

    ```go theme={null}
    ViewRule *string // nil = deny all, "" = allow all
    ```
  </Tab>

  <Tab title="Create rule">
    Controls who can create new records.

    ```go theme={null}
    CreateRule *string // nil = deny all, "" = allow all
    ```
  </Tab>

  <Tab title="Update rule">
    Controls who can update existing records.

    ```go theme={null}
    UpdateRule *string // nil = deny all, "" = allow all
    ```
  </Tab>

  <Tab title="Delete rule">
    Controls who can delete records.

    ```go theme={null}
    DeleteRule *string // nil = deny all, "" = allow all
    ```
  </Tab>
</Tabs>

<Info>
  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.
</Info>

### Fields

The `Fields` property contains a list of all fields in the collection:

```go theme={null}
Fields FieldsList // Ordered list of field definitions
```

See the [Fields documentation](/collections/fields) for detailed information about available field types and their configuration.

### Indexes

You can define database indexes to improve query performance:

```go theme={null}
Indexes types.JSONArray[string] // SQL index definitions
```

Example index definitions:

```go theme={null}
collection.AddIndex("idx_email", true, "`email`", "")
collection.AddIndex("idx_status_active", false, "`status`", "`active` = true")
```

## Working with collections

### Creating a new collection

<Tabs>
  <Tab title="Factory functions">
    Use the factory functions to create collections with proper defaults:

    ```go theme={null}
    // Create a base collection
    collection := core.NewBaseCollection("posts")

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

    // Create a view collection
    stats := core.NewViewCollection("stats")
    ```
  </Tab>

  <Tab title="Generic factory">
    You can also use the generic factory with a type parameter:

    ```go theme={null}
    collection := core.NewCollection(core.CollectionTypeBase, "posts")
    collection := core.NewCollection(core.CollectionTypeAuth, "users")
    collection := core.NewCollection(core.CollectionTypeView, "stats")
    ```
  </Tab>
</Tabs>

### Adding fields

After creating a collection, add fields to define its structure:

```go theme={null}
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:

```go theme={null}
if err := app.Save(collection); err != nil {
    return err
}
```

<Note>
  When you save a collection, PocketBase automatically creates or updates the corresponding database table and applies any schema changes.
</Note>

## 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

<Warning>
  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.
</Warning>

## Next steps

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

<CardGroup cols={3}>
  <Card title="Base collections" icon="database" href="/collections/base-collections">
    Learn about standard collections
  </Card>

  <Card title="Auth collections" icon="user-lock" href="/collections/auth-collections">
    Add user authentication
  </Card>

  <Card title="Fields reference" icon="list" href="/collections/fields">
    Explore all field types
  </Card>
</CardGroup>
