Adding Data Sources
Data sources are the foundation of Grafana. They provide the connection between Grafana and your data, whether it’s metrics, logs, traces, or other time-series data. This guide covers how to add, configure, and manage data sources in Grafana.Prerequisites
Before you begin:- Grafana is installed and running
- You have Organization Admin permissions
- You have connection details for your data source (URL, credentials, etc.)
- Network connectivity to your data source
Understanding Data Sources
A data source represents a connection to a backend system that stores data. Grafana supports:- Time-series databases: Prometheus, InfluxDB, Graphite
- SQL databases: PostgreSQL, MySQL, Microsoft SQL Server
- Logging systems: Loki, Elasticsearch
- Tracing systems: Tempo, Jaeger, Zipkin
- Cloud monitoring: CloudWatch, Azure Monitor, Google Cloud Monitoring
- Many more: See the full list
Only users with the Organization Admin role can add or modify data sources.
Built-in Data Sources
Grafana includes these core data sources:Prometheus
Time-series metrics database
Loki
Log aggregation system
Tempo
Distributed tracing backend
Elasticsearch
Search and analytics engine
PostgreSQL
Relational database
MySQL
Relational database
CloudWatch
AWS monitoring service
Azure Monitor
Azure monitoring service
TestData
Built-in test data for demos
Adding Your First Data Source
Step 1: Access Data Source Management
- Click Connections in the left sidebar
- Click Data sources under “Your connections”
- Click Add data source
Step 2: Select a Data Source Type
Let’s add a Prometheus data source as an example.- Search for Prometheus in the search box
- Click on Prometheus to select it
Step 3: Configure Basic Settings
Each data source has specific configuration requirements.Name and Default
- Name:
Prometheus-Production - Default: Toggle ON to make this the default data source
The default data source is automatically selected when creating new panels.
Connection Settings
- URL:
http://prometheus:9090 - Access: Server (default) - Grafana backend accesses the data source
Server vs Browser Access
Server vs Browser Access
Server (Recommended)
- Grafana backend makes requests to the data source
- Works with data sources not accessible from browsers
- Better security (credentials stay on server)
- Supports alerting
- User’s browser makes requests directly
- Useful for publicly accessible data sources
- Subject to CORS restrictions
- Does not support alerting
Step 4: Configure Authentication
Depending on your data source, you may need to configure authentication.Basic Authentication
Custom HTTP Headers
TLS Client Authentication
Step 5: Configure Additional Settings
Timeout
- HTTP method: POST (recommended for Prometheus)
- Timeout: 60 seconds
Query Settings (Prometheus-specific)
Step 6: Test the Connection
- Scroll to the bottom of the page
- Click Save & Test
- You should see: “Data source is working”
Configuring Specific Data Sources
Prometheus
- Scrape interval: Match your Prometheus scrape interval (default: 15s)
- Query timeout: Maximum time for query execution
- Custom query parameters: Add custom parameters to all queries
Loki (Logs)
Loki Config
- Max lines: Limit number of log lines returned (default: 1000)
- Derived fields: Extract values from logs for linking
- Timeout: Query timeout in seconds
PostgreSQL
PostgreSQL Config
- SSL Mode: disable, require, verify-ca, verify-full
- SSL Root Certificate: Path to CA certificate
- SSL Client Certificate: Path to client certificate
- SSL Client Key: Path to client key
MySQL
MySQL Config
- Max open connections: Maximum concurrent connections
- Max idle connections: Maximum idle connections in pool
- Connection lifetime: Maximum time a connection can be reused (seconds)
Elasticsearch
Elasticsearch Config
- Index name: Pattern for log indices (supports wildcards)
- Time field name: Field containing timestamps
- Max concurrent shard requests: Limit concurrent requests
- Log message field: Field containing log messages
- Log level field: Field containing log levels
CloudWatch (AWS)
CloudWatch Config
- AWS SDK Default: Uses IAM role or environment credentials
- Access & Secret Key: Manual credential entry
- Credentials file: Path to AWS credentials file
IAM Policy
Tempo (Tracing)
Tempo Config
- Trace to logs: Link traces to log data source
- Trace to metrics: Link traces to metrics data source
- Search: Configure TraceQL query editor
- Service graph: Enable service graph visualization
Data Source Permissions
Data source permissions are available in Grafana Enterprise and Grafana Cloud.
Permission Levels
- Query: Can query the data source
- Edit: Can query and modify data source configuration
- Admin: Full control including permissions management
Assigning Permissions
- Navigate to Connections → Data sources
- Click on the data source
- Go to the Permissions tab
- Click Add a permission
- Select:
- User, Service Account, Team, or Role
- The entity to grant permission
- Permission level (Query, Edit, or Admin)
- Click Save
Query Caching
Query caching is available in Grafana Enterprise and Grafana Cloud.
Enable Caching
- Navigate to the data source settings
- Go to the Cache tab
- Click Enable
- Configure cache settings:
Cache Settings
- In-memory: Simple but limited by available RAM
- Redis: Recommended for production
- Memcached: Alternative distributed cache
Per-Panel Cache Override
Override cache TTL for specific panels:- Edit the panel
- Expand Query options
- Set Cache timeout in milliseconds
Data Source Variables
Create dashboard variables that allow users to switch between data sources.- In dashboard settings, click Variables
- Click Add variable
- Configure:
Variable Config
${datasource} as the data source.
Provisioning Data Sources
Automate data source configuration using YAML files. Create/etc/grafana/provisioning/datasources/datasources.yaml:
datasources.yaml
Managing Data Sources
Edit Data Source
- Navigate to Connections → Data sources
- Click on the data source name
- Modify settings
- Click Save & Test
Delete Data Source
- Navigate to Connections → Data sources
- Click on the data source
- Scroll to the bottom
- Click Delete
- Confirm deletion
Test Data Source
Regularly test data source connectivity:- Open the data source settings
- Click Save & Test
- Verify “Data source is working” message
Building Dashboards from Data Sources
After configuring a data source, quickly create a dashboard:- Navigate to Connections → Data sources
- Click on the data source
- Click Build a dashboard
- Grafana creates a new dashboard with the data source pre-selected
Best Practices
Descriptive Names
Use clear names like
Prometheus-Production instead of prom1Default Data Source
Set your most-used data source as default
Test Connections
Always test data source connections after configuration
Secure Credentials
Use environment variables for sensitive data
Connection Pooling
Configure appropriate pool sizes for SQL databases
Query Timeouts
Set reasonable timeouts to prevent slow queries
Security Recommendations
Use Read-Only Credentials
Use Read-Only Credentials
- Create dedicated database users with read-only permissions
- Limit access to only necessary databases/tables
- Avoid using admin or root credentials
PostgreSQL Example
Enable TLS/SSL
Enable TLS/SSL
- Always use encrypted connections in production
- Verify certificate validity
- Use certificate pinning when possible
Network Isolation
Network Isolation
- Place Grafana and data sources in the same network
- Use private IPs when possible
- Implement firewall rules
- Consider using VPN or SSH tunneling
Rotate Credentials
Rotate Credentials
- Regularly update data source passwords
- Use secrets management (Vault, AWS Secrets Manager)
- Implement credential rotation policies
- Monitor for unauthorized access
Next Steps
Create Dashboards
Build visualizations with your data
User Management
Control access to data sources
Troubleshooting
Connection Timeout
Connection Timeout
Symptoms: “Connection timeout” or “Unable to connect”Solutions:
- Verify network connectivity:
ping data-source-host - Check firewall rules allow Grafana → Data source
- Increase timeout settings
- Verify data source service is running
- Check DNS resolution:
nslookup data-source-host
Authentication Failed
Authentication Failed
Symptoms: “401 Unauthorized” or “403 Forbidden”Solutions:
- Verify credentials are correct
- Check user has necessary permissions
- Confirm authentication method (Basic, Token, etc.)
- Review data source logs for errors
- Test credentials using curl or similar tool
TLS/SSL Errors
TLS/SSL Errors
Symptoms: “Certificate verify failed” or “SSL handshake failed”Solutions:
- Verify CA certificate is correct
- Check certificate expiration
- Ensure certificate hostname matches URL
- Add custom CA certificates to Grafana
- Try disabling certificate verification (non-production only)
Slow Queries
Slow Queries
Symptoms: Panels take long to load or timeoutSolutions:
- Reduce time range in queries
- Optimize data source queries
- Enable query caching
- Increase query timeout
- Check data source performance
- Use query result transformations
No Data Returned
No Data Returned
Symptoms: Panel shows “No data”Solutions:
- Verify time range includes data
- Check query syntax
- Confirm data exists in data source
- Review data source permissions
- Enable query inspector to see actual query
- Check browser console for errors