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

# Databases

> Provision, monitor and manage Square Cloud databases, including credential rotation and snapshots.

<Info>
  Databases are exposed by the `Databases` interface embedded in `rest.Rest`. They are available on **Standard, Pro and Enterprise** plans.
</Info>

```go theme={null}
client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()

api := rest.New(client)
```

## Creating a database

`api.CreateDatabase(opts)` provisions a new database from a `squarecloud.DatabaseCreateOptions`.

```go theme={null}
created, err := api.CreateDatabase(squarecloud.DatabaseCreateOptions{
	Name:    "my-mongo",
	Memory:  1024,
	Type:    squarecloud.DatabaseTypeMongo,
	Version: "8",
})
if err != nil {
	panic(err)
}

fmt.Println(created.ID)
fmt.Println(created.ConnectionURL) // ready to use — password already embedded
fmt.Println(created.Password)      // shown once — store securely
fmt.Println(created.Certificate)   // shown once — only for engines that issue a TLS cert
```

| Field     | Type                       | Description                                                                                                                                   |
| --------- | -------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `Name`    | `string`                   | Display name, 1–32 characters                                                                                                                 |
| `Memory`  | `int`                      | Allocated memory in MB                                                                                                                        |
| `Type`    | `squarecloud.DatabaseType` | Engine: `DatabaseTypeMongo`, `DatabaseTypeMySQL`, `DatabaseTypeRedis` or `DatabaseTypePostgres` (slugs `mongo`, `mysql`, `redis`, `postgres`) |
| `Version` | `string`                   | Engine version                                                                                                                                |

<Warning>
  `Password` (and `Certificate`, when applicable) appear **only in the creation response** and cannot be recovered later — store them now. `ConnectionURL` comes ready to use with the password embedded.
</Warning>

An unsupported type fails with `INVALID_DATABASE_TYPE`; too little memory for the plan fails with `INSUFFICIENT_MEMORY`; a failure during provisioning fails with `DATABASE_CREATION_FAILED`.

## Listing your databases

`api.GetDatabases()` lists every database you own as compact `squarecloud.UserDatabase` descriptors:

```go theme={null}
dbs, err := api.GetDatabases()
if err != nil {
	panic(err)
}

for _, db := range dbs {
	fmt.Printf("%s (%s)\n", db.Name, db.ID)
}
```

## Fetching a single database

```go theme={null}
db, err := api.GetDatabase("abc123def456abc123def456")
if err != nil {
	panic(err)
}

fmt.Println(db.ID, db.Name, db.Type, db.RAM, db.Cluster, db.CreatedAt)
```

## Lifecycle

```go theme={null}
err := api.StartDatabase("abc123def456abc123def456")
if err != nil {
	panic(err)
}

err = api.StopDatabase("abc123def456abc123def456")
```

## Status and metrics

The database status and metrics endpoints share the same shapes as the application ones (they are type aliases) — see [Managing applications](/en/sdks/go/managing_application#getting-the-application-status) for the field-by-field breakdown.

```go theme={null}
status, err := api.GetDatabaseStatus("abc123def456abc123def456")
if err != nil {
	panic(err)
}
fmt.Println(status.Status, status.CPU, status.RAM) // formatted strings

raw, err := api.GetDatabaseStatusRaw("abc123def456abc123def456") // numeric values
if err != nil {
	panic(err)
}

metrics, err := api.GetDatabaseMetrics("abc123def456abc123def456")
if err != nil {
	panic(err)
}
fmt.Println(len(metrics), "metric points (up to 288 over 24h)")
```

### Summary status for every database

```go theme={null}
list, err := api.GetDatabaseListStatus()
if err != nil {
	panic(err)
}

for _, summary := range list {
	fmt.Printf("%s → running=%v\n", summary.ID, summary.Running)
}
```

## Updating

`api.UpdateDatabase(dbID, opts)` changes the display name and/or memory allocation. At least one field must be provided.

```go theme={null}
err := api.UpdateDatabase("abc123def456abc123def456", squarecloud.DatabaseUpdateOptions{
	Name: "primary-db",
	RAM:  1024,
})
if err != nil {
	panic(err)
}
```

## Credentials

`api.GetDatabaseCertificate(dbID)` returns the TLS certificate (base64-encoded PEM):

```go theme={null}
cert, err := api.GetDatabaseCertificate("abc123def456abc123def456")
if err != nil {
	panic(err)
}
fmt.Println("certificate length:", len(cert))
```

`api.ResetDatabaseCredentials(dbID, reset)` rotates either the password or the certificate, using the `squarecloud.DatabaseReset*` constants:

```go theme={null}
// Rotate the password — the new value is shown only once
result, err := api.ResetDatabaseCredentials("abc123def456abc123def456", squarecloud.DatabaseResetPassword)
if err != nil {
	panic(err)
}
fmt.Println("new password:", result.Password)

// Rotate the certificate — fetch the new one via GetDatabaseCertificate
_, err = api.ResetDatabaseCredentials("abc123def456abc123def456", squarecloud.DatabaseResetCertificate)
```

<Warning>
  The new password is returned **only on the password reset response** and displayed a single time — store it immediately.
</Warning>

## Snapshots

Database snapshots mirror the application snapshot API — see [Snapshots](/en/sdks/go/snapshots#database-snapshots).

```go theme={null}
snapshots, err := api.GetDatabaseSnapshots("abc123def456abc123def456")
if err != nil {
	panic(err)
}

created, err := api.CreateDatabaseSnapshot("abc123def456abc123def456")
if err != nil {
	panic(err)
}
fmt.Println("download URL:", created.URL)
```

## Deleting a database

<Warning>
  Deleting a database is irreversible. Make sure you have a recent [snapshot](/en/sdks/go/snapshots) if you might need to recover the data.
</Warning>

```go theme={null}
err := api.DeleteDatabase("abc123def456abc123def456")
if err != nil {
	panic(err)
}
```
