Make pg_stat_monitor automatically accessible for every newly created database¶
In PostgreSQL, a database is created by copying the system template database template1. To automatically make pg_stat_monitor available for every newly created database, you can do the following:
- modify the
template1template database and enable the extension for it. Then all databases you create have thepg_stat_monitorview available. - create a new system template database and enable the extension for it. You need to define this template when you create a database to have the
pg_stat_monitorview available for it.
For either option, your user must have the superuser privileges; the instructions below assume your are working as the postgres user:
-
Log in as the
postgresuser$ sudo su postgres -
Create the extension for the
template1database$ psql -d template1 -c 'create extension pg_stat_monitor;' -
Create a new database
$ createdb -T my_template dbtest -
Check that
pg_stat_monitoris accessible from the database$ psql -d dbtest -c '\dx';Output:
Name | Version | Schema | Description -----------------+---------+------------+------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------- pg_stat_monitor | 2.0 | public | The pg_stat_monitor is a PostgreSQL Query Performance Monitoring tool, based on PostgreSQL contrib module pg _stat_statements. pg_stat_monitor provides aggregated statistics, client information, plan details including plan, and histogram information. plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language (2 rows)
-
Log in as the
postgresuser$ sudo su postgres -
Create a database to serve as the template.
$ createdb my_template -
Connect to this database and create the
pg_stat_monitorextension$ psql -d my_template -c 'create extension pg_stat_monitor;' -
Set the database as the template database.
- Connect to the database:
$ psql -d my_template- Update the database
UPDATE pg_database SET datistemplate=true where datname='my_template'; -
Create a database using the new template
$ createdb -T my_template dbtest -
Check that
pg_stat_monitoris accessible from the database$ psql -d dbtest -c '\dx';Output:
Name | Version | Schema | Description -----------------+---------+------------+------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------- pg_stat_monitor | 2.0 | public | The pg_stat_monitor is a PostgreSQL Query Performance Monitoring tool, based on PostgreSQL contrib module pg _stat_statements. pg_stat_monitor provides aggregated statistics, client information, plan details including plan, and histogram information. plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language (2 rows)