Prepare your PostgreSQL database
In this milestone, you’ll prepare your PostgreSQL database for monitoring. This involves enabling the pg_stat_statements extension, configuring query tracking settings for complete query capture, creating a dedicated monitoring user, and granting the privileges Alloy needs.
To prepare your PostgreSQL database instance for Database Observability, complete the following steps:
Add or update the query tracking settings in your
postgresql.conffile:shared_preload_libraries = 'pg_stat_statements' compute_query_id = on pg_stat_statements.track = all track_activity_query_size = 4096shared_preload_libraries,compute_query_id, andtrack_activity_query_sizeare startup settings. If you change any of them, restart PostgreSQL before continuing.Create the extension in each database you want to monitor:
-- repeat across all logical databases CREATE EXTENSION IF NOT EXISTS pg_stat_statements;Verify that the settings are applied:
SHOW shared_preload_libraries; SELECT extname FROM pg_extension WHERE extname = 'pg_stat_statements'; SHOW compute_query_id; SHOW pg_stat_statements.track; SHOW track_activity_query_size;Expected results:
shared_preload_librariesincludespg_stat_statements, the extension query returnspg_stat_statements,compute_query_idison,pg_stat_statements.trackisall, andtrack_activity_query_sizeis4096or4kB.Create the
db-o11ymonitoring user and grant its base roles. ReplaceYOUR_DB_O11Y_PASSWORDwith a strong password:CREATE USER "db-o11y" WITH PASSWORD 'YOUR_DB_O11Y_PASSWORD'; GRANT pg_monitor TO "db-o11y"; GRANT pg_read_all_stats TO "db-o11y";Alloy connects as this user. If you choose a different user name, use it in every later step.
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, so Alloy’s own collection queries don’t show up as database workload:
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, if you’re unsure which schemas need access, use the predefined role to grant read access to all objects:
GRANT pg_read_all_data TO "db-o11y";
You’ve prepared your PostgreSQL database for Database Observability monitoring. pg_stat_statements tracks full query text with stable query IDs, and the db-o11y user can read the data Alloy needs.
In the next milestone, you’ll generate the Grafana Alloy configuration for this database and deploy it.