From 1ab8d2bbc84b1523df01dccf85bbe5807dffe984 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 12 Aug 2019 03:40:58 +0200 Subject: [PATCH] Discover only databases that are not templates and allow connections (#297) * Discover only databases that are not templates and allow connections * Update readme --- README.md | 2 +- cmd/postgres_exporter/postgres_exporter.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e4632551..c16daa31 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ flag. This removes all built-in metrics, and uses only metrics defined by querie ### Automatically discover databases To scrape metrics from all databases on a database server, the database DSN's can be dynamically discovered via the -`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database` is run for all configured DSN's. From the +`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false` is run for all configured DSN's. From the result a new set of DSN's is created for which the metrics are scraped. In addition, the option `--exclude-databases` adds the possibily to filter the result from the auto discovery to discard databases you do not need. diff --git a/cmd/postgres_exporter/postgres_exporter.go b/cmd/postgres_exporter/postgres_exporter.go index ae19745b..920dfc3d 100644 --- a/cmd/postgres_exporter/postgres_exporter.go +++ b/cmd/postgres_exporter/postgres_exporter.go @@ -1074,7 +1074,7 @@ func newDesc(subsystem, name, help string, labels prometheus.Labels) *prometheus } func queryDatabases(server *Server) ([]string, error) { - rows, err := server.db.Query("SELECT datname FROM pg_database") // nolint: safesql + rows, err := server.db.Query("SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false") // nolint: safesql if err != nil { return nil, fmt.Errorf("Error retrieving databases: %v", err) }