# How-to-deploy-postgres-exporter-on-SaaS-to-monitor-postgres-custom-query_704971984
The community repo of postgres-exporter is [https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus-postgres-exporter](https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus-postgres-exporter)

The image verison of postgres-expoter we used is v0.12.0. we have scaned the image security.

![](attachments/704971984/704971891.png)

**Preparation steps:**

Create a dedicated postgres read user to access database.

CREATE USER grafana\_reader WITH PASSWORD '\*\*\*\*\*';

GRANT CONNECT ON DATABASE xservices\_ems TO grafana\_reader;  
GRANT CONNECT ON DATABASE xservices\_rms TO grafana\_reader;  
GRANT CONNECT ON DATABASE idm TO grafana\_reader;

GRANT CONNECT ON DATABASE bo\_ats TO grafana\_reader;

\\c xservices\_ems  
GRANT USAGE ON SCHEMA maas\_admin TO grafana\_reader;  
GRANT SELECT ON ALL TABLES IN SCHEMA maas\_admin TO grafana\_reader;

\\c xservices\_rms  
GRANT USAGE ON SCHEMA maas\_admin TO grafana\_reader;  
GRANT SELECT ON ALL TABLES IN SCHEMA maas\_admin TO grafana\_reader;

\\c idm  
GRANT USAGE ON SCHEMA idm TO grafana\_reader;  
GRANT SELECT ON ALL TABLES IN SCHEMA idm TO grafana\_reader;

\\c postgres  
GRANT USAGE ON SCHEMA public TO grafana\_reader;  
GRANT SELECT ON ALL TABLES IN SCHEMA public TO grafana\_reader;

\\c bo\_ats  
GRANT USAGE ON SCHEMA bo\_db\_user TO grafana\_reader;  
GRANT SELECT ON ALL TABLES IN SCHEMA bo\_db\_user TO grafana\_reader;

GRANT pg\_read\_all\_stats TO grafana\_reader;

**Create function based on the basic SQL query:**

1. To query the tenantID cross-database, use the dblink extension. The SELECT statement from dblink will return a table. Check if the extension has been created.

SELECT \* FROM pg\_extension WHERE extname = 'dblink';

CREATE EXTENSION dblink;

2\. use maas\_admin to login xservices\_ems,and run below alert default command to let feature newly created table with select permission.

ALTER DEFAULT PRIVILEGES IN SCHEMA maas\_admin GRANT SELECT ON TABLES TO grafana\_reader;

3\. Create functions in xservice\_ems with postgres user. Here is the query definition.

For xservices\_ems database: [database\_monitoring\_functions\_definition.txt](attachments/704971984/704971933.txt)

For xservices\_rms database: [database\_monitoring\_functions\_definition\_xservices\_rms.txt](attachments/704971984/711822858.txt)

4\. Create index for the query, **this step is very important, please confirm this with expert.**

create index "ops\_transaction\_context\_<tenant\_id>\_timestamp" on transaction\_context\_<tenant\_id> (transaction\_timestamp);

create index ops\_transaction\_context\_<tenant\_id>\_entity\_type on transaction\_context\_<tenant\_id> ((entity::jsonb ->> 'entity\_type'));

This needs to be scripted and performed for every tenant in the farm.

**Installation steps:**

1. Download the helm chart [prometheus-postgres-exporter.zip](attachments/704971984/704972022.zip)
2. Replace the <grafana\_reader\_password> and <smax-postgres-hostname> with actual value in the values\_database\_level.yaml,values\_dead\_tuple.yaml and values\_instance\_level.yaml.
3. Add User query in values\_database\_level.yaml file.
	```
	userQueries: |-
	    nascm_transaction_context_queue_alltenants:
	      query: "select tid,row_count from get_transaction_context_counts();"
	      master: true
	      cache_seconds: 1200
	      metrics:
	        - row_count:
	            description: context queue count
	            usage: GAUGE
	        - tid:
	            description: tenant id
	            usage: LABEL
	    nascm_transaction_context_queue_retries_alltenants:
	      query: "select tid,row_count from get_transaction_context_queue_retries();"
	      master: true
	      cache_seconds: 1200
	      metrics:
	        - row_count:
	            description: context queue count
	            usage: GAUGE
	        - tid:
	            description: tenant id
	            usage: LABEL
	    etl_job_queue_alltenants:
	      query: "select tid,etl_job_name,row_count from get_transaction_etl_job_queue();"
	      master: true
	      cache_seconds: 1200
	      metrics:
	        - row_count:
	            description: job queue count
	            usage: GAUGE
	        - tid:
	            description: tenant id
	            usage: LABEL
	        - etl_job_name:
	            description: job name
	            usage: LABEL
	    etl_job_delay_time_alltenants:
	      query: "select tid,etl_job_name,delay_time from get_transaction_etl_job_delay_time();"
	      master: true
	      cache_seconds: 1200
	      metrics:
	        - delay_time:
	            description: delay time
	            usage: GAUGE
	        - tid:
	            description: tenant id
	            usage: LABEL
	        - etl_job_name:
	            description: job name
	            usage: LABEL
	```
4. run helm install command to deploy postgres exporter

helm install prometheus-postgres-exporter-instance-level. -f values\_instance\_level.yaml --namespace=monitoring

helm install prometheus-postgres-exporter-database-level. -f values\_database\_level.yaml --namespace=monitoring

helm install prometheus-postgres-exporter-for-dtuple. -f values\_dead\_tuple.yaml --namespace=monitoring

**Configure Grafana to show metrics:**

The metrics name is consist of <query name>\_<metrics\_name>. eg:

![](attachments/704971984/704971980.png)

**Import Dashboard to Grafana:**

Download the [PostgresSQL RDS Monitoring Dashboard](attachments/704971984/709413468.json) and import to Grafana.

**Related pages**

- Page:
	[ESM Cloud Farm Version Tracking](/display/ICSD/ESM+Cloud+Farm+Version+Tracking)
- Page:
	[How to get an Opentext Confluence account](/display/ICSD/How+to+get+an+Opentext+Confluence+account)
- Page:
	[ITOM APM AppPluse Cloud Farm Information](/display/ICSD/ITOM+APM+AppPluse+Cloud+Farm+Information)
- Page:
	[ITOM Cloud Service Ops Doc Management Process](/display/ICSD/ITOM+Cloud+Service+Ops+Doc+Management+Process)
- Page:
	[ITOM ESM Cloud Service Catalog](/display/ICSD/ITOM+ESM+Cloud+Service+Catalog)
- Page:
	[ITOM OpsB NOM Cloud Service Catalog](/display/ICSD/ITOM+OpsB+NOM+Cloud+Service+Catalog)
- Page:
	[OpsB and NOM Cloud Deployments Version Tracking](/display/ICSD/OpsB+and+NOM+Cloud+Deployments+Version+Tracking)

## Attachments:

[image-2025-5-26\_14-53-21.png](attachments/704971984/704971891.png) (image/png)  
[database\_monitoring\_functions\_definition.txt](attachments/704971984/709409438.txt) (text/plain)  
[image-2025-5-26\_15-15-4.png](attachments/704971984/704971980.png) (image/png)  
[prometheus-postgres-exporter.zip](attachments/704971984/704972022.zip) (application/zip)  
[database\_monitoring\_functions\_definition.txt](attachments/704971984/709409852.txt) (text/plain)  
[database\_monitoring\_functions\_definition.txt](attachments/704971984/704971933.txt) (text/plain)  
[7\. PostgreSQL RDS Monitoring-1751508520632.json](attachments/704971984/709413471.json) (application/json)  
[7\. PostgreSQL RDS Monitoring-1751508520632.json](attachments/704971984/709413468.json) (application/json)  
[database\_monitoring\_functions\_definition\_xservices\_rms.txt](attachments/704971984/711822860.txt) (text/plain)  
[database\_monitoring\_functions\_definition\_xservices\_rms.txt](attachments/704971984/711822858.txt) (text/plain)
