Prepare your PostgreSQL database
In this milestone, you’ll prepare your PostgreSQL database for monitoring. This involves enabling the pg_stat_statements extension, creating a dedicated monitoring user, and adjusting query tracking settings for complete query capture.
To prepare your PostgreSQL database instance for Database Observability, complete the following steps:
Add
pg_stat_statementstoshared_preload_librariesin yourpostgresql.conffile and restart PostgreSQL to apply the change.Create the extension in each database you want to monitor:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;Verify the extension is installed:
SELECT * FROM pg_extension WHERE extname = 'pg_stat_statements';Expected result: One row confirming the extension exists.
Set
pg_stat_statements.tracktoallin yourpostgresql.conffile:pg_stat_statements.track = allVerify the setting:
SHOW pg_stat_statements.track;Expected result: Value is
all.Increase
track_activity_query_sizeto4096in yourpostgresql.conffile to capture complete query text.Verify the setting:
SHOW track_activity_query_size;Expected result:
4kB(4096).Create a dedicated monitoring user and grant the required roles:
CREATE USER "db-o11y" WITH PASSWORD '<DB_O11Y_PASSWORD>'; GRANT pg_monitor TO "db-o11y"; GRANT pg_read_all_stats TO "db-o11y";Replace the password with a strong value.
Verify that the monitoring user can query
pg_stat_statements:-- run with the db-o11y user SELECT * FROM pg_stat_statements LIMIT 1;Disable tracking of queries executed by the monitoring user:
ALTER ROLE "db-o11y" SET pg_stat_statements.track = 'none';Grant object-level privileges for schema details and explain plans. Connect to each database and grant access to the schemas you want to monitor:
GRANT USAGE ON SCHEMA public TO "db-o11y"; GRANT SELECT ON ALL TABLES IN SCHEMA public TO "db-o11y";Alternatively, grant read access to all objects:
GRANT pg_read_all_data TO "db-o11y";You’ve prepared your PostgreSQL database for Database Observability monitoring.
In the next milestone, you’ll configure Grafana Alloy with the Database Observability components.