w3resource

How to use Prometheus for PostgreSQL Monitoring?


Monitoring PostgreSQL with Prometheus

Prometheus is a powerful open-source monitoring and alerting toolkit designed for modern applications. When integrated with PostgreSQL, it provides detailed insights into database performance, query execution, and resource usage. This article explains how to monitor PostgreSQL using Prometheus, including setup, examples, and best practices.

What is Prometheus?

Description: Prometheus is a time-series database optimized for monitoring and metrics collection. It uses a flexible query language, PromQL, to analyze metrics.

Features:

  • Multi-dimensional data collection.
  • Powerful querying with PromQL.
  • Built-in alerting system.
  • Visualizations through tools like Grafana.

Prometheus and PostgreSQL Integration

Prometheus collects PostgreSQL metrics through an exporter. The PostgreSQL Exporter acts as a bridge, exposing database metrics in a format Prometheus understands.

Setup: PostgreSQL Exporter

Step 1: Install PostgreSQL Exporter

Download and install the PostgreSQL Exporter:

# Clone the PostgreSQL Exporter repository
git clone https://github.com/prometheus-community/postgres_exporter.git

# Navigate to the repository folder
cd postgres_exporter

# Build the exporter (requires Go installed)
make build

Step 2: Configure PostgreSQL

Create a user in PostgreSQL for monitoring:

-- Create a dedicated monitoring user
CREATE USER prometheus WITH PASSWORD 'your_password';

-- Grant read-only access to system metrics
GRANT CONNECT ON DATABASE your_database TO prometheus;
GRANT USAGE ON SCHEMA pg_catalog TO prometheus;
GRANT SELECT ON ALL TABLES IN SCHEMA pg_catalog TO prometheus;

Step 3: Start the Exporter

Run the exporter with database connection details:

# Run PostgreSQL Exporter with connection settings
DATA_SOURCE_NAME="postgresql://prometheus:your_password@localhost:5432/your_database?sslmode=disable" ./postgres_exporter

Configuring Prometheus to Scrape PostgreSQL Metrics

Modify the Prometheus configuration file (prometheus.yml) to include the PostgreSQL Exporter as a scrape target:

Code:

scrape_configs:
  - job_name: 'postgresql'
    static_configs:
      - targets: ['localhost:9187']  # Default exporter port

Restart Prometheus to apply the changes:

Code:

# Restart Prometheus
systemctl restart prometheus

Querying PostgreSQL Metrics in Prometheus

Once set up, Prometheus collects metrics such as query performance, connection counts, and cache hits.

Example Query in PromQL:

Code:

pg_stat_activity_count

Explanation: This query retrieves the total number of active connections to the PostgreSQL database.

Example Query for Cache Hit Ratio:

Code:

pg_stat_database_blks_hit / (pg_stat_database_blks_hit + pg_stat_database_blks_read)

Explanation: Calculates the cache hit ratio for database reads.

Visualizing Metrics with Grafana

Prometheus metrics can be visualized using Grafana. To integrate:

    1. Install Grafana and add Prometheus as a data source.

    2. Import prebuilt PostgreSQL dashboards from Grafana’s community templates.

Key Use Cases of Monitoring PostgreSQL with Prometheus

    1. Database Performance: Monitor query latency, slow queries, and index usage.

    2. Capacity Planning: Analyze storage and connection trends for scaling.

    3. Alerting: Set up alerts for metrics like high connection counts or low cache hit ratios.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/snippets/monitoring-postgresql-with-prometheus.php