Configure the PostgreSQL data source
This document explains how to configure the PostgreSQL data source and lists all configuration options. For general information on managing data sources, refer to Data source management.
Before you begin
You need the
Organization administratorrole to configure the data source. You can also configure it via YAML using Grafana provisioning or using Terraform.Grafana includes a built-in PostgreSQL data source; no plugin installation is required.
Have your PostgreSQL security details ready (certificates and client keys, if using TLS/SSL).
Note your PostgreSQL version; you’ll be prompted for it during configuration.
Note
When adding a data source, the database user you specify should have only
SELECTpermissions on the relevant schemas and tables. Grafana does not validate the safety of queries, so users could run potentially harmful SQL (for example,DROP TABLE). Create a dedicated PostgreSQL user with restricted permissions to limit risk.
Example:
CREATE USER grafanareader WITH PASSWORD 'password';
GRANT USAGE ON SCHEMA schema TO grafanareader;
GRANT SELECT ON schema.table TO grafanareader;Replace schema and table with your schema and table names.
Add the PostgreSQL data source
Complete the following steps to set up a new PostgreSQL data source:
- Click Connections in the left-side menu.
- Click Add new connection.
- Type
PostgreSQLin the search bar. - Select the PostgreSQL data source.
- Click Add new data source in the upper right.
You are taken to the Settings tab where you will configure the data source.
PostgreSQL configuration options
Following is a list of PostgreSQL configuration options:
Connection section:
Authentication section:
TLS/SSL Auth Details:
If you select the TLS/SSL Mode options require, verify-ca or verify-full and file system path the following are required:
If you select the TLS/SSL Mode option require and TLS/SSL Method certificate content the following are required:
If you select the TLS/SSL Mode options verify-ca or verify-full with the TLS/SSL Method certificate content the following are required:
PostgreSQL Options:
Connection limits:
Private data source connect:
Click Manage private data source connect to be taken to your PDC connection page, where you’ll find your PDC configuration details.
After you have added your PostgreSQL connection settings, click Save & test to test and save the data source connection.
Min time interval
The Min time interval setting defines a lower limit for the
$__interval and
$__interval_ms variables.
This option can also be configured or overridden in the dashboard panel under the data source settings.
This value must be formatted as a number followed by a valid time identifier:
Provision the data source
You can define and configure the data source in YAML files with provisioning. For more information about provisioning and available configuration options, refer to Provision Grafana.
PostgreSQL provisioning example
apiVersion: 1
datasources:
- name: Postgres
type: postgres
url: localhost:5432
user: grafana
secureJsonData:
password: 'Password!'
jsonData:
database: grafana
sslmode: 'disable' # disable/require/verify-ca/verify-full
maxOpenConns: 100
maxIdleConns: 100
maxIdleConnsAuto: true
connMaxLifetime: 14400
postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
timescaledb: falseTroubleshoot provisioning issues
If you encounter metric request errors or other issues when provisioning, see Provisioning errors in the PostgreSQL troubleshooting guide.
Configure with Terraform
You can configure the PostgreSQL data source using Terraform with the Grafana Terraform provider.
For more information about provisioning resources with Terraform, refer to Grafana as code using Terraform.
Terraform example
The following example creates a basic PostgreSQL data source:
resource "grafana_data_source" "postgres" {
name = "Postgres"
type = "postgres"
url = "localhost:5432"
user = "grafana"
json_data_encoded = jsonencode({
database = "grafana"
sslmode = "disable"
maxOpenConns = 100
maxIdleConns = 100
maxIdleConnsAuto = true
connMaxLifetime = 14400
postgresVersion = 903
timescaledb = false
})
secure_json_data_encoded = jsonencode({
password = "Password!"
})
}For all available configuration options, refer to the Grafana provider data source resource documentation.
Next steps
After configuring your PostgreSQL data source, you can:
- Write queries using the query editor to explore and visualize your data.
- Use template variables to create dynamic, reusable dashboards.
- Add annotations to overlay PostgreSQL events on your panels.
- Set up alerting to create alert rules based on your PostgreSQL data (time series format only).
- Troubleshoot issues if you encounter problems with your data source.



