# Database

## Backend Selection

```yaml
backend: "SQLITE"
```

| Backend      | Use Case                                 |
| ------------ | ---------------------------------------- |
| `SQLITE`     | เซิร์ฟเวอร์เล็ก (< 50 คน) ไม่ต้องตั้งค่า |
| `MYSQL`      | เซิร์ฟเวอร์กลาง (50-200 คน)              |
| `MARIADB`    | เหมือน MySQL                             |
| `POSTGRESQL` | เซิร์ฟเวอร์ใหญ่ (200+ คน)                |
| `MONGODB`    | NoSQL, รองรับ Atlas/Replica Set          |
| `H2`         | Embedded Java DB                         |

## SQLite (Default)

```yaml
sqlite:
  file: "data.db"
  journal-mode: "WAL"
  synchronous: "NORMAL"
```

ไม่ต้องตั้งค่าอะไรเพิ่ม ใช้งานได้ทันที

## MySQL / MariaDB

```yaml
mysql:
  host: "localhost"
  port: 3306
  database: "cakeprotect"
  username: "root"
  password: "your_password"
  table-prefix: "cp_"
  use-ssl: false
  pool:
    maximum-pool-size: 10
    minimum-idle: 2
```

## PostgreSQL

```yaml
postgresql:
  host: "localhost"
  port: 5432
  database: "cakeprotect"
  username: "postgres"
  password: "your_password"
  schema: "public"
  table-prefix: "cp_"
```

## MongoDB

```yaml
mongodb:
  uri: "mongodb://localhost:27017"
  database: "cakeprotect"
  collections:
    regions: "regions"
  auto-index: true
```

รองรับ connection string แบบ:

* Local: `mongodb://localhost:27017`
* Auth: `mongodb://user:pass@host:27017/db?authSource=admin`
* Atlas: `mongodb+srv://user:pass@cluster.mongodb.net/db`

## Redis (Cache Layer)

```yaml
redis:
  enabled: false
  host: "localhost"
  port: 6379
  password: ""
  secret: "CHANGE_ME_TO_RANDOM_SECRET"
```

{% hint style="danger" %}
ต้องเปลี่ยน `secret` จาก default! ใช้สำหรับ HMAC signing ป้องกัน packet ปลอม
{% endhint %}

## Backup

```yaml
backup:
  enabled: true
  interval-minutes: 360
  max-backups: 7
  folder: "backups"
  compress: true
```

## Migration

ย้ายข้อมูลระหว่าง backend:

```
/ps admin migrate <source> <target>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.niendsitekungz.com/configuration/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
