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

# Managing Databases

> In this section, you will learn how to manage databases using the Python SDK. Create, retrieve, update, delete, and monitor databases for your applications.

[Client]: client

Databases are managed services provided by Square Cloud that allow you to store and manage data for your applications. Supported databases include Redis, MongoDB, MySQL, and PostgreSQL.

## Creating a database

`client.create_database` returns a `Database` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    # Create a MongoDB database with 1024MB of memory
    database = await client.create_database(
        name='my_database',
        memory=1024,  # Memory in MB
        type='mongo',  # "redis", "mongo", "mysql", or "postgres"
        version='8.0.11'  # Optional - version will be inferred if not provided
    )
    
    print(database.id)               # Database identifier
    print(database.name)             # Database name
    print(database.type)             # Database type
    print(database.cluster)          # Database cluster
    print(database.memory)           # Memory allocated (MB)
    print(database.cpu)              # CPU allocated
    print(database.password)         # Database password
    print(database.connection_url)   # Connection URL
    print(database.certificate)      # TLS certificate
```

## Retrieving database information

`client.get_database_info` returns a `DatabaseInfo` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    database_info = await client.get_database_info(database_id='DATABASE_ID')
    
    print(database_info.id)            # Database identifier
    print(database_info.name)          # Database name
    print(database_info.type)          # Database type
    print(database_info.owner)         # Database owner user ID
    print(database_info.cluster)       # Database cluster
    print(database_info.ram)           # RAM usage in MB
    print(database_info.port)          # Database port
    print(database_info.created_at)    # Creation date (ISO 8601)
    print(database_info.created_at_datetime)  # Creation date as datetime object
```

## Getting database status

`client.get_database_status` returns a `StatusData` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    status = await client.get_database_status(database_id='DATABASE_ID')
    
    print(status.ram)      # RAM usage
    print(status.cpu)      # CPU usage percentage
    print(status.network)  # Network statistics
    print(status.running)  # Whether database is running
    print(status.storage)  # Storage usage
```

## Listing all databases

`client.all_databases_status` returns a list of `ResumedStatus` objects.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    all_databases = await client.all_databases_status()
    
    for database in all_databases:
        print(f"Database: {database.id}")
        print(f"  CPU: {database.cpu}")
        print(f"  RAM: {database.ram}")
        print(f"  Running: {database.running}")
```

## Starting a database

`client.start_database` returns a `Response` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.start_database(database_id='DATABASE_ID')
    print(f"Database started: {response.status}")
```

## Stopping a database

`client.stop_database` returns a `Response` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.stop_database(database_id='DATABASE_ID')
    print(f"Database stopped: {response.status}")
```

## Editing a database

`client.edit_database` returns a `Response` object. You can update the database name and/or memory allocation.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    # Update database name
    response = await client.edit_database(
        database_id='DATABASE_ID',
        name='new_database_name'
    )
    
    # Update memory allocation
    response = await client.edit_database(
        database_id='DATABASE_ID',
        memory=2048  # New memory in MB
    )
    
    # Update both name and memory
    response = await client.edit_database(
        database_id='DATABASE_ID',
        name='new_name',
        memory=2048
    )
    
    print(f"Database updated: {response.status}")
```

## Deleting a database

`client.delete_database` returns a `Response` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.delete_database(database_id='DATABASE_ID')
    print(f"Database deleted: {response.status}")
```

## Managing database credentials

### Getting the database certificate

`client.get_database_certificate` returns a `Certificate` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    certificate = await client.get_database_certificate(database_id='DATABASE_ID')
    
    # Save certificate to file
    certificate.save()  # Saves as 'certificate.pem' by default
    
    # Save certificate with custom filename
    certificate.save(filename='my_cert', export_to='cert')
    
    # Save certificate to specific directory
    certificate.save(dir='./certs', filename='my_cert', export_to='cert')
```

### Resetting database password

`client.reset_database_password` returns a new password string.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    new_password = await client.reset_database_password(database_id='DATABASE_ID')
    print(f"New password: {new_password}")
```

### Resetting database certificate

`client.reset_database_certificate` returns a `Response` object.

```python theme={null}
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.reset_database_certificate(database_id='DATABASE_ID')
    print(f"Certificate reset: {response.status}")
```

## Database data structures

### Database

The `Database` object represents a newly created database with full details:

| Property         | Type          | Description                                   |
| ---------------- | ------------- | --------------------------------------------- |
| `id`             | `str`         | Database unique identifier                    |
| `name`           | `str`         | Database name                                 |
| `type`           | `str`         | Database type (redis, mongo, mysql, postgres) |
| `cluster`        | `str`         | Database cluster information                  |
| `memory`         | `int`         | Memory allocated (MB)                         |
| `cpu`            | `int`         | CPU allocated (vCPU)                          |
| `password`       | `str`         | Database password                             |
| `certificate`    | `Certificate` | TLS certificate for secure connection         |
| `connection_url` | `str`         | Connection URL for the database               |

### DatabaseInfo

The `DatabaseInfo` object contains information about an existing database:

| Property     | Type  | Description                                   |
| ------------ | ----- | --------------------------------------------- |
| `id`         | `str` | Database unique identifier                    |
| `name`       | `str` | Database name                                 |
| `type`       | `str` | Database type (redis, mongo, mysql, postgres) |
| `cluster`    | `str` | Database cluster information                  |
| `owner`      | `str` | Database owner user ID                        |
| `port`       | `int` | Database port                                 |
| `ram`        | `int` | Current RAM usage (MB)                        |
| `created_at` | `str` | Creation timestamp (ISO 8601)                 |

### StatusData

The `StatusData` object contains real-time status information about a database:

| Property  | Type   | Description                          |
| --------- | ------ | ------------------------------------ |
| `ram`     | `str`  | Current RAM usage                    |
| `cpu`     | `str`  | Current CPU usage percentage         |
| `network` | `dict` | Network statistics (upload/download) |
| `running` | `bool` | Whether the database is running      |
| `storage` | `str`  | Storage usage                        |

## Supported database types

Square Cloud supports the following databases:

| Type       | Default Version | Description                                                 |
| ---------- | --------------- | ----------------------------------------------------------- |
| `redis`    | 7.4.5           | In-memory data store for caching and real-time applications |
| `mongo`    | 8.0.11          | NoSQL document database                                     |
| `mysql`    | 9.5             | Relational database                                         |
| `postgres` | 17.6            | Advanced relational database                                |
