# Create database

Create a database under the specified Catalog.

```text
POST https://moi.matrixorigin.cn/newmoi/catalog/database/create
```

## Preparation before calling

First [query the catalog list](list-catalogs.md) to obtain the parent catalog ID. Prepare a personal access token with access to the target workspace, the target workspace ID, and the parent catalog ID.

The example below uses:

- `$AI_STUDIO_API_KEY`: The actual personal access token, passed through the `X-API-Key` Header.
- `$WORKSPACE_ID`: Target workspace ID, passed through `X-Workspace-ID` Header.
- `$CATALOG_ID`: The parent catalog ID under which the database is to be created.

## Request example

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/catalog/database/create" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "catalog_id": '"$CATALOG_ID"',
    "name": "<DATABASE_NAME>",
    "description": "<DATABASE_DESCRIPTION>"
  }'
```

## Request body

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `catalog_id` | integer | Yes | The parent Catalog ID. |
| `name` | string | Yes | Database name, must be a valid identifier. |
| `description` | string | No | Database description. |

## Successful response

Returns `200` on success. Save `data.id` for subsequent operations.

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "id": 2
  }
}
```

The response fields are as follows.

| Field | Type | Description |
| --- | --- | --- |
| `code` | string | `OK` on success. |
| `msg` | string | `OK` on success. |
| `data.id` | integer or null | Create a new database ID; if not provided by the server, it is `null`. |

## Error response

```json
{
  "code": "ErrAlreadyExists",
  "msg": "数据库名称已存在",
  "data": null
}
```

### Common HTTP errors

```{list-table}
:header-rows: 1
:widths: 12 22 32 34

* - HTTP status code
  - error code
  - Common causes
  - Recommended actions
* - `400`
  - `ErrParamInvalid`
  - The request field is missing, or the name is not a valid identifier.
  - Check `catalog_id` and `name`.
* - `403`
  - `ErrForbidden`
  - The current identity does not have the permission to create a database in the parent Catalog.
  - Requesting permission to `database.create`.
* - `409`
  - `ErrAlreadyExists`
  - There is already a database with the same name in the parent Catalog.
  - Change the name.
* - `409`
  - `ErrReadonlySystemCatalog`、`ErrReadonlySystemDatabase`
  - The parent object is a read-only system object.
  - Select Writable Catalog.
* - `500`
  - `ErrServer`
  - The server failed to create the database.
  - Try again later.
```

## Follow-up operations

Use `data.id` [Query database details](get-database.md).
