---
docId: doc_9ffa57bebadc413a8be6c4f6a3488107
title: Connect a database
description: "Connect PostgreSQL or MySQL and let your agent run read-only queries."
---

<script>
  import { Note, Steps, Ways } from '$lib/components';
</script>

# Connect a database

<span id="what-youll-build"></span>

Connect a PostgreSQL or MySQL database so the agent can look up data, such as an order's status. Test the connection, then assign its tools to the agent.

## Before you start

You need an [agent](/guides/create-an-agent), its id in `AGENT_ID` and a database user with read-only permission.

For Supabase, use the **Session pooler** connection details: the direct host only has IPv6.

To connect it from the platform, see [Database](/help/integrations/functions/database).

<Steps>
<Steps.Step>

**Connect the database**

Send the password when creating the connection: it is stored as a secret and is not returned by reads. Also define which columns the agent must not see. ([Connect a database](/api-reference/databases/connect))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl https://api.platic.ai/v1/tools/databases \
  -H "Authorization: Bearer $PLATIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "store",
    "description": "Orders and customers of the online store.",
    "connectionString": "'$DATABASE_URL'",
    "hiddenColumns": ["customers.phone"]
  }'
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
import { Platic } from "platic-sdk";

const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! });
const agentId = process.env.AGENT_ID!;

const db = await platic.tools.databases.create({
  name: "store",
  description: "Orders and customers of the online store.",
  connectionString: process.env.DATABASE_URL!,
  hiddenColumns: ["customers.phone"],
});
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

Connect my store's database to Platica and hide the phone column of customers.

</Ways.Say>

Tool: [`create_database`](/mcp/tools#create_database). To keep the password out of the chat, create the connection yourself in **Integrations → Functions → Database** and ask it to continue from there.

</Ways.Way>
</Ways>

`DATABASE_URL` is the connection string, like `postgres://reader:<password>@db.acme.com:5432/store`. The response has the connection's `id` (keep it in `DATABASE_ID`) and, in `tools`, its four tools.

</Steps.Step>
<Steps.Step>

**Test it**

Test the connection to check that Platica can reach the database and see its tables. ([Test a database](/api-reference/databases/test))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl -X POST https://api.platic.ai/v1/tools/databases/$DATABASE_ID/test \
  -H "Authorization: Bearer $PLATIC_API_KEY"
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
const test = await platic.tools.databases.test(db.id);
if (!test.connected) throw new Error(`${test.error.code}: ${test.error.message}`);
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

Test the connection to the store database and tell me which tables it sees.

</Ways.Say>

Tool: [`test_database`](/mcp/tools#test_database).

</Ways.Way>
</Ways>

If `connected` is `false`, `error` says what to check. See [Test a database](/api-reference/databases/test).

</Steps.Step>
<Steps.Step>

**Give its tools to the agent**

Each tool is connected separately, with its `id`. It applies right away, without publishing the agent. ([Connect a tool](/api-reference/agents/tools/connect))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl -X PUT https://api.platic.ai/v1/agents/$AGENT_ID/tools/$TOOL_ID \
  -H "Authorization: Bearer $PLATIC_API_KEY"
```

Repeat the call with the `id` of each tool in `tools`.

</Ways.Way>
<Ways.Way name="sdk">

```ts
for (const tool of db.tools) {
  await platic.agents.tools.connect(agentId, tool.id);
}
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

Give the Sales agent the tools of the store database.

</Ways.Say>

Tool: [`connect_agent_tool`](/mcp/tools#connect_agent_tool), once per tool.

</Ways.Way>
</Ways>

</Steps.Step>
</Steps>

## Check it worked

Use [List the agent's tools](/api-reference/agents/tools/list) to check that all four database tools are assigned. Ask about a known value, such as an order's status, and check the reply.

<Note type="important">

The agent only runs `SELECT` queries, up to 200 rows per page and within the connection's time limit. To check what it will find, run a query yourself with [Query a database](/api-reference/databases/query).

</Note>

## Next steps

- [Test it before you publish](/guides/evals): a scenario can require the agent to query the database.
- [Databases](/api-reference/databases): passwords, data access and query limits.
