ankane/pghero · error · PgHero::Error
Invalid database id
Error message
Invalid database id
What it means
PgHero raises this from the private gcp_stats helper (lib/pghero/methods/system.rb:90) when the configured gcp_database_id fails /\A[a-z0-9\-:]+\z/i. Like the metric-name check, it guards interpolation: gcp_database_id is embedded verbatim into the Cloud Monitoring filter (resource.label.database_id = "...") and split(":").first is used for the project path, so characters outside alphanumerics, hyphens, and colons are rejected before any API call. A well-formed Cloud SQL id looks like myproject:us-central1:myinstance.
Source
Thrown at lib/pghero/methods/system.rb:90
data
else
raise NotEnabled, "System stats not enabled"
end
end
private
def gcp_stats(metric_name, duration: nil, period: nil, offset: nil, series: false)
# TODO DRY with RDS stats
duration = (duration || 1.hour).to_i
period = (period || 1.minute).to_i
offset = (offset || 0).to_i
end_time = Time.at(((Time.now - offset).to_f / period).ceil * period)
start_time = end_time - duration
# validate input since we need to interpolate below
raise Error, "Invalid metric name" unless /\A[a-z\/_]+\z/i.match?(metric_name)
raise Error, "Invalid database id" unless /\A[a-z0-9\-:]+\z/i.match?(gcp_database_id)
# we handle three situations:
# 1. google-cloud-monitoring-v3
# 2. google-cloud-monitoring
# 3. google-apis-monitoring_v3
begin
require "google/cloud/monitoring/v3"
rescue LoadError
require "google/apis/monitoring_v3"
end
# for situations 1 and 2
# Google::Cloud::Monitoring.metric_service doesn't work for situation 1
if defined?(Google::Cloud::Monitoring::V3)
client = Google::Cloud::Monitoring::V3::MetricService::Client.new
interval = Google::Cloud::Monitoring::V3::TimeInterval.new
interval.end_time = Google::Protobuf::Timestamp.new(seconds: end_time.to_i)View on GitHub (pinned to 7edb57986f)
Solutions
- Set gcp_database_id to the exact project:region:instance form, e.g. `gcp_database_id: myproject:us-central1:myinstance` in config/pghero.yml, with no quotes, spaces, or path prefixes.
- Verify the value against the regex in console: /\A[a-z0-9\-:]+\z/i.match?(PgHero.config.dig("databases", "main", "gcp_database_id")) or simply check PgHero.gcp_database_id.
- Restart/reload the app after editing pghero.yml so the config cache picks up the new value.
Example fix
# before - config/pghero.yml # gcp_database_id: projects/myproject/instances/myinstance # raises PgHero::Error: Invalid database id # after - project:region:instance form # gcp_database_id: myproject:us-central1:myinstance PgHero.cpu_usage
Defensive patterns
Strategy: validation
Validate before calling
id = PgHero.gcp_database_id raise ArgumentError, "bad gcp_database_id" unless id.is_a?(String) && id.match?(/\A[a-z0-9\-:]+\z/i) PgHero.cpu_usage
Type guard
def valid_gcp_database_id?(id)
id.is_a?(String) && id.match?(/\A[a-z0-9\-:]+\z/i) && id.count(":") == 2
end Try / catch
begin
PgHero.cpu_usage(duration: 1.hour)
rescue PgHero::Error => e
raise unless e.message == "Invalid database id"
Rails.logger.error("Fix gcp_database_id in pghero.yml (expected project:region:instance)")
{}
end Prevention
- Document the project:region:instance format next to the key in pghero.yml comments.
- Add a boot-time config assertion for gcp_database_id format so bad deploys fail fast.
- Copy the value from `gcloud sql instances describe` connectionName-adjacent output, never from the console URL.
When it happens
Trigger: Setting gcp_database_id in config/pghero.yml to a value with dots, slashes, spaces, quotes, or the full resource path ("projects/myproject/instances/inst") instead of project:region:instance; then calling any system-stats method (cpu_usage, connection_stats, ...) on that database, which routes to gcp_stats.
Common situations: Copy-pasting the instance connection name from the GCP console in the wrong format; wrapping the YAML value in extra quotes that become part of the string; trailing whitespace from YAML indentation; using the instance's IP or DNS name instead of the database_id.
Related errors
- Invalid metric name
- Invalid config file
- User not found: #{user}
- Invalid query hash: #{query_hash}
- Historical query stats not enabled
AI-assisted analysis of ankane/pghero@7edb57986f (2026-08-21).
Data as JSON: /api/errors/b77715ae7176914e.
Report an issue: GitHub.