Datadog Agent for PostgreSQL
Kloudfuse collects metrics and logs from self-managed PostgreSQL deployments using the Kloudfuse observability agent. The agent discovers PostgreSQL pods automatically via Kubernetes pod annotations — no separate scraper deployment is required.
Overview
The Kloudfuse observability agent (kfuse-observability-agent) is a customised Datadog agent that runs as a DaemonSet — one pod on every Kubernetes node in your cluster.
It uses Datadog Autodiscovery: it continuously watches pod annotations prefixed with ad.datadoghq.com/ to find services to monitor.
When the agent finds a PostgreSQL pod with the right annotations, it:
-
Resolves
%%host%%to the pod’s cluster IP and%%port%%to its port — automatically, from the pod’s own network identity. -
Opens a TCP connection to PostgreSQL using the credentials in the annotation.
-
Runs the built-in
postgrescheck, queryingpg_stat_statements,pg_stat_bgwriter,pg_stat_replication, andpg_stat_database. -
Forwards the results to the Kloudfuse ingester as time-series metrics.
The flow for logs is similar: the agent tail-reads the PostgreSQL log output from the same pod and sends log lines to the Kloudfuse log ingestion endpoint.
Prerequisites
Enable pg_stat_statements
The postgres check depends on the pg_stat_statements extension for query-level metrics.
-
Add it to
shared_preload_librariesinpostgresql.conf:shared_preload_libraries = 'pg_stat_statements'iniIf you manage PostgreSQL with Helm (for example, Bitnami’s
postgresqlchart):postgresql: primary: postgresqlSharedPreloadLibraries: "pg_stat_statements"yaml -
Restart PostgreSQL, then create the extension in each database you want to monitor:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;sql
Create a Monitoring User
The postgres check connects using a dedicated low-privilege user. Follow Datadog’s pre-flight steps to create this user and grant the required permissions.
At minimum, the user needs:
CREATE USER kfmon WITH PASSWORD '<password>' CONNECTION LIMIT 5;
GRANT pg_monitor TO kfmon; -- PostgreSQL 10+
GRANT SELECT ON pg_stat_database TO kfmon;
GRANT SELECT ON pg_stat_statements TO kfmon;
On PostgreSQL 14+, also grant access to the statements info view:
GRANT SELECT ON pg_stat_statements_info TO kfmon;
pg_monitor (PostgreSQL 10+) grants read-only access to all pg_stat_* views.
CONNECTION LIMIT 5 prevents the monitoring user from consuming connection slots needed by the application — the Datadog Agent postgres check uses a single persistent connection per instance.
For PostgreSQL 9.6 and earlier, replace the pg_monitor role with the explicit grants in the Datadog documentation.
Also ensure pg_hba.conf allows connections from the Kubernetes pod network CIDR.
Verify the Observability Agent Is Running
Confirm DaemonSet pods are running on the same nodes as your PostgreSQL pods:
kubectl get pods -n <kloudfuse-namespace> -l app=kfuse-observability-agent -o wide
kubectl get pods -n <postgresql-namespace> -l app.kubernetes.io/name=postgresql -o wide
Every node that runs a PostgreSQL pod must also have a healthy observability agent pod.
Configure Autodiscovery Annotations
The observability agent picks up the PostgreSQL check when your pod carries the following annotations. Add them to the pod template of your PostgreSQL Deployment, StatefulSet, or Helm chart values.
Metrics
podAnnotations:
ad.datadoghq.com/postgresql.check_names: '["postgres"]'
ad.datadoghq.com/postgresql.init_configs: '[{}]'
ad.datadoghq.com/postgresql.instances: >-
[{
"host": "%%host%%",
"port": 5432,
"username": "kfmon",
"password": "<password>",
"tags": ["env:<environment>", "service:postgresql"]
}]
%%host%% is an Autodiscovery template variable that the agent replaces with the pod’s cluster IP at runtime.
The agent on the same node resolves the IP automatically — you do not specify it manually.
If you use the Bitnami postgresql chart, add the annotations under both primary.podAnnotations and readReplicas.podAnnotations:
postgresql:
primary:
podAnnotations:
ad.datadoghq.com/postgresql.check_names: '["postgres"]'
ad.datadoghq.com/postgresql.init_configs: '[{}]'
ad.datadoghq.com/postgresql.instances: >-
[{"host": "%%host%%", "port": 5432, "username": "kfmon", "password": "<password>", "tags": []}]
readReplicas:
podAnnotations:
ad.datadoghq.com/postgresql.check_names: '["postgres"]'
ad.datadoghq.com/postgresql.init_configs: '[{}]'
ad.datadoghq.com/postgresql.instances: >-
[{"host": "%%host%%", "port": 5432, "username": "kfmon", "password": "<password>", "tags": []}]
Verify Metrics Are Arriving
After restarting the agent, confirm metrics are arriving in the Kloudfuse UI:
-
Click the Metrics tab, then select Explorer from the drop-down menu.
-
Set the time range to the last 15 minutes using the interval picker in the top-right corner.
-
In the metric selector, type
postgresql_connectionsand select it from the suggestions list. Confirm a chart appears with data points. -
Use the label filters to narrow results by database or host — for example, filter by
db="mydb"to isolate metrics for a specific database.
Key metrics collected by the postgres check:
| Metric | Description |
|---|---|
|
Number of active client connections |
|
Rows fetched per second by database |
|
DML activity rates |
|
Transaction commit and rollback rates |
|
Deadlock count per interval |
|
Buffers written by the checkpointer |
|
Replication lag in bytes (primary only) |
|
Buffer cache hit ratio |
|
Per-query execution statistics from |
To verify logs are arriving, click the Logs tab, filter by source postgresql in the Filters panel, and confirm log entries appear in the list.
Troubleshooting
No PostgreSQL Metrics in Kloudfuse
If postgresql_connections returns no data:
-
Check that the observability agent pod is on the same node as the PostgreSQL pod:
kubectl get pods -n <kloudfuse-namespace> -l app=kfuse-observability-agent -o wide kubectl get pods -n <postgresql-namespace> -l app.kubernetes.io/name=postgresql -o wide -
Run the check manually from the agent pod:
kubectl exec -n <kloudfuse-namespace> <agent-pod-name> -- agent check postgres -
Check agent logs for connection errors:
kubectl logs -n <kloudfuse-namespace> <agent-pod-name> | grep -i "postgres"
Authentication Error in Agent Logs
If the agent logs show FATAL: password authentication failed for user "kfmon":
-
Confirm the password in the
ad.datadoghq.com/postgresql.instancesannotation matches the PostgreSQL user password. -
Verify the user exists:
SELECT usename FROM pg_user WHERE usename='kfmon'; -
Confirm
pg_hba.confallows connections from the pod network CIDR.