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

# Database imports

> Import data from PostgreSQL, MySQL, SQLite, MongoDB, Firestore, DynamoDB, Redis, and Elasticsearch into KiwiFS.

KiwiFS can import rows from relational and document databases, converting each record into a markdown page with structured frontmatter.

## PostgreSQL

```bash theme={null}
kiwifs import --from postgres \
  --dsn "postgres://user:pass@localhost:5432/mydb" \
  --table users \
  --prefix people/ \
  --root ./knowledge
```

### Options

| Flag        | Description                                 |
| ----------- | ------------------------------------------- |
| `--dsn`     | PostgreSQL connection string (required)     |
| `--table`   | Table name                                  |
| `--query`   | Custom SQL query (overrides `--table`)      |
| `--columns` | Comma-separated fields to include           |
| `--prefix`  | Path prefix in KiwiFS (default: table name) |
| `--limit`   | Max rows to import                          |
| `--dry-run` | Preview without writing                     |

```bash theme={null}
kiwifs import --from postgres \
  --dsn "postgres://user:pass@localhost/mydb" \
  --query "SELECT id, name, email FROM users WHERE active = true" \
  --prefix active-users/
```

## MySQL

```bash theme={null}
kiwifs import --from mysql \
  --dsn "user:pass@tcp(localhost:3306)/mydb" \
  --table articles \
  --prefix articles/
```

Same flags as PostgreSQL. The DSN format follows Go's `mysql` driver convention.

## SQLite

```bash theme={null}
kiwifs import --from sqlite \
  --db ./data.db \
  --table entries \
  --prefix entries/
```

| Flag      | Description                             |
| --------- | --------------------------------------- |
| `--db`    | Path to SQLite database file (required) |
| `--table` | Table name                              |
| `--query` | Custom SQL query (overrides `--table`)  |

## MongoDB

```bash theme={null}
kiwifs import --from mongodb \
  --uri "mongodb://localhost:27017" \
  --database myapp \
  --collection articles \
  --prefix articles/
```

| Flag           | Description                       |
| -------------- | --------------------------------- |
| `--uri`        | MongoDB connection URI (required) |
| `--database`   | Database name (required)          |
| `--collection` | Collection name (required)        |

## Firestore

```bash theme={null}
kiwifs import --from firestore \
  --project my-gcp-project \
  --collection users \
  --prefix users/
```

| Flag           | Description                          |
| -------------- | ------------------------------------ |
| `--project`    | GCP project ID (required)            |
| `--collection` | Firestore collection name (required) |

<Note>
  Firestore requires Google Cloud credentials. Set `GOOGLE_APPLICATION_CREDENTIALS` or run within a GCP environment.
</Note>

## DynamoDB

```bash theme={null}
kiwifs import --from dynamodb \
  --table my-table \
  --region us-east-1 \
  --prefix dynamo/
```

| Flag       | Description                    |
| ---------- | ------------------------------ |
| `--table`  | DynamoDB table name (required) |
| `--region` | AWS region (required)          |
| `--prefix` | Path prefix in KiwiFS          |
| `--limit`  | Max items to import            |

<Note>
  Uses your default AWS credentials (`~/.aws/credentials`, `AWS_PROFILE`, or IAM role).
</Note>

## Redis

```bash theme={null}
kiwifs import --from redis \
  --addr "localhost:6379" \
  --pattern "article:*" \
  --prefix redis/
```

| Flag         | Description                               |
| ------------ | ----------------------------------------- |
| `--addr`     | Redis address (default: `localhost:6379`) |
| `--password` | Redis password                            |
| `--redis-db` | Redis database number (default: `0`)      |
| `--pattern`  | Key glob pattern to import (default: `*`) |
| `--prefix`   | Path prefix in KiwiFS                     |

Each matching key becomes a markdown page. Hash keys become frontmatter fields; string values become the page body.

## Elasticsearch

```bash theme={null}
kiwifs import --from elasticsearch \
  --url "http://localhost:9200" \
  --table my-index \
  --prefix search/
```

| Flag       | Description                                                |
| ---------- | ---------------------------------------------------------- |
| `--url`    | Elasticsearch URL (required)                               |
| `--table`  | Index name (required)                                      |
| `--query`  | Elasticsearch query JSON (optional, defaults to match-all) |
| `--prefix` | Path prefix in KiwiFS                                      |
| `--limit`  | Max documents to import                                    |

## MCP usage

All database imports are also available via the `kiwi_import` MCP tool:

```json theme={null}
{
  "tool": "kiwi_import",
  "arguments": {
    "from": "postgres",
    "dsn": "postgres://user:pass@localhost/mydb",
    "table": "users",
    "prefix": "people/",
    "dry_run": true
  }
}
```

<Tip>
  Use `dry_run: true` to preview what would be imported before writing any files.
</Tip>

## Output format

Each imported record becomes a markdown file:

```markdown theme={null}
---
_imported_at: "2026-04-25T18:46:15Z"
_source: users
_source_id: pg:users:42
name: Jane Doe
email: jane@example.com
role: admin
---

# Jane Doe

> Auto-imported from users (row pg:users:42)
```

Re-importing the same data is idempotent — KiwiFS compares `_source_id` and field values, skipping unchanged records.
