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

# Database Operations

> Run SQL queries, check Redis memory, and inspect databases, all through exec

<Frame>
  <img className="block dark:hidden" src="https://mintcdn.com/akua-1dce587a/AEEz0U2s7Do2sYaM/images/heros/ai-database-operations-light.svg?fit=max&auto=format&n=AEEz0U2s7Do2sYaM&q=85&s=88f412ea3f8539c44e157218cf6a20e2" alt="An exec call carrying a command that crosses into a pod container to run psql and redis-cli shells, returning a table-size report and a Redis memory stat as evidence" width="1536" height="864" data-path="images/heros/ai-database-operations-light.svg" />

  <img className="hidden dark:block" src="https://mintcdn.com/akua-1dce587a/AEEz0U2s7Do2sYaM/images/heros/ai-database-operations-dark.svg?fit=max&auto=format&n=AEEz0U2s7Do2sYaM&q=85&s=b5dc606a615b29023180868ff33bdc93" alt="An exec call carrying a command that crosses into a pod container to run psql and redis-cli shells, returning a table-size report and a Redis memory stat as evidence" width="1536" height="864" data-path="images/heros/ai-database-operations-dark.svg" />
</Frame>

The [exec endpoint](/ai/kubernetes-access#command-execution) isn't just for running `echo hello`. Agents use it to interact with databases, caches, and any CLI tool running inside a container, turning natural language into actual application-level operations.

## PostgreSQL: Table size report

An agent asked "what's the largest table in my Postgres?" runs `psql` inside the container:

```js theme={null}
async () => {
  const clusterId = "cls_abc123"; // resolved by the agent from conversation

  const res = await platform.request({
    method: "POST",
    path: `/v1/clusters/${clusterId}:exec`,
    body: {
      namespace: "default",
      pod: "postgres-0",
      container: "postgresql",
      command: ["psql", "-U", "postgres", "-c", `
        SELECT schemaname, tablename,
               pg_size_pretty(pg_total_relation_size(schemaname || '.' || tablename)) AS total_size,
               pg_size_pretty(pg_relation_size(schemaname || '.' || tablename)) AS data_size,
               pg_size_pretty(pg_indexes_size(schemaname || '.' || quote_ident(tablename))) AS index_size,
               n_live_tup AS row_count
        FROM pg_stat_user_tables
        ORDER BY pg_total_relation_size(schemaname || '.' || tablename) DESC
        LIMIT 15;
      `]
    }
  });
  return res.body;
}
```

The agent gets back formatted SQL output with table sizes, index sizes, and row counts. It can then reason about which tables need vacuuming, which indexes are bloated, or whether a migration is needed.

## Redis: Memory analysis

```js theme={null}
async () => {
  const clusterId = "cls_abc123"; // resolved by the agent from conversation

  const info = await platform.request({
    method: "POST",
    path: `/v1/clusters/${clusterId}:exec`,
    body: {
      namespace: "default",
      pod: "redis-0",
      container: "redis",
      command: ["redis-cli", "INFO", "memory"]
    }
  });

  const stats = await platform.request({
    method: "POST",
    path: `/v1/clusters/${clusterId}:exec`,
    body: {
      namespace: "default",
      pod: "redis-0",
      container: "redis",
      command: ["redis-cli", "INFO", "keyspace"]
    }
  });

  return { memory: info.body.output, keyspace: stats.body.output };
}
```

## Chaining queries

The agent can chain multiple queries in a single execution, checking replication lag and then inspecting the slowest queries:

```js expandable theme={null}
async () => {
  const clusterId = "cls_abc123"; // resolved by the agent from conversation

  const exec = (command) => platform.request({
    method: "POST",
    path: `/v1/clusters/${clusterId}:exec`,
    body: {
      namespace: "default",
      pod: "postgres-0",
      container: "postgresql",
      command: ["psql", "-U", "postgres", "-t", "-A", "-c", command]
    }
  }).then(r => r.body.output.trim());

  const [replLag, slowQueries, connCount, dbSize] = await Promise.all([
    exec("SELECT CASE WHEN pg_is_in_recovery() THEN extract(epoch FROM now() - pg_last_xact_replay_timestamp())::int || 's' ELSE 'primary' END"),
    exec("SELECT query, calls, mean_exec_time::int || 'ms' FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 5"),
    exec("SELECT count(*) FROM pg_stat_activity WHERE state = 'active'"),
    exec("SELECT pg_size_pretty(pg_database_size(current_database()))"),
  ]);

  return { replication_lag: replLag, slow_queries: slowQueries, active_connections: connCount, database_size: dbSize };
}
```

Four `psql` queries in parallel: replication lag, slowest queries, active connections, database size. All from one `execute` call.

## Why this matters

Database operations through traditional MCP tools would need a dedicated tool per database type (`postgres-query`, `redis-info`, `mongo-stats`), each with their own schema, parameters, and maintenance burden.

With exec + Code Mode, the agent writes the exact command it needs. It knows `psql` flags, `redis-cli` subcommands, and `mongosh` syntax from its training data. No pre-built tools required. The agent composes the right command for any database, any query.

<Note>
  Exec is non-interactive with a 30-second timeout. It's ideal for read queries and diagnostics. For long-running operations or interactive sessions, use the dashboard terminal.
</Note>

## Related topics

<CardGroup cols={2}>
  <Card title="Full-stack dashboard in minutes" icon="gauge" href="/ai/examples/dashboard-in-minutes">
    Combine database exec queries with cluster health in one dashboard.
  </Card>

  <Card title="Kubernetes access" icon="dharmachakra" href="/ai/kubernetes-access">
    The exec endpoint request and response shape.
  </Card>

  <Card title="Incident debugging" icon="bug" href="/ai/examples/incident-debugging">
    Adaptive SRE triage using pod health, events, and logs.
  </Card>

  <Card title="Code Mode in action" icon="bolt" href="/ai/code-mode-in-action">
    How agents chain multiple operations in one call.
  </Card>
</CardGroup>
