You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 28, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+75-6
Original file line number
Diff line number
Diff line change
@@ -11,12 +11,6 @@ You can add as many database connections as you like to the
11
11
./postgresql_exporter -config=my/config.yml
12
12
```
13
13
14
-
By default some stat views like pg_stat_statements and pg_stat_activity doesn't allow viewing queries run by other users, unless you are a database superuser. Since you probably don't want monitoring to run as a superuser, you can setup, in a AWS RDS instance, a separate monitoring user like this:
15
-
16
-
```sql
17
-
GRANT pg_monitor TO my_monitor_user;
18
-
```
19
-
20
14
Then you can add hostname:9111 to the prometheus scrapes config:
21
15
22
16
```yml
@@ -28,6 +22,81 @@ Then you can add hostname:9111 to the prometheus scrapes config:
28
22
And voilá, metrics should be there and you should be able to query,
29
23
graph and alert on them.
30
24
25
+
## Setting up a restricted monitoring user
26
+
27
+
By default some stat views like pg_stat_statements and pg_stat_activity doesn't allow viewing queries run by other users, unless you are a database superuser. Since you probably don't want monitoring to run as a superuser, you can setup a separate monitoring user like this:
28
+
29
+
```sql
30
+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
31
+
CREATE EXTENSION IF NOT EXISTS pgstattuple;
32
+
33
+
CREATE SCHEMA monitoring;
34
+
35
+
CREATE OR REPLACE FUNCTION monitoring.pgstattuple(IN relname text,
36
+
OUT table_len BIGINT,
37
+
OUT tuple_count BIGINT,
38
+
OUT tuple_len BIGINT,
39
+
OUT tuple_percent FLOAT8,
40
+
OUT dead_tuple_count BIGINT,
41
+
OUT dead_tuple_len BIGINT,
42
+
OUT dead_tuple_percent FLOAT8,
43
+
OUT free_space BIGINT,
44
+
OUT free_percent FLOAT8) AS $$
45
+
SELECT
46
+
table_len,
47
+
tuple_count,
48
+
tuple_len,
49
+
tuple_percent,
50
+
dead_tuple_count,
51
+
dead_tuple_len,
52
+
dead_tuple_percent,
53
+
free_space,
54
+
free_percent
55
+
FROM public.pgstattuple(relname)
56
+
$$ LANGUAGE SQL VOLATILE SECURITY DEFINER;
57
+
58
+
CREATE ROLE monitoring WITH LOGIN PASSWORD 'mypassword'
59
+
CONNECTION LIMIT 5 IN ROLE pg_monitor;
60
+
ALTER ROLE monitoring SET search_path = monitoring, pg_catalog, public;
61
+
62
+
GRANT CONNECT ON DATABASE {{database_name}} TO monitoring;
63
+
GRANT USAGE ON SCHEMA monitoring TO monitoring;
64
+
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA monitoring TO monitoring;
65
+
```
66
+
67
+
Note that these statements must be run as a superuser (to create the SECURITY DEFINER function), but from here onwards you can use the `monitoring` user instead. The exporter will automatically use the helper methods if they exist in the `monitoring` schema, otherwise data will be fetched directly.
68
+
69
+
The default role `pg_monitor` was only added in PostgreSQL 10 (See more details [here](https://www.postgresql.org/docs/10/static/default-roles.html)). If you're running Postgres 9.6 or lower you need to create some other helper methods in the `monitoring` schema:
70
+
71
+
```sql
72
+
CREATE OR REPLACEFUNCTIONmonitoring.pg_stat_activity() RETURNS SETOF pg_stat_activity AS $$
73
+
SELECT*FROMpg_catalog.pg_stat_activity;
74
+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
75
+
76
+
CREATEVIEWmonitoring.pg_stat_activity AS
77
+
SELECT*FROMmonitoring.pg_stat_activity();
78
+
79
+
CREATE OR REPLACEFUNCTIONmonitoring.pg_stat_statements() RETURNS SETOF pg_stat_statements AS $$
80
+
SELECT*FROMpublic.pg_stat_statements;
81
+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
82
+
83
+
CREATEVIEWmonitoring.pg_stat_statements AS
84
+
SELECT*FROMmonitoring.pg_stat_statements();
85
+
86
+
CREATE OR REPLACEFUNCTIONmonitoring.pg_stat_replication() RETURNS SETOF pg_stat_replication AS $$
87
+
SELECT*FROMpg_catalog.pg_stat_replication;
88
+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
89
+
90
+
CREATEVIEWmonitoring.pg_stat_replication AS
91
+
SELECT*FROMmonitoring.pg_stat_replication();
92
+
93
+
CREATE OR REPLACEFUNCTIONmonitoring.pg_stat_progress_vacuum() RETURNS SETOF pg_stat_progress_vacuum AS $$
0 commit comments