# Import PBX Data from a Database into Grafana

Once your PBX (Private Branch Exchange, your phone system) data is flowing into a third-party database, you can surface it in Grafana. Register the database as a Grafana data source, then load the Cloud Voice dashboard template to turn your call records into charts and tables.

## Requirements

| Platform | Requirement |
| --- | --- |
| Grafana | An account with the **Organization administrator** role. |
| Third-party database | See the list below. |

For the database, make sure all of the following are in place:

- **Version**: The database version is one that Grafana supports. For details, see Grafana's reference pages for [Microsoft SQL Server](https://grafana.com/docs/grafana/latest/datasources/mssql/#supported-versions), [MySQL-compatible databases](https://grafana.com/docs/grafana/latest/datasources/mysql/#supported-databases), and [PostgreSQL-compatible databases](https://grafana.com/docs/grafana/latest/datasources/postgres/#supported-databases).
- **Network**: Grafana can reach the database over the network.
- **Account**: A login with read-only (SELECT) access to the schemas and tables you plan to query.

:::caution
Grafana does not check whether the queries it runs are safe, so a user could execute damaging SQL (Structured Query Language) statements. Create a dedicated account with the minimum permissions needed to keep that risk contained.
:::

## Prerequisites

You have already [synced your PBX data to a third-party database](/pbx/integrations/data-connector/connect-cloud-voice-cloud-voice-to-an-sql-database-using-data-connector/).

## Overview

Setting up the integration takes two stages:

1. Connect Grafana to the database.
2. Import a dashboard from the database using the Cloud Voice template.

:::note
The steps below use **PostgreSQL** as the working example. The process for **Microsoft SQL** and **MySQL** is essentially identical.
:::

## Step 1. Connect Grafana to the database

1. Sign in to Grafana and go to **Connections > Add new connection**.

   ![The Add new connection page under Connections in Grafana](/images/pbx/grafana-connections.png)

2. Add a data source:

   a. Search for your database type and pick it from the list of available data sources. In this example, choose the **PostgreSQL** data source.

   ![Selecting the PostgreSQL data source from the connection search results](/images/pbx/grafana-select-postgresql.png)

   b. In the top-right corner, click **Add new data source**.

   ![The Add new data source button on the PostgreSQL connection page](/images/pbx/grafana-add-data-source.png)

3. Enter the details Grafana needs to reach the database:

   a. Under **Connection**, provide the connection settings.

   | Item | Description |
   | --- | --- |
   | Host URL | The IP address or domain name of the machine running the database, along with the database port. |
   | Database name | The name of the database. |

   b. Under **Authentication**, provide the sign-in credentials.

   | Item | Description |
   | --- | --- |
   | Username | The username used to connect to the database. |
   | Password | The password used to connect to the database. |

4. **Optional:** Adjust any other settings your setup requires.

5. Scroll to the bottom of the page and click **Save & test** to validate and store the connection. A **Database Connection OK** message confirms that Grafana reached the database.

## Step 2. Import a dashboard using the template

1. In your browser's address bar, copy the data source UID (Unique Identifier, the ID Grafana assigns to the data source you just created), it is the final segment of the URL. You need it when editing the template.

   ![The data source UID shown at the end of the Grafana page URL](/images/pbx/grafana-database-credential.png)

   In this example, the UID is `bfkbvupmpwe0wb`.

2. Download and edit the dashboard JSON template:

   a. Download and unzip the Cloud Voice dashboard template package. It bundles templates for each supported database, pick the one that matches yours. This example uses the PostgreSQL template.

   ![The PostgreSQL dashboard template file inside the template package](/images/pbx/grafana-postgresql-template.png)

   b. In every panel, replace each `uid` value under `datasource` with the UID you copied.

   ![The datasource uid values being edited in the dashboard JSON file](/images/pbx/grafana-json-template.png)

   :::note
   Leave the `uid` values in the annotations and dashboard settings as they are.

   ![The uid values in annotations and dashboard settings left unchanged](/images/pbx/grafana-uid-unchanged.png)
   :::

   c. **Optional:** Change the `title` value. Grafana uses it as the dashboard name.

   ![The title field being set in the dashboard JSON template](/images/pbx/grafana-title.png)

3. Import the edited template into Grafana:

   a. In the left navigation bar, click **Dashboards**.

   b. In the top-right corner, click the add icon ![Add icon](/images/pbx/add-macbook.png) and choose **Import dashboard**.

   ![The Import dashboard option in the Dashboards add menu](/images/pbx/grafana-import-dashboard.png)

   c. Click **Upload dashboard JSON file** and select your edited `.json` file.

   d. Click **Import**.

   ![The dashboard import settings before confirming the import](/images/pbx/grafana-dashboard-import-settings.png)

## Result

The dashboard loads into Grafana and your PBX data appears as charts and tables.

![A Grafana dashboard populated with imported PBX call data](/images/pbx/grafana-dashboard.png)

## What to do next

Tune the SQL queries in the dashboard to control exactly which data appears.

:::note
Out of the box, the Cloud Voice template surfaces only the call reports listed below. This is because a Grafana dashboard cannot combine multiple queries drawn from a single source.

- Extension Call Accounting
- Extension Call Accounting Details
- AI Receptionist Call Activity
- Agent Missed Call Activity
- Queue Callback Activity
- Satisfaction Survey
- Satisfaction Survey Details
- IVR Report
- DID/Outbound Caller ID Activity

To show more call reports, add your own SQL queries. See [Call Report Calculations with Multiple SQL Queries](/pbx/integrations/data-connector/call-report-calculations-with-multiple-sql-queries/) for the data logic behind each report.
:::
