Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 22fc479

Browse files
committed
Improve docs with permission section
1 parent fab005d commit 22fc479

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

README.md

+75-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ You can add as many database connections as you like to the
1111
./postgresql_exporter -config=my/config.yml
1212
```
1313

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-
2014
Then you can add hostname:9111 to the prometheus scrapes config:
2115

2216
```yml
@@ -28,6 +22,81 @@ Then you can add hostname:9111 to the prometheus scrapes config:
2822
And voilá, metrics should be there and you should be able to query,
2923
graph and alert on them.
3024
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 REPLACE FUNCTION monitoring.pg_stat_activity() RETURNS SETOF pg_stat_activity AS $$
73+
SELECT * FROM pg_catalog.pg_stat_activity;
74+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
75+
76+
CREATE VIEW monitoring.pg_stat_activity AS
77+
SELECT * FROM monitoring.pg_stat_activity();
78+
79+
CREATE OR REPLACE FUNCTION monitoring.pg_stat_statements() RETURNS SETOF pg_stat_statements AS $$
80+
SELECT * FROM public.pg_stat_statements;
81+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
82+
83+
CREATE VIEW monitoring.pg_stat_statements AS
84+
SELECT * FROM monitoring.pg_stat_statements();
85+
86+
CREATE OR REPLACE FUNCTION monitoring.pg_stat_replication() RETURNS SETOF pg_stat_replication AS $$
87+
SELECT * FROM pg_catalog.pg_stat_replication;
88+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
89+
90+
CREATE VIEW monitoring.pg_stat_replication AS
91+
SELECT * FROM monitoring.pg_stat_replication();
92+
93+
CREATE OR REPLACE FUNCTION monitoring.pg_stat_progress_vacuum() RETURNS SETOF pg_stat_progress_vacuum AS $$
94+
SELECT * FROM pg_catalog.pg_stat_progress_vacuum;
95+
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
96+
97+
CREATE VIEW monitoring.pg_stat_progress_vacuum AS
98+
SELECT * FROM monitoring.pg_stat_progress_vacuum();
99+
```
31100

32101
## Running it within Docker
33102

0 commit comments

Comments
 (0)