<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>PostgreSQL data source on Grafana Labs</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/</link><description>Recent content in PostgreSQL data source on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/grafana/v12.4/datasources/postgres/index.xml" rel="self" type="application/rss+xml"/><item><title>Configure the PostgreSQL data source</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/configure/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/postgres/configure/</guid><content><![CDATA[&lt;h1 id=&#34;configure-the-postgresql-data-source&#34;&gt;Configure the PostgreSQL data source&lt;/h1&gt;
&lt;p&gt;This document explains how to configure the PostgreSQL data source and lists all configuration options. For general information on managing data sources, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/administration/data-source-management/&#34;&gt;Data source management&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;You need the &lt;code&gt;Organization administrator&lt;/code&gt; role to configure the data source. You can also &lt;a href=&#34;#provision-the-data-source&#34;&gt;configure it via YAML&lt;/a&gt; using Grafana provisioning or &lt;a href=&#34;#configure-with-terraform&#34;&gt;using Terraform&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Grafana includes a built-in PostgreSQL data source; no plugin installation is required.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Have your PostgreSQL security details ready (certificates and client keys, if using TLS/SSL).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Note your PostgreSQL version; you’ll be prompted for it during configuration.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;When adding a data source, the database user you specify should have only &lt;code&gt;SELECT&lt;/code&gt; permissions on the relevant schemas and tables. Grafana does not validate the safety of queries, so users could run potentially harmful SQL (for example, &lt;code&gt;DROP TABLE&lt;/code&gt;). Create a dedicated PostgreSQL user with restricted permissions to limit risk.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt; CREATE USER grafanareader WITH PASSWORD &amp;#39;password&amp;#39;;
 GRANT USAGE ON SCHEMA schema TO grafanareader;
 GRANT SELECT ON schema.table TO grafanareader;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Replace &lt;code&gt;schema&lt;/code&gt; and &lt;code&gt;table&lt;/code&gt; with your schema and table names.&lt;/p&gt;
&lt;h2 id=&#34;add-the-postgresql-data-source&#34;&gt;Add the PostgreSQL data source&lt;/h2&gt;
&lt;p&gt;Complete the following steps to set up a new PostgreSQL data source:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Connections&lt;/strong&gt; in the left-side menu.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add new connection&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;PostgreSQL&lt;/code&gt; in the search bar.&lt;/li&gt;
&lt;li&gt;Select the &lt;strong&gt;PostgreSQL data source&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add new data source&lt;/strong&gt; in the upper right.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You are taken to the &lt;strong&gt;Settings&lt;/strong&gt; tab where you will configure the data source.&lt;/p&gt;
&lt;h2 id=&#34;postgresql-configuration-options&#34;&gt;PostgreSQL configuration options&lt;/h2&gt;
&lt;p&gt;Following is a list of PostgreSQL configuration options:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Name&lt;/td&gt;
              &lt;td&gt;Sets the name you use to refer to the data source in panels and queries. Examples: &lt;code&gt;PostgreSQL-DB-1&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Default&lt;/td&gt;
              &lt;td&gt;Toggle to set this specific PostgreSQL data source as the default pre-selected data source in panels and visualizations.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Connection section:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Host URL&lt;/td&gt;
              &lt;td&gt;The IP address/hostname and optional port of your PostgreSQL instance.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Database name&lt;/td&gt;
              &lt;td&gt;The name of your PostgreSQL database. This database is used as the default for queries in the 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/query-editor/&#34;&gt;query editor&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Authentication section:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Username&lt;/td&gt;
              &lt;td&gt;Enter the username used to connect to your PostgreSQL database.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Password&lt;/td&gt;
              &lt;td&gt;Enter the password used to connect to the PostgreSQL database. If a password is not specified and your PostgreSQL is configured to request a password, the data source will look for a &lt;a href=&#34;https://www.postgresql.org/docs/current/static/libpq-pgpass.html&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;standard PostgreSQL password file&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Mode&lt;/td&gt;
              &lt;td&gt;Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. When TLS/SSL Mode is disabled, TLS/SSL Method and TLS/SSL Auth Details aren’t visible options.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Method&lt;/td&gt;
              &lt;td&gt;Determines how TLS/SSL certificates are configured.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;- File system path&lt;/td&gt;
              &lt;td&gt;This option allows you to configure certificates by specifying paths to existing certificates on the local file system where Grafana is running. Ensure this file is readable by the user executing the Grafana process.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;- Certificate content&lt;/td&gt;
              &lt;td&gt;This option allows you to configure certificates by specifying their content. The content is stored and encrypted in the Grafana database. When connecting to the database, the certificates are saved as files, on the local filesystem, in the Grafana data path.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;TLS/SSL Auth Details:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you select the TLS/SSL Mode options &lt;strong&gt;require&lt;/strong&gt;, &lt;strong&gt;verify-ca&lt;/strong&gt; or &lt;strong&gt;verify-full&lt;/strong&gt; and &lt;strong&gt;file system path&lt;/strong&gt; the following are required:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Root Certificate&lt;/td&gt;
              &lt;td&gt;Specify the path to the root certificate file.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Client Certificate&lt;/td&gt;
              &lt;td&gt;Specify the path to the client certificate and ensure the file is accessible to the user running the Grafana process.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Client Key&lt;/td&gt;
              &lt;td&gt;Specify the path to the client key file and ensure the file is accessible to the user running the Grafana process.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;If you select the TLS/SSL Mode option &lt;strong&gt;require&lt;/strong&gt; and TLS/SSL Method certificate content the following are required:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Client Certificate&lt;/td&gt;
              &lt;td&gt;Provide the client certificate.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Client Key&lt;/td&gt;
              &lt;td&gt;Provide the client key.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;If you select the TLS/SSL Mode options &lt;strong&gt;verify-ca&lt;/strong&gt; or &lt;strong&gt;verify-full&lt;/strong&gt; with the TLS/SSL Method certificate content the following are required:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Client Certificate&lt;/td&gt;
              &lt;td&gt;Provide the client certificate.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Root Certificate&lt;/td&gt;
              &lt;td&gt;Provide the root certificate.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TLS/SSL Client Key&lt;/td&gt;
              &lt;td&gt;Provide the client key.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;PostgreSQL Options:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Version&lt;/td&gt;
              &lt;td&gt;The PostgreSQL server version. Determines which functions are available in the query builder. The default is 9.3.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Min time interval&lt;/td&gt;
              &lt;td&gt;Defines a lower limit for the auto group by time interval. Grafana recommends aligning this setting with the data write frequency. For example, set it to &lt;code&gt;1m&lt;/code&gt; if your data is written every minute. Refer to &lt;a href=&#34;#min-time-interval&#34;&gt;Min time interval&lt;/a&gt; for format examples.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;TimescaleDB&lt;/td&gt;
              &lt;td&gt;A time series database built as a PostgreSQL extension. When enabled, Grafana uses &lt;code&gt;time_bucket&lt;/code&gt; in the &lt;code&gt;$__timeGroup&lt;/code&gt; macro to display TimescaleDB-specific aggregate functions in the query builder. For more information, refer to &lt;a href=&#34;https://docs.timescale.com/timescaledb/latest/tutorials/grafana/grafana-timescalecloud/#connect-timescaledb-and-grafana&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;TimescaleDB documentation&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Connection limits:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Max open&lt;/td&gt;
              &lt;td&gt;The maximum number of open connections to the database. The default is &lt;code&gt;100&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Auto max idle&lt;/td&gt;
              &lt;td&gt;Toggle to set the maximum number of idle connections to the number of maximum open connections. This setting is toggled on by default.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Max idle&lt;/td&gt;
              &lt;td&gt;The maximum number of connections in the idle connection pool. The default is &lt;code&gt;100&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Max lifetime&lt;/td&gt;
              &lt;td&gt;The maximum amount of time in seconds a connection may be reused. The default is &lt;code&gt;14400&lt;/code&gt;, or 4 hours.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;&lt;strong&gt;Private data source connect:&lt;/strong&gt;&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Setting&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;Private data source connect&lt;/td&gt;
              &lt;td&gt;&lt;em&gt;Only for Grafana Cloud users.&lt;/em&gt; Private data source connect, or PDC, allows you to establish a private, secured connection between a Grafana Cloud instance, or stack, and data sources secured within a private network. Click the drop-down to locate the URL for PDC. For more information, refer to &lt;a href=&#34;/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/&#34;&gt;Private data source connect (PDC)&lt;/a&gt;.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;p&gt;Click &lt;strong&gt;Manage private data source connect&lt;/strong&gt; to be taken to your PDC connection page, where you’ll find your PDC configuration details.&lt;/p&gt;
&lt;p&gt;After you have added your PostgreSQL connection settings, click &lt;strong&gt;Save &amp;amp; test&lt;/strong&gt; to test and save the data source connection.&lt;/p&gt;
&lt;h3 id=&#34;min-time-interval&#34;&gt;Min time interval&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;Min time interval&lt;/strong&gt; setting defines a lower limit for the 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/#__interval&#34;&gt;&lt;code&gt;$__interval&lt;/code&gt;&lt;/a&gt; and 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/#__interval_ms&#34;&gt;&lt;code&gt;$__interval_ms&lt;/code&gt;&lt;/a&gt; variables.&lt;/p&gt;
&lt;p&gt;This option can also be configured or overridden in the dashboard panel under the data source settings.&lt;/p&gt;
&lt;p&gt;This value must be formatted as a number followed by a valid time identifier:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Identifier&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;y&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;year&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;M&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;month&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;w&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;week&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;d&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;day&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;h&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;hour&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;m&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;minute&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;s&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;second&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;ms&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;millisecond&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;provision-the-data-source&#34;&gt;Provision the data source&lt;/h2&gt;
&lt;p&gt;You can define and configure the data source in YAML files with 
    &lt;a href=&#34;/docs/grafana/v12.4/administration/provisioning/#data-sources&#34;&gt;provisioning&lt;/a&gt;. For more information about provisioning and available configuration options, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/administration/provisioning/#datasources&#34;&gt;Provision Grafana&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&#34;postgresql-provisioning-example&#34;&gt;PostgreSQL provisioning example&lt;/h3&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;apiVersion: 1

datasources:
  - name: Postgres
    type: postgres
    url: localhost:5432
    user: grafana
    secureJsonData:
      password: &amp;#39;Password!&amp;#39;
    jsonData:
      database: grafana
      sslmode: &amp;#39;disable&amp;#39; # disable/require/verify-ca/verify-full
      maxOpenConns: 100
      maxIdleConns: 100
      maxIdleConnsAuto: true
      connMaxLifetime: 14400
      postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
      timescaledb: false&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h4 id=&#34;troubleshoot-provisioning-issues&#34;&gt;Troubleshoot provisioning issues&lt;/h4&gt;
&lt;p&gt;If you encounter metric request errors or other issues when provisioning, see 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/troubleshooting/#provisioning-errors&#34;&gt;Provisioning errors&lt;/a&gt; in the PostgreSQL troubleshooting guide.&lt;/p&gt;
&lt;h2 id=&#34;configure-with-terraform&#34;&gt;Configure with Terraform&lt;/h2&gt;
&lt;p&gt;You can configure the PostgreSQL data source using &lt;a href=&#34;https://www.terraform.io/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Terraform&lt;/a&gt; with the &lt;a href=&#34;https://registry.terraform.io/providers/grafana/grafana/latest/docs&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana Terraform provider&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For more information about provisioning resources with Terraform, refer to &lt;a href=&#34;/docs/grafana-cloud/developer-resources/infrastructure-as-code/terraform/&#34;&gt;Grafana as code using Terraform&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&#34;terraform-example&#34;&gt;Terraform example&lt;/h3&gt;
&lt;p&gt;The following example creates a basic PostgreSQL data source:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;hcl&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-hcl&#34;&gt;resource &amp;#34;grafana_data_source&amp;#34; &amp;#34;postgres&amp;#34; {
  name = &amp;#34;Postgres&amp;#34;
  type = &amp;#34;postgres&amp;#34;
  url  = &amp;#34;localhost:5432&amp;#34;
  user = &amp;#34;grafana&amp;#34;

  json_data_encoded = jsonencode({
    database         = &amp;#34;grafana&amp;#34;
    sslmode          = &amp;#34;disable&amp;#34;
    maxOpenConns     = 100
    maxIdleConns     = 100
    maxIdleConnsAuto = true
    connMaxLifetime  = 14400
    postgresVersion  = 903
    timescaledb      = false
  })

  secure_json_data_encoded = jsonencode({
    password = &amp;#34;Password!&amp;#34;
  })
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;For all available configuration options, refer to the &lt;a href=&#34;https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/data_source&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana provider data source resource documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;next-steps&#34;&gt;Next steps&lt;/h2&gt;
&lt;p&gt;After configuring your PostgreSQL data source, you can:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/query-editor/&#34;&gt;Write queries&lt;/a&gt; using the query editor to explore and visualize your data.&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/template-variables/&#34;&gt;Use template variables&lt;/a&gt; to create dynamic, reusable dashboards.&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/annotations/&#34;&gt;Add annotations&lt;/a&gt; to overlay PostgreSQL events on your panels.&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/alerting/&#34;&gt;Set up alerting&lt;/a&gt; to create alert rules based on your PostgreSQL data (time series format only).&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/troubleshooting/&#34;&gt;Troubleshoot issues&lt;/a&gt; if you encounter problems with your data source.&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="configure-the-postgresql-data-source">Configure the PostgreSQL data source&lt;/h1>
&lt;p>This document explains how to configure the PostgreSQL data source and lists all configuration options. For general information on managing data sources, refer to
&lt;a href="/docs/grafana/v12.4/administration/data-source-management/">Data source management&lt;/a>.&lt;/p></description></item><item><title>PostgreSQL query editor</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/query-editor/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/postgres/query-editor/</guid><content><![CDATA[&lt;h1 id=&#34;postgresql-query-editor&#34;&gt;PostgreSQL query editor&lt;/h1&gt;
&lt;p&gt;The PostgreSQL query editor lets you build and run queries against your data source. For general query editor and data transformation concepts, see 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/query-transform-data/&#34;&gt;Query and transform data&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can open the PostgreSQL query editor from the 
    &lt;a href=&#34;/docs/grafana/v12.4/explore/&#34;&gt;Explore page&lt;/a&gt; or from a dashboard panel—click the ellipsis in the upper right of the panel and select &lt;strong&gt;Edit&lt;/strong&gt;.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;A default database must be configured in the data source settings. If none is set, or it is removed, the data source will not run queries until a database is configured again.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h2 id=&#34;postgresql-query-editor-components&#34;&gt;PostgreSQL query editor components&lt;/h2&gt;
&lt;p&gt;The PostgreSQL query editor has two modes: &lt;strong&gt;Builder&lt;/strong&gt; and &lt;strong&gt;Code&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Builder mode helps you build a query using a visual interface. Code mode allows for advanced querying and offers support for complex SQL query writing.&lt;/p&gt;
&lt;h3 id=&#34;postgresql-builder-mode&#34;&gt;PostgreSQL Builder mode&lt;/h3&gt;
&lt;p&gt;The following components will help you build a PostgreSQL query:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Format&lt;/strong&gt; - Select the query result format from the drop-down. The default is &lt;strong&gt;Table&lt;/strong&gt;. If you use the &lt;strong&gt;Time series&lt;/strong&gt; format option, one of the columns must be &lt;code&gt;time&lt;/code&gt;. Refer to &lt;a href=&#34;#time-series-queries&#34;&gt;Time series queries&lt;/a&gt; for more information.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Table&lt;/strong&gt; - Select a table from the drop-down. Tables correspond to the chosen database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data operations&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt; Select an aggregation from the drop-down. You can add multiple data operations by clicking the &lt;strong&gt;&#43;&lt;/strong&gt; sign. Click the &lt;strong&gt;X&lt;/strong&gt; sign to remove data operations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Column&lt;/strong&gt; - Select a column on which to run the aggregation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Alias&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt; Add an alias from the drop-down. You can also add your own alias by typing it in the box and clicking &lt;strong&gt;Enter&lt;/strong&gt;. Remove an alias by clicking the &lt;strong&gt;X&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Filter&lt;/strong&gt; - Toggle to add filters.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Filter by column value&lt;/strong&gt; - &lt;em&gt;Optional&lt;/em&gt; If you toggle &lt;strong&gt;Filter&lt;/strong&gt;, you can add a column to filter by from the drop-down. To filter on more columns, click the &lt;strong&gt;&#43;&lt;/strong&gt; sign to the right of the condition drop-down. Choose an operator from the drop-down next to the condition. When multiple filters are added, you can add an &lt;code&gt;AND&lt;/code&gt; operator to display all true conditions or an &lt;code&gt;OR&lt;/code&gt; operator to display any true conditions. Use the second drop-down to choose a filter value. To remove a filter, click the &lt;strong&gt;X&lt;/strong&gt; next to that filter&amp;rsquo;s drop-down. After selecting a date-type column, you can choose &lt;strong&gt;Macros&lt;/strong&gt; from the operators list and select &lt;code&gt;timeFilter&lt;/code&gt; to add the &lt;code&gt;$__timeFilter&lt;/code&gt; macro to the query with the selected date column.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Group&lt;/strong&gt; - Toggle to add &lt;strong&gt;Group by column&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Group by column&lt;/strong&gt; - Select a column to group by from the drop-down. Click the &lt;strong&gt;&#43;&lt;/strong&gt; sign to group by multiple columns. Click the &lt;strong&gt;X&lt;/strong&gt; to remove a column.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Order&lt;/strong&gt; - Toggle to add an &lt;code&gt;ORDER BY&lt;/code&gt; statement.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Order by&lt;/strong&gt; - Select a column to order by from the drop-down. Select ascending (&lt;code&gt;ASC&lt;/code&gt;) or descending (&lt;code&gt;DESC&lt;/code&gt;) order.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Limit&lt;/strong&gt; - You can add an optional limit on the number of retrieved results. Default is 50.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preview&lt;/strong&gt; - Toggle for a preview of the SQL query generated by the query builder. Preview is toggled on by default.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;postgresql-code-mode&#34;&gt;PostgreSQL Code mode&lt;/h2&gt;
&lt;p&gt;To create advanced queries, switch to &lt;strong&gt;Code mode&lt;/strong&gt; by clicking &lt;strong&gt;Code&lt;/strong&gt; in the upper right of the editor window. Code mode supports the auto-completion of tables, columns, SQL keywords, standard SQL functions, Grafana template variables, and Grafana macros. Columns cannot be completed before a table has been specified.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;If a table or column name is a reserved word or contains mixed case or special characters, use double quotes in SQL. For example, &lt;code&gt;&amp;quot;user&amp;quot;&lt;/code&gt; or &lt;code&gt;&amp;quot;Created At&amp;quot;&lt;/code&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p docs-image--no-shadow&#34;
    style=&#34;max-width: 963px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/static/img/docs/v92/sql_code_editor.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/static/img/docs/v92/sql_code_editor.png&#34;data-srcset=&#34;/static/img/docs/v92/sql_code_editor.png?w=320 320w, /static/img/docs/v92/sql_code_editor.png?w=550 550w, /static/img/docs/v92/sql_code_editor.png?w=750 750w, /static/img/docs/v92/sql_code_editor.png?w=900 900w, /static/img/docs/v92/sql_code_editor.png?w=1040 1040w, /static/img/docs/v92/sql_code_editor.png?w=1240 1240w, /static/img/docs/v92/sql_code_editor.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;&#34;width=&#34;963&#34;height=&#34;326&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/static/img/docs/v92/sql_code_editor.png&#34;
            alt=&#34;&#34;width=&#34;963&#34;height=&#34;326&#34;class=&#34;docs-image--no-shadow&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;Select &lt;strong&gt;Table&lt;/strong&gt; or &lt;strong&gt;Time Series&lt;/strong&gt; as the format. Click the &lt;strong&gt;{}&lt;/strong&gt; in the bottom right to format the query. Click the &lt;strong&gt;downward caret&lt;/strong&gt; to expand the Code mode editor. &lt;strong&gt;CTRL/CMD &#43; Return&lt;/strong&gt; serves as a keyboard shortcut to execute the query.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-warning&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Warning&lt;/p&gt;&lt;p&gt;Changes made to a query in Code mode will not transfer to Builder mode and will be discarded. You will be prompted to copy your code to the clipboard to save any changes.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h2 id=&#34;macros&#34;&gt;Macros&lt;/h2&gt;
&lt;p&gt;You can add macros to your queries to simplify the syntax and enable dynamic elements, such as date range filters.&lt;/p&gt;
&lt;p&gt;PostgreSQL expands macros into native SQL. When the &lt;a href=&#34;https://www.timescale.com/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;TimescaleDB&lt;/a&gt; extension is enabled, &lt;code&gt;$__timeGroup&lt;/code&gt; and &lt;code&gt;$__timeGroupAlias&lt;/code&gt; use &lt;code&gt;time_bucket()&lt;/code&gt; for more efficient grouping.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Macro example&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__time(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Renames the column to &lt;code&gt;time&lt;/code&gt;. Example: &lt;code&gt;dateColumn AS &amp;quot;time&amp;quot;&lt;/code&gt;. Use for native date/time columns.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeEpoch(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Converts to UNIX epoch (seconds) and renames the column to &lt;code&gt;time&lt;/code&gt;. Example: &lt;code&gt;extract(epoch from dateColumn) as &amp;quot;time&amp;quot;&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeFilter(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with a time range filter using the specified column name. Example: &lt;code&gt;dateColumn BETWEEN &#39;2020-07-13T20:19:09.254Z&#39; AND &#39;2020-07-13T21:19:09.254Z&#39;&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeFrom()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with the start of the currently active time selection (RFC3339Nano). Example: &lt;code&gt;&#39;2020-07-13T20:19:09.254Z&#39;&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeTo()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with the end of the currently active time selection (RFC3339Nano). Example: &lt;code&gt;&#39;2020-07-13T20:19:09.254Z&#39;&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with an expression suitable for use in a &lt;code&gt;GROUP BY&lt;/code&gt; clause. Example: &lt;code&gt;floor(extract(epoch from dateColumn)/300)*300&lt;/code&gt;. With TimescaleDB: &lt;code&gt;time_bucket(&#39;300s&#39;, dateColumn)&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;, 0)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as the &lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;)&lt;/code&gt; macro, but includes a fill parameter to ensure missing points in the series are added by Grafana, using 0 as the default value. &lt;strong&gt;This applies only to time series queries.&lt;/strong&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;, NULL)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as the &lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;, 0)&lt;/code&gt; but &lt;code&gt;NULL&lt;/code&gt; is used as the value for missing points. &lt;em&gt;This applies only to time series queries.&lt;/em&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;, previous)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as &lt;code&gt;$__timeGroup(dateColumn,&#39;5m&#39;, 0)&lt;/code&gt; but uses the previous value in the series as the fill value. If no previous value exists, it uses &lt;code&gt;NULL&lt;/code&gt;. &lt;em&gt;This applies only to time series queries.&lt;/em&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeGroupAlias(dateColumn,&#39;5m&#39;)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as &lt;code&gt;$__timeGroup&lt;/code&gt; but with an added column alias &lt;code&gt;AS &amp;quot;time&amp;quot;&lt;/code&gt;. With TimescaleDB, uses &lt;code&gt;time_bucket()&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochFilter(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with a time range filter for columns storing UNIX epoch (seconds). Example: &lt;code&gt;dateColumn &amp;gt;= 1494410783 AND dateColumn &amp;lt;= 1494497183&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochFrom()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with the start of the currently active time selection as a UNIX timestamp (seconds). Example: &lt;code&gt;1494410783&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochTo()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with the end of the currently active time selection as a UNIX timestamp (seconds). Example: &lt;code&gt;1494497183&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochNanoFilter(dateColumn)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with a time range filter for columns storing UNIX epoch in nanoseconds. Example: &lt;code&gt;dateColumn &amp;gt;= 1494410783152415214 AND dateColumn &amp;lt;= 1494497183142514872&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochNanoFrom()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with the start of the currently active time selection as a nanosecond timestamp. Example: &lt;code&gt;1494410783152415214&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochNanoTo()&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Replaces the value with the end of the currently active time selection as a nanosecond timestamp. Example: &lt;code&gt;1494497183142514872&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochGroup(dateColumn,&#39;5m&#39;, [fillmode])&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as &lt;code&gt;$__timeGroup&lt;/code&gt; but for columns storing UNIX epoch (seconds). Example: &lt;code&gt;floor((dateColumn)/300)*300&lt;/code&gt;. &lt;code&gt;fillMode&lt;/code&gt; only works with time series queries.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochGroupAlias(dateColumn,&#39;5m&#39;, [fillmode])&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Same as &lt;code&gt;$__unixEpochGroup&lt;/code&gt; but with an added column alias &lt;code&gt;AS &amp;quot;time&amp;quot;&lt;/code&gt;. &lt;code&gt;fillMode&lt;/code&gt; only works with time series queries.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;table-sql-queries&#34;&gt;Table SQL queries&lt;/h2&gt;
&lt;p&gt;If the &lt;strong&gt;Format&lt;/strong&gt; option is set to &lt;strong&gt;Table&lt;/strong&gt;, you can execute virtually any type of SQL query. The Table panel will automatically display the resulting columns and rows from your query.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;/media/docs/postgres/PostgreSQL-query-editor-v11.4.png&#34;
  alt=&#34;Table query&#34; width=&#34;1305&#34;
     height=&#34;513&#34;/&gt;&lt;/p&gt;
&lt;p&gt;You can change or customize the name of a Table panel column by using the SQL keyword &lt;code&gt;AS&lt;/code&gt; syntax.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  title as &amp;#34;Title&amp;#34;,
  &amp;#34;user&amp;#34;.login as &amp;#34;Created By&amp;#34;,
  dashboard.created as &amp;#34;Created On&amp;#34;
FROM dashboard
INNER JOIN &amp;#34;user&amp;#34; on &amp;#34;user&amp;#34;.id = dashboard.created_by
WHERE $__timeFilter(dashboard.created)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can use 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/template-variables/&#34;&gt;template variables&lt;/a&gt; in queries. For example, to filter by a variable named &lt;code&gt;hostname&lt;/code&gt;: &lt;code&gt;WHERE hostname IN($hostname)&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;time-series-queries&#34;&gt;Time series queries&lt;/h2&gt;
&lt;p&gt;Set the &lt;strong&gt;Format&lt;/strong&gt; option to &lt;strong&gt;Time series&lt;/strong&gt; to create and run time series queries.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;To run a time series query you must include a column named &lt;code&gt;time&lt;/code&gt; that returns either a SQL &lt;code&gt;datetime&lt;/code&gt; value or a numeric datatype representing the UNIX epoch time in seconds. Additionally, the query results must be sorted by the &lt;code&gt;time&lt;/code&gt; column for proper visualization in panels.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;The examples in this section refer to the data in the following table:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;--------------&amp;#43;---------------------&amp;#43;----------&amp;#43;
| time_date_time      | value_double | CreatedAt           | hostname |
&amp;#43;---------------------&amp;#43;--------------&amp;#43;---------------------&amp;#43;----------&amp;#43;
| 2020-01-02 03:05:00 | 3.0          | 2020-01-02 03:05:00 | 10.0.1.1 |
| 2020-01-02 03:06:00 | 4.0          | 2020-01-02 03:06:00 | 10.0.1.2 |
| 2020-01-02 03:10:00 | 6.0          | 2020-01-02 03:10:00 | 10.0.1.1 |
| 2020-01-02 03:11:00 | 7.0          | 2020-01-02 03:11:00 | 10.0.1.2 |
| 2020-01-02 03:20:00 | 5.0          | 2020-01-02 03:20:00 | 10.0.1.2 |
&amp;#43;---------------------&amp;#43;--------------&amp;#43;---------------------&amp;#43;----------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Time series query results are returned in &lt;a href=&#34;/developers/plugin-tools/key-concepts/data-frames#wide-format&#34;&gt;wide data frame format&lt;/a&gt;. In the data frame query result, any column, except for time or string-type columns, transforms into value fields. String columns, on the other hand, become field labels.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;For backward compatibility, an exception to this rule applies to queries that return three columns, one of which is a string column named &lt;code&gt;metric&lt;/code&gt;. Instead of converting the metric column into field labels, it is used as the field name, while the series name is set to its value. See the following example for reference.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example with &lt;code&gt;metric&lt;/code&gt; column:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(&amp;#34;time_date_time&amp;#34;,&amp;#39;5m&amp;#39;),
  min(&amp;#34;value_double&amp;#34;),
  &amp;#39;min&amp;#39; as metric
FROM test_data
WHERE $__timeFilter(&amp;#34;time_date_time&amp;#34;)
GROUP BY time
ORDER BY time&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Data frame result:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;-----------------&amp;#43;
| Name: time          | Name: min       |
| Labels:             | Labels:         |
| Type: []time.Time   | Type: []float64 |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;
| 2020-01-02 03:05:00 | 3               |
| 2020-01-02 03:10:00 | 6               |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To customize default series name formatting, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/configure-standard-options/#display-name&#34;&gt;Standard options definitions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Following are time series query examples.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example with no grouping (raw points):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To return raw time and value points without grouping, use &lt;code&gt;$__time&lt;/code&gt; to alias your date/time column as &lt;code&gt;time&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__time(&amp;#34;time_date_time&amp;#34;),
  &amp;#34;value_double&amp;#34; as value
FROM test_data
WHERE $__timeFilter(&amp;#34;time_date_time&amp;#34;)
ORDER BY time&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Example using the fill parameter in the $__timeGroupAlias macro to convert null values to zero:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(&amp;#34;CreatedAt&amp;#34;,&amp;#39;5m&amp;#39;,0),
  sum(value) as value,
  hostname
FROM test_data
WHERE
  $__timeFilter(&amp;#34;CreatedAt&amp;#34;)
GROUP BY time, hostname
ORDER BY time&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Based on the data frame result in the following example, the time series panel will generate two series named &lt;em&gt;value 10.0.1.1&lt;/em&gt; and &lt;em&gt;value 10.0.1.2&lt;/em&gt;. To display the series names as &lt;em&gt;10.0.1.1&lt;/em&gt; and &lt;em&gt;10.0.1.2&lt;/em&gt;, use the 
    &lt;a href=&#34;/docs/grafana/v12.4/panels-visualizations/configure-standard-options/#display-name&#34;&gt;Standard options definitions&lt;/a&gt; display value &lt;code&gt;${__field.labels.hostname}&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Data frame result:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;---------------------------&amp;#43;---------------------------&amp;#43;
| Name: time          | Name: value               | Name: value               |
| Labels:             | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 |
| Type: []time.Time   | Type: []float64           | Type: []float64           |
&amp;#43;---------------------&amp;#43;---------------------------&amp;#43;---------------------------&amp;#43;
| 2020-01-02 03:05:00 | 3                         | 4                         |
| 2020-01-02 03:10:00 | 6                         | 7                         |
&amp;#43;---------------------&amp;#43;---------------------------&amp;#43;---------------------------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Example with multiple columns:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(&amp;#34;time_date_time&amp;#34;,&amp;#39;5m&amp;#39;),
  min(&amp;#34;value_double&amp;#34;) as &amp;#34;min_value&amp;#34;,
  max(&amp;#34;value_double&amp;#34;) as &amp;#34;max_value&amp;#34;
FROM test_data
WHERE $__timeFilter(&amp;#34;time_date_time&amp;#34;)
GROUP BY time
ORDER BY time&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Data frame result:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;&amp;#43;---------------------&amp;#43;-----------------&amp;#43;-----------------&amp;#43;
| Name: time          | Name: min_value | Name: max_value |
| Labels:             | Labels:         | Labels:         |
| Type: []time.Time   | Type: []float64 | Type: []float64 |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;-----------------&amp;#43;
| 2020-01-02 03:04:00 | 3               | 4               |
| 2020-01-02 03:05:00 | 6               | 7               |
&amp;#43;---------------------&amp;#43;-----------------&amp;#43;-----------------&amp;#43;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Example with UNIX epoch time column:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When your time column stores UNIX epoch (seconds), use &lt;code&gt;$__timeEpoch&lt;/code&gt; to alias it as &lt;code&gt;time&lt;/code&gt; and &lt;code&gt;$__unixEpochFilter&lt;/code&gt; in the WHERE clause:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeEpoch(epoch_seconds_column),
  value_column as value
FROM my_table
WHERE $__unixEpochFilter(epoch_seconds_column)
ORDER BY time&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;For grouped time series with epoch columns, use &lt;code&gt;$__unixEpochGroupAlias&lt;/code&gt; and &lt;code&gt;$__unixEpochFilter&lt;/code&gt;. See the &lt;a href=&#34;#macros&#34;&gt;Macros&lt;/a&gt; table for details.&lt;/p&gt;
&lt;h2 id=&#34;next-steps&#34;&gt;Next steps&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Use 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/template-variables/&#34;&gt;template variables&lt;/a&gt; to create dynamic, reusable dashboards.&lt;/li&gt;
&lt;li&gt;Add 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/annotations/&#34;&gt;annotations&lt;/a&gt; from PostgreSQL to overlay events on your panels.&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/alerting/&#34;&gt;Set up alerting&lt;/a&gt; to get notified when metrics cross thresholds (time series format only).&lt;/li&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/troubleshooting/&#34;&gt;Troubleshoot&lt;/a&gt; issues if you encounter problems with queries.&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="postgresql-query-editor">PostgreSQL query editor&lt;/h1>
&lt;p>The PostgreSQL query editor lets you build and run queries against your data source. For general query editor and data transformation concepts, see
&lt;a href="/docs/grafana/v12.4/panels-visualizations/query-transform-data/">Query and transform data&lt;/a>.&lt;/p></description></item><item><title>PostgreSQL template variables</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/template-variables/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/postgres/template-variables/</guid><content><![CDATA[&lt;h1 id=&#34;postgresql-template-variables&#34;&gt;PostgreSQL template variables&lt;/h1&gt;
&lt;p&gt;Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
Grafana displays these variables in drop-down select boxes at the top of the dashboard to help you change the data displayed in your dashboard.
Grafana refers to such variables as &lt;strong&gt;template variables&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;For an introduction to templating and template variables, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/&#34;&gt;Templating&lt;/a&gt; and 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/&#34;&gt;Add and manage variables&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;query-variable&#34;&gt;Query variable&lt;/h2&gt;
&lt;p&gt;A query variable in Grafana dynamically retrieves values from your data source using a query. With a query variable, you can write a PostgreSQL query that returns values such as measurement names, key names, or key values that are shown in a drop-down select box.&lt;/p&gt;
&lt;p&gt;For example, the following query returns all values from the &lt;code&gt;hostname&lt;/code&gt; column:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname FROM host&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;A query can return multiple columns, and Grafana automatically generates a list using the values from those columns. For example, the following query returns values from both the &lt;code&gt;hostname&lt;/code&gt; and &lt;code&gt;hostname2&lt;/code&gt; columns, which are included in the variable&amp;rsquo;s drop-down list.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To use time range dependent macros like &lt;code&gt;$__timeFilter(column)&lt;/code&gt; in your query, you must set the template variable&amp;rsquo;s refresh mode to &lt;strong&gt;On Time Range Change&lt;/strong&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT event_name FROM event_log WHERE $__timeFilter(time_column)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;keyvalue-variables&#34;&gt;Key/value variables&lt;/h3&gt;
&lt;p&gt;You can create a key/value variable so the drop-down shows a user-friendly label (for example, hostname) while panel queries use a different value (for example, ID). Use the variable editor’s &lt;strong&gt;Value field&lt;/strong&gt; and &lt;strong&gt;Text field&lt;/strong&gt; at the bottom of the query section to specify which query columns supply the value and the label. Your query can use any column names; you do not need &lt;code&gt;__value&lt;/code&gt; or &lt;code&gt;__text&lt;/code&gt; in the SQL.&lt;/p&gt;
&lt;p&gt;Example: run a query that returns &lt;code&gt;hostname&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt;, then set &lt;strong&gt;Text field&lt;/strong&gt; to &lt;code&gt;hostname&lt;/code&gt; and &lt;strong&gt;Value field&lt;/strong&gt; to &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname, id FROM host&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Note that the values in the text column should be unique. If there are duplicates, Grafana uses only the first matching entry.&lt;/p&gt;
&lt;p&gt;Alternatively, you can use the legacy approach: return columns named &lt;code&gt;__text&lt;/code&gt; and &lt;code&gt;__value&lt;/code&gt; in your query (for example, &lt;code&gt;SELECT hostname AS __text, id AS __value FROM host&lt;/code&gt;).&lt;/p&gt;
&lt;h3 id=&#34;nested-variables&#34;&gt;Nested variables&lt;/h3&gt;
&lt;p&gt;You can create nested variables, where one variable depends on the value of another. For example, if you have a variable named &lt;code&gt;region&lt;/code&gt;, you can configure a &lt;code&gt;hosts&lt;/code&gt; variable to only show hosts from the selected region. If &lt;code&gt;region&lt;/code&gt; is a multi-value variable, use the &lt;code&gt;IN&lt;/code&gt; operator instead of &lt;code&gt;=&lt;/code&gt; to match against multiple selected values.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname FROM host WHERE region IN($region)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;filter-results-with-__searchfilter&#34;&gt;Filter results with &lt;code&gt;__searchFilter&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Using &lt;code&gt;__searchFilter&lt;/code&gt; in the query field allows the query results to be filtered based on the user&amp;rsquo;s input in the drop-down selection box. If you don&amp;rsquo;t enter anything, the default value for &lt;code&gt;__searchFilter&lt;/code&gt; is &lt;code&gt;%&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that you must enclose the &lt;code&gt;__searchFilter&lt;/code&gt; expression in quotes as Grafana doesn&amp;rsquo;t add them automatically.&lt;/p&gt;
&lt;p&gt;The following example demonstrates how to use &lt;code&gt;__searchFilter&lt;/code&gt; in the query field to enable real-time searching for &lt;code&gt;hostname&lt;/code&gt; as the user types in the drop-down selection box.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT hostname FROM host WHERE hostname LIKE &amp;#39;$__searchFilter&amp;#39;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;multi-property-variables&#34;&gt;Multi-property variables&lt;/h2&gt;
&lt;p&gt;The PostgreSQL data source supports &lt;strong&gt;multi-property variables&lt;/strong&gt;. Use them when the same logical concept has different identifiers in different contexts (for example, an environment called &lt;code&gt;dev&lt;/code&gt; in one system and &lt;code&gt;development&lt;/code&gt; in another). Instead of maintaining several variables in sync, you can map all of those values to one variable and reference the property you need in each panel or query.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: 601px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link&#34;
        href=&#34;/media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload &#34;
          data-src=&#34;/media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png&#34;data-srcset=&#34;/media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=320 320w, /media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=550 550w, /media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=750 750w, /media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=900 900w, /media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=1040 1040w, /media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=1240 1240w, /media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png?w=1920 1920w&#34;data-sizes=&#34;auto&#34;alt=&#34;PostgreSQL multi-property variable example&#34;width=&#34;601&#34;height=&#34;475&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;/media/docs/postgres/postgreSQL-multi-prop-variable-v12.3.png&#34;
            alt=&#34;PostgreSQL multi-property variable example&#34;width=&#34;601&#34;height=&#34;475&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;You can create a multi-property variable with either &lt;strong&gt;Type: Custom&lt;/strong&gt; or &lt;strong&gt;Type: Query&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Type: Custom&lt;/strong&gt; – In &lt;strong&gt;Custom options&lt;/strong&gt; &amp;gt; &lt;strong&gt;JSON&lt;/strong&gt;, paste your own JSON array with the mapping. Each object in the array can have any number of properties; use &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; for the label and value shown in the drop-down, and add additional properties as needed. For the JSON format and examples, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/#multi-property-custom-variables&#34;&gt;Multi-property custom variables&lt;/a&gt; in Add and manage variables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Type: Query&lt;/strong&gt; – Write a SQL query that returns multiple columns. In the variable editor, set &lt;strong&gt;Value field&lt;/strong&gt; and &lt;strong&gt;Text field&lt;/strong&gt; to the columns that supply the value and the label for the drop-down. Add one column per property you want to reference; each column name becomes a property name. In panels and queries, reference a property with &lt;code&gt;${varName.columnName}&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Example (Type: Query):&lt;/strong&gt; A variable named &lt;code&gt;env&lt;/code&gt; that lists environments with different identifiers per cloud. In the variable editor, set &lt;strong&gt;Text field&lt;/strong&gt; to &lt;code&gt;name&lt;/code&gt; and &lt;strong&gt;Value field&lt;/strong&gt; to &lt;code&gt;id&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  name,
  id,
  aws_identifier AS env_aws,
  azure_identifier AS env_azure
FROM environments&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In a panel query you might use &lt;code&gt;$env.env_aws&lt;/code&gt; for an AWS-related query and &lt;code&gt;$env.env_azure&lt;/code&gt; for an Azure-related query. For more on the concept, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/#configure-multi-property-variables&#34;&gt;Configure multi-property variables&lt;/a&gt; in 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/add-template-variables/&#34;&gt;Add and manage variables&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;use-variables-in-queries&#34;&gt;Use variables in queries&lt;/h2&gt;
&lt;p&gt;Grafana automatically quotes template variable values only when the template variable is a &lt;strong&gt;multi-value&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;When using a multi-value variable, use the &lt;code&gt;IN&lt;/code&gt; comparison operator instead of &lt;code&gt;=&lt;/code&gt; to match against multiple values.&lt;/p&gt;
&lt;p&gt;Grafana supports two syntaxes for using variables in queries:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;$&amp;lt;varname&amp;gt;&lt;/code&gt; syntax&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example with a template variable named &lt;code&gt;hostname&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  atimestamp AS time,
  aint AS value
FROM table
WHERE $__timeFilter(atimestamp) AND hostname IN($hostname)
ORDER BY atimestamp ASC&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;[[varname]]&lt;/code&gt; syntax&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example with a template variable named &lt;code&gt;hostname&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  atimestamp AS time,
  aint AS value
FROM table
WHERE $__timeFilter(atimestamp) AND hostname IN([[hostname]])
ORDER BY atimestamp ASC&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;disable-quoting-for-multi-value-variables&#34;&gt;Disable quoting for multi-value variables&lt;/h3&gt;
&lt;p&gt;By default, Grafana formats multi-value variables as a quoted, comma-separated string. For example, if &lt;code&gt;server01&lt;/code&gt; and &lt;code&gt;server02&lt;/code&gt; are selected, the result will be &lt;code&gt;&#39;server01&#39;&lt;/code&gt;, &lt;code&gt;&#39;server02&#39;&lt;/code&gt;. To disable quoting, use the &lt;code&gt;csv&lt;/code&gt; formatting option for variables:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;${servers:csv}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This outputs the values as an unquoted comma-separated list.&lt;/p&gt;
&lt;p&gt;Refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/variables/variable-syntax/#advanced-variable-format-options&#34;&gt;Advanced variable format options&lt;/a&gt; for additional information.&lt;/p&gt;
]]></content><description>&lt;h1 id="postgresql-template-variables">PostgreSQL template variables&lt;/h1>
&lt;p>Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables.
Grafana displays these variables in drop-down select boxes at the top of the dashboard to help you change the data displayed in your dashboard.
Grafana refers to such variables as &lt;strong>template variables&lt;/strong>.&lt;/p></description></item><item><title>PostgreSQL annotations</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/annotations/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/postgres/annotations/</guid><content><![CDATA[&lt;h1 id=&#34;postgresql-annotations&#34;&gt;PostgreSQL annotations&lt;/h1&gt;
&lt;p&gt;Annotations overlay event data on your dashboard graphs, helping you correlate events with metrics.
You can use PostgreSQL as a data source for annotations to display events such as deployments, alerts, or other significant occurrences on your visualizations.&lt;/p&gt;
&lt;p&gt;For general information about annotations, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/dashboards/build-dashboards/annotate-visualizations/&#34;&gt;Annotate visualizations&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;p&gt;Before creating PostgreSQL annotations, ensure you have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/configure/&#34;&gt;A configured PostgreSQL data source&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Tables containing event data with timestamp fields.&lt;/li&gt;
&lt;li&gt;Read access to the tables containing your events.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;create-an-annotation-query&#34;&gt;Create an annotation query&lt;/h2&gt;
&lt;p&gt;To add a PostgreSQL annotation to your dashboard:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Navigate to your dashboard and click &lt;strong&gt;Dashboard settings&lt;/strong&gt; (gear icon).&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Annotations&lt;/strong&gt; in the left menu.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add annotation query&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Enter a &lt;strong&gt;Name&lt;/strong&gt; for the annotation.&lt;/li&gt;
&lt;li&gt;Select your &lt;strong&gt;PostgreSQL&lt;/strong&gt; data source from the &lt;strong&gt;Data source&lt;/strong&gt; drop-down.&lt;/li&gt;
&lt;li&gt;Write a SQL query that returns the required columns.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Save dashboard&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;query-columns&#34;&gt;Query columns&lt;/h2&gt;
&lt;p&gt;Your annotation query must return a &lt;code&gt;time&lt;/code&gt; column and can optionally include &lt;code&gt;timeend&lt;/code&gt;, &lt;code&gt;text&lt;/code&gt;, and &lt;code&gt;tags&lt;/code&gt; columns.&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Column&lt;/th&gt;
              &lt;th&gt;Required&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;time&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Yes&lt;/td&gt;
              &lt;td&gt;The timestamp for the annotation. Can be a native SQL date/time type or UNIX epoch value.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;timeend&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;No&lt;/td&gt;
              &lt;td&gt;The end timestamp for range annotations. Creates a shaded region instead of a vertical line.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;text&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;No&lt;/td&gt;
              &lt;td&gt;The annotation description displayed when you hover over the annotation.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;tags&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;No&lt;/td&gt;
              &lt;td&gt;Tags for the annotation as a comma-separated string. Helps categorize and filter annotations.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;example-queries&#34;&gt;Example queries&lt;/h2&gt;
&lt;p&gt;The following examples show common annotation query patterns.&lt;/p&gt;
&lt;h3 id=&#34;basic-annotation-with-epoch-time&#34;&gt;Basic annotation with epoch time&lt;/h3&gt;
&lt;p&gt;Display events using UNIX epoch timestamps:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  epoch_time as time,
  metric1 as text,
  concat_ws(&amp;#39;, &amp;#39;, metric1::text, metric2::text) as tags
FROM public.test_data
WHERE $__unixEpochFilter(epoch_time)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;range-annotation-with-start-and-end-time&#34;&gt;Range annotation with start and end time&lt;/h3&gt;
&lt;p&gt;Display events with duration as shaded regions:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  epoch_time as time,
  epoch_time_end as timeend,
  metric1 as text,
  concat_ws(&amp;#39;, &amp;#39;, metric1::text, metric2::text) as tags
FROM public.test_data
WHERE $__unixEpochFilter(epoch_time)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;annotation-with-native-sql-datetime&#34;&gt;Annotation with native SQL date/time&lt;/h3&gt;
&lt;p&gt;Display events using native PostgreSQL date/time columns:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  native_date_time as time,
  metric1 as text,
  concat_ws(&amp;#39;, &amp;#39;, metric1::text, metric2::text) as tags
FROM public.test_data
WHERE $__timeFilter(native_date_time)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;deployment-annotations&#34;&gt;Deployment annotations&lt;/h3&gt;
&lt;p&gt;Display deployment events:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  deployed_at as time,
  concat(&amp;#39;Deployed &amp;#39;, version, &amp;#39; to &amp;#39;, environment) as text,
  environment as tags
FROM deployments
WHERE $__timeFilter(deployed_at)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;range-annotation-for-maintenance-windows&#34;&gt;Range annotation for maintenance windows&lt;/h3&gt;
&lt;p&gt;Display maintenance windows as shaded regions:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  start_time as time,
  end_time as timeend,
  concat(&amp;#39;Maintenance: &amp;#39;, description) as text,
  &amp;#39;maintenance&amp;#39; as tags
FROM maintenance_windows
WHERE $__timeFilter(start_time)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;macros&#34;&gt;Macros&lt;/h2&gt;
&lt;p&gt;Use these macros in your annotation queries to filter by the dashboard time range:&lt;/p&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Macro&lt;/th&gt;
              &lt;th&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__timeFilter(column)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Filters by time range using a native SQL date/time column.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;code&gt;$__unixEpochFilter(column)&lt;/code&gt;&lt;/td&gt;
              &lt;td&gt;Filters by time range using a column with UNIX epoch timestamps.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;best-practices&#34;&gt;Best practices&lt;/h2&gt;
&lt;p&gt;Follow these best practices when creating PostgreSQL annotations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use time filters:&lt;/strong&gt; Always include &lt;code&gt;$__timeFilter()&lt;/code&gt; or &lt;code&gt;$__unixEpochFilter()&lt;/code&gt; to limit results to the dashboard time range.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep queries efficient:&lt;/strong&gt; Add indexes on time columns and filter columns to improve query performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Use meaningful text:&lt;/strong&gt; Include descriptive information in the &lt;code&gt;text&lt;/code&gt; column to make annotations useful.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Organize with tags:&lt;/strong&gt; Use consistent tag values to categorize annotations and enable filtering.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test queries first:&lt;/strong&gt; Verify your query returns expected results in Explore before adding it as an annotation.&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="postgresql-annotations">PostgreSQL annotations&lt;/h1>
&lt;p>Annotations overlay event data on your dashboard graphs, helping you correlate events with metrics.
You can use PostgreSQL as a data source for annotations to display events such as deployments, alerts, or other significant occurrences on your visualizations.&lt;/p></description></item><item><title>PostgreSQL alerting</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/alerting/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/postgres/alerting/</guid><content><![CDATA[&lt;h1 id=&#34;postgresql-alerting&#34;&gt;PostgreSQL alerting&lt;/h1&gt;
&lt;p&gt;The PostgreSQL data source supports 
    &lt;a href=&#34;/docs/grafana/v12.4/alerting/&#34;&gt;Grafana Alerting&lt;/a&gt;. You can create alert rules that evaluate time series queries against your PostgreSQL database and send notifications when conditions are met.&lt;/p&gt;
&lt;h2 id=&#34;before-you-begin&#34;&gt;Before you begin&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Ensure your 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/configure/&#34;&gt;PostgreSQL data source is configured&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Familiarize yourself with 
    &lt;a href=&#34;/docs/grafana/v12.4/alerting/fundamentals/alert-rules/&#34;&gt;Alert rules&lt;/a&gt; and 
    &lt;a href=&#34;/docs/grafana/v12.4/alerting/alerting-rules/create-grafana-managed-rule/&#34;&gt;Create a Grafana-managed alert rule&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;supported-query-format&#34;&gt;Supported query format&lt;/h2&gt;
&lt;p&gt;Only &lt;strong&gt;time series&lt;/strong&gt; queries can be used in alert rule conditions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Your query must return a column named &lt;code&gt;time&lt;/code&gt; (native SQL date/time or UNIX epoch) and one or more numeric value columns.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Table&lt;/strong&gt; formatted queries are not supported in alert rule conditions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For details on writing time series queries, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/query-editor/#time-series-queries&#34;&gt;Time series queries&lt;/a&gt; in the PostgreSQL query editor.&lt;/p&gt;
&lt;h2 id=&#34;create-an-alert-rule&#34;&gt;Create an alert rule&lt;/h2&gt;
&lt;p&gt;To create an alert rule that uses PostgreSQL:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Alerting&lt;/strong&gt; (bell icon) in the left menu and select &lt;strong&gt;Alert rules&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;New alert rule&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the query section, select your &lt;strong&gt;PostgreSQL&lt;/strong&gt; data source.&lt;/li&gt;
&lt;li&gt;Set the query &lt;strong&gt;Format&lt;/strong&gt; to &lt;strong&gt;Time series&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Write a SQL query that returns a &lt;code&gt;time&lt;/code&gt; column and numeric value(s).&lt;/li&gt;
&lt;li&gt;Configure the condition, evaluation group, and notification settings.&lt;/li&gt;
&lt;li&gt;Save the rule.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For step-by-step guidance, refer to 
    &lt;a href=&#34;/docs/grafana/v12.4/alerting/alerting-rules/create-grafana-managed-rule/&#34;&gt;Create a Grafana-managed alert rule&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;example-time-series-query-for-alerting&#34;&gt;Example time series query for alerting&lt;/h2&gt;
&lt;p&gt;The following query returns a time series suitable for a threshold alert (e.g. alert when value exceeds a limit):&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT
  $__timeGroupAlias(&amp;#34;time_date_time&amp;#34;, &amp;#39;5m&amp;#39;),
  avg(&amp;#34;value_double&amp;#34;) AS value
FROM test_data
WHERE $__timeFilter(&amp;#34;time_date_time&amp;#34;)
GROUP BY time
ORDER BY time&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Use condition types such as &lt;strong&gt;Is above&lt;/strong&gt; or &lt;strong&gt;Is below&lt;/strong&gt; in the alert rule to evaluate the series.&lt;/p&gt;
&lt;h2 id=&#34;template-annotations-and-labels&#34;&gt;Template annotations and labels&lt;/h2&gt;
&lt;p&gt;You can use 
    &lt;a href=&#34;/docs/grafana/v12.4/alerting/alerting-rules/templates/&#34;&gt;template annotations and labels&lt;/a&gt; to include query results or metadata in alert notifications and labels.&lt;/p&gt;
]]></content><description>&lt;h1 id="postgresql-alerting">PostgreSQL alerting&lt;/h1>
&lt;p>The PostgreSQL data source supports
&lt;a href="/docs/grafana/v12.4/alerting/">Grafana Alerting&lt;/a>. You can create alert rules that evaluate time series queries against your PostgreSQL database and send notifications when conditions are met.&lt;/p></description></item><item><title>Troubleshoot PostgreSQL data source issues</title><link>https://grafana.com/docs/grafana/v12.4/datasources/postgres/troubleshooting/</link><pubDate>Fri, 03 Apr 2026 12:35:46 -0500</pubDate><guid>https://grafana.com/docs/grafana/v12.4/datasources/postgres/troubleshooting/</guid><content><![CDATA[&lt;h1 id=&#34;troubleshoot-postgresql-data-source-issues&#34;&gt;Troubleshoot PostgreSQL data source issues&lt;/h1&gt;
&lt;p&gt;This document provides troubleshooting information for common errors you may encounter when using the PostgreSQL data source in Grafana.&lt;/p&gt;
&lt;h2 id=&#34;connection-errors&#34;&gt;Connection errors&lt;/h2&gt;
&lt;p&gt;The following errors occur when Grafana cannot establish or maintain a connection to PostgreSQL.&lt;/p&gt;
&lt;h3 id=&#34;failed-to-connect-to-postgresql&#34;&gt;Failed to connect to PostgreSQL&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : connect: connection refused&lt;/code&gt; or &lt;code&gt;dial tcp: connect: connection refused&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Grafana cannot establish a network connection to the PostgreSQL server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify that the Host URL is correct in the data source configuration.&lt;/li&gt;
&lt;li&gt;Check that PostgreSQL is running and accessible from the Grafana server.&lt;/li&gt;
&lt;li&gt;Verify the port is correct (the PostgreSQL default port is &lt;code&gt;5432&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Ensure there are no firewall rules blocking the connection.&lt;/li&gt;
&lt;li&gt;Check that PostgreSQL is configured to accept connections from the Grafana server in &lt;code&gt;pg_hba.conf&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For Grafana Cloud, ensure you have configured &lt;a href=&#34;/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/&#34;&gt;Private data source connect&lt;/a&gt; if your PostgreSQL instance is not publicly accessible.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;request-timed-out&#34;&gt;Request timed out&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;context deadline exceeded&amp;rdquo; or &amp;ldquo;i/o timeout&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The connection to PostgreSQL timed out before receiving a response.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the network latency between Grafana and PostgreSQL.&lt;/li&gt;
&lt;li&gt;Verify that PostgreSQL is not overloaded or experiencing performance issues.&lt;/li&gt;
&lt;li&gt;Increase the &lt;strong&gt;Max lifetime&lt;/strong&gt; setting in the data source configuration under &lt;strong&gt;Connection limits&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Reduce the time range or complexity of your query.&lt;/li&gt;
&lt;li&gt;Check if any network devices (load balancers, proxies) are timing out the connection.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;host-not-found&#34;&gt;Host not found&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : hostname resolving error&lt;/code&gt; or &lt;code&gt;lookup hostname: no such host&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The hostname specified in the data source configuration cannot be resolved.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the hostname is spelled correctly.&lt;/li&gt;
&lt;li&gt;Check that DNS resolution is working on the Grafana server.&lt;/li&gt;
&lt;li&gt;Try using an IP address instead of a hostname.&lt;/li&gt;
&lt;li&gt;Ensure the PostgreSQL server is accessible from the Grafana server&amp;rsquo;s network.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;authentication-errors&#34;&gt;Authentication errors&lt;/h2&gt;
&lt;p&gt;The following errors occur when there are issues with authentication credentials or permissions.&lt;/p&gt;
&lt;h3 id=&#34;password-authentication-failed&#34;&gt;Password authentication failed&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : server error: FATAL: password authentication failed for user &amp;quot;username&amp;quot; (SQLSTATE 28P01)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The username or password is incorrect.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify that the username and password are correct in the data source configuration.&lt;/li&gt;
&lt;li&gt;Check that the user exists in PostgreSQL.&lt;/li&gt;
&lt;li&gt;Verify the password has not expired.&lt;/li&gt;
&lt;li&gt;If no password is specified, ensure a &lt;a href=&#34;https://www.postgresql.org/docs/current/static/libpq-pgpass.html&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PostgreSQL password file&lt;/a&gt; is configured.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;permission-denied&#34;&gt;Permission denied&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;ERROR: permission denied for table table_name (SQLSTATE 42501)&lt;/code&gt; or &lt;code&gt;ERROR: permission denied for schema schema_name (SQLSTATE 42501)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The database user does not have permission to access the requested table or schema.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Verify the user has &lt;code&gt;SELECT&lt;/code&gt; permissions on the required tables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Grant the necessary permissions:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;SQL&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-sql&#34;&gt;GRANT USAGE ON SCHEMA schema_name TO grafanareader;
GRANT SELECT ON schema_name.table_name TO grafanareader;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Check that the user has access to the correct database.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Verify the search path includes the schema containing your tables.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;no-pg_hbaconf-entry&#34;&gt;No pg_hba.conf entry&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : server error: FATAL: no pg_hba.conf entry for host &amp;quot;ip_address&amp;quot;, user &amp;quot;username&amp;quot;, database &amp;quot;database_name&amp;quot; (SQLSTATE 28000)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; PostgreSQL is not configured to accept connections from the Grafana server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Edit the &lt;code&gt;pg_hba.conf&lt;/code&gt; file on the PostgreSQL server.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add an entry to allow connections from the Grafana server:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;text&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-text&#34;&gt;host    database_name    username    grafana_ip/32    md5&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Reload PostgreSQL configuration: &lt;code&gt;SELECT pg_reload_conf();&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If using SSL, ensure the correct authentication method is specified (for example, &lt;code&gt;hostssl&lt;/code&gt; instead of &lt;code&gt;host&lt;/code&gt;).&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;tls-and-certificate-errors&#34;&gt;TLS and certificate errors&lt;/h2&gt;
&lt;p&gt;The following errors occur when there are issues with TLS configuration.&lt;/p&gt;
&lt;h3 id=&#34;certificate-verification-failed&#34;&gt;Certificate verification failed&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;x509: certificate signed by unknown authority&amp;rdquo; or &amp;ldquo;certificate verify failed&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Grafana cannot verify the TLS certificate presented by PostgreSQL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Set the &lt;strong&gt;TLS/SSL Mode&lt;/strong&gt; to the appropriate level (&lt;code&gt;require&lt;/code&gt;, &lt;code&gt;verify-ca&lt;/code&gt;, or &lt;code&gt;verify-full&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;If using a self-signed certificate, add the CA certificate in &lt;strong&gt;TLS/SSL Auth Details&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Verify the certificate chain is complete and valid.&lt;/li&gt;
&lt;li&gt;Ensure the certificate has not expired.&lt;/li&gt;
&lt;li&gt;For testing only, set &lt;strong&gt;TLS/SSL Mode&lt;/strong&gt; to &lt;code&gt;disable&lt;/code&gt; (not recommended for production).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;ssl-not-supported&#34;&gt;SSL not supported&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : server refused TLS connection&lt;/code&gt; or &lt;code&gt;server does not support SSL&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The PostgreSQL server is not configured for SSL connections, but the data source requires SSL.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Set &lt;strong&gt;TLS/SSL Mode&lt;/strong&gt; to &lt;code&gt;disable&lt;/code&gt; if SSL is not required.&lt;/li&gt;
&lt;li&gt;Alternatively, enable SSL on the PostgreSQL server by configuring &lt;code&gt;ssl = on&lt;/code&gt; in &lt;code&gt;postgresql.conf&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Ensure the server has valid SSL certificates configured.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;client-certificate-error&#34;&gt;Client certificate error&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;TLS: failed to find any PEM data in certificate input&amp;rdquo; or &amp;ldquo;could not load client certificate&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The client certificate or key is invalid or incorrectly formatted.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the certificate and key are in PEM format.&lt;/li&gt;
&lt;li&gt;Ensure the certificate file path is correct and readable by the Grafana process.&lt;/li&gt;
&lt;li&gt;Check that the certificate and key match (belong to the same key pair).&lt;/li&gt;
&lt;li&gt;If using certificate content, ensure you&amp;rsquo;ve pasted the complete certificate including headers.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;database-errors&#34;&gt;Database errors&lt;/h2&gt;
&lt;p&gt;The following errors occur when there are issues with the database configuration.&lt;/p&gt;
&lt;h3 id=&#34;database-does-not-exist&#34;&gt;Database does not exist&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : server error: FATAL: database &amp;quot;database_name&amp;quot; does not exist (SQLSTATE 3D000)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The specified database name is incorrect or the database doesn&amp;rsquo;t exist.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the database name in the data source configuration.&lt;/li&gt;
&lt;li&gt;Check that the database exists: &lt;code&gt;\l&lt;/code&gt; in psql or &lt;code&gt;SELECT datname FROM pg_database;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Ensure the database name is case-sensitive and matches exactly.&lt;/li&gt;
&lt;li&gt;Verify the user has permission to connect to the database.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;relation-does-not-exist&#34;&gt;Relation does not exist&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;ERROR: relation &amp;quot;table_name&amp;quot; does not exist (SQLSTATE 42P01)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The specified table or view does not exist, or the user cannot access it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the table name is correct and exists in the database.&lt;/li&gt;
&lt;li&gt;Check the schema name if the table is not in the public schema.&lt;/li&gt;
&lt;li&gt;Use fully qualified names: &lt;code&gt;schema_name.table_name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Verify the user has &lt;code&gt;SELECT&lt;/code&gt; permission on the table.&lt;/li&gt;
&lt;li&gt;Check the search path: &lt;code&gt;SHOW search_path;&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;query-errors&#34;&gt;Query errors&lt;/h2&gt;
&lt;p&gt;The following errors occur when there are issues with SQL syntax or query execution.&lt;/p&gt;
&lt;h3 id=&#34;query-syntax-error&#34;&gt;Query syntax error&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;ERROR: syntax error at or near &amp;quot;keyword&amp;quot; (SQLSTATE 42601)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The SQL query contains invalid syntax.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check your query syntax for typos or invalid keywords.&lt;/li&gt;
&lt;li&gt;Verify column and table names are correctly quoted if they contain special characters or are reserved words.&lt;/li&gt;
&lt;li&gt;Use double quotes for identifiers: &lt;code&gt;&amp;quot;column_name&amp;quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Test the query directly in a PostgreSQL client (psql, pgAdmin).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;column-does-not-exist&#34;&gt;Column does not exist&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;ERROR: column &amp;quot;column_name&amp;quot; does not exist (SQLSTATE 42703)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The specified column name is incorrect or doesn&amp;rsquo;t exist in the table.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the column name is spelled correctly.&lt;/li&gt;
&lt;li&gt;Check that column names are case-sensitive in PostgreSQL when quoted.&lt;/li&gt;
&lt;li&gt;Use the correct quoting for column names: &lt;code&gt;&amp;quot;Column_Name&amp;quot;&lt;/code&gt; for case-sensitive names.&lt;/li&gt;
&lt;li&gt;Verify the column exists in the table: &lt;code&gt;\d table_name&lt;/code&gt; in psql.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;no-time-column-found&#34;&gt;No time column found&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;no time column found&amp;rdquo; or time series visualization shows no data&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The query result does not include a properly formatted time column.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ensure your query includes a column named &lt;code&gt;time&lt;/code&gt; that returns a timestamp or epoch value.&lt;/li&gt;
&lt;li&gt;Use an alias to rename your time column: &lt;code&gt;SELECT created_at AS time&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Ensure the time column is of type &lt;code&gt;timestamp&lt;/code&gt;, &lt;code&gt;timestamptz&lt;/code&gt;, or a numeric epoch value.&lt;/li&gt;
&lt;li&gt;Order results by the time column: &lt;code&gt;ORDER BY time ASC&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;macro-expansion-error&#34;&gt;Macro expansion error&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;macro &amp;lsquo;$__timeFilter&amp;rsquo; not found&amp;rdquo; or incorrect query results with macros&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Grafana macros are not being properly expanded.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the macro syntax is correct, for example &lt;code&gt;$__timeFilter(time_column)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Ensure the column name passed to the macro exists in your table.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;Preview&lt;/strong&gt; toggle in Builder mode to see the expanded query.&lt;/li&gt;
&lt;li&gt;For time-based macros, ensure the column contains timestamp data.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;performance-issues&#34;&gt;Performance issues&lt;/h2&gt;
&lt;p&gt;The following issues relate to slow query execution or resource constraints.&lt;/p&gt;
&lt;h3 id=&#34;query-timeout&#34;&gt;Query timeout&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;canceling statement due to statement timeout&amp;rdquo; or &amp;ldquo;query timeout&amp;rdquo;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The query took longer than the configured timeout.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reduce the time range of your query.&lt;/li&gt;
&lt;li&gt;Add indexes to columns used in WHERE clauses and joins.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;$__timeFilter&lt;/code&gt; macro to limit data to the dashboard time range.&lt;/li&gt;
&lt;li&gt;Increase the statement timeout in PostgreSQL if you have admin access.&lt;/li&gt;
&lt;li&gt;Optimize your query to reduce complexity.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;too-many-connections&#34;&gt;Too many connections&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &lt;code&gt;failed to connect to ... : server error: FATAL: too many connections for role &amp;quot;username&amp;quot; (SQLSTATE 53300)&lt;/code&gt; or &lt;code&gt;connection pool exhausted&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The maximum number of connections to PostgreSQL has been reached.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reduce the &lt;strong&gt;Max open&lt;/strong&gt; connections setting in the data source configuration.&lt;/li&gt;
&lt;li&gt;Increase &lt;code&gt;max_connections&lt;/code&gt; in PostgreSQL&amp;rsquo;s &lt;code&gt;postgresql.conf&lt;/code&gt; if you have admin access.&lt;/li&gt;
&lt;li&gt;Check for connection leaks in other applications connecting to the same database.&lt;/li&gt;
&lt;li&gt;Enable &lt;strong&gt;Auto max idle&lt;/strong&gt; to automatically manage idle connections.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;slow-query-performance&#34;&gt;Slow query performance&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Queries take a long time to execute.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Reduce the time range of your query.&lt;/li&gt;
&lt;li&gt;Add appropriate indexes to your tables.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;$__timeFilter&lt;/code&gt; macro to limit the data scanned.&lt;/li&gt;
&lt;li&gt;Increase the &lt;strong&gt;Min time interval&lt;/strong&gt; setting to reduce the number of data points.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; in PostgreSQL to identify query bottlenecks.&lt;/li&gt;
&lt;li&gt;Consider using materialized views for complex aggregations.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;provisioning-errors&#34;&gt;Provisioning errors&lt;/h2&gt;
&lt;p&gt;The following errors occur when provisioning the data source via YAML.&lt;/p&gt;
&lt;h3 id=&#34;invalid-provisioning-configuration&#34;&gt;Invalid provisioning configuration&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Error message:&lt;/strong&gt; &amp;ldquo;metric request error&amp;rdquo; or data source test fails after provisioning&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The provisioning YAML file contains incorrect configuration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ensure parameter names match the expected format exactly.&lt;/li&gt;
&lt;li&gt;Verify the database name is &lt;strong&gt;not&lt;/strong&gt; included in the URL.&lt;/li&gt;
&lt;li&gt;Use the correct format for the URL: &lt;code&gt;hostname:port&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check that string values are properly quoted in the YAML file.&lt;/li&gt;
&lt;li&gt;Refer to the 
    &lt;a href=&#34;/docs/grafana/v12.4/datasources/postgres/configure/#provision-the-data-source&#34;&gt;provisioning example&lt;/a&gt; for the correct format.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Example correct configuration:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;datasources:
  - name: Postgres
    type: postgres
    url: localhost:5432
    user: grafana
    secureJsonData:
      password: &amp;#39;Password!&amp;#39;
    jsonData:
      database: grafana
      sslmode: &amp;#39;disable&amp;#39;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;other-common-issues&#34;&gt;Other common issues&lt;/h2&gt;
&lt;p&gt;The following issues don&amp;rsquo;t produce specific error messages but are commonly encountered.&lt;/p&gt;
&lt;h3 id=&#34;empty-query-results&#34;&gt;Empty query results&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The query returns no data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Verify the time range includes data in your database.&lt;/li&gt;
&lt;li&gt;Check that table and column names are correct.&lt;/li&gt;
&lt;li&gt;Test the query directly in PostgreSQL.&lt;/li&gt;
&lt;li&gt;Ensure filters are not excluding all data.&lt;/li&gt;
&lt;li&gt;Verify the &lt;code&gt;$__timeFilter&lt;/code&gt; macro is using the correct time column.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;timescaledb-functions-not-available&#34;&gt;TimescaleDB functions not available&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; TimescaleDB-specific functions like &lt;code&gt;time_bucket&lt;/code&gt; are not available in the query builder.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Enable the &lt;strong&gt;TimescaleDB&lt;/strong&gt; toggle in the data source configuration under &lt;strong&gt;PostgreSQL Options&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Verify TimescaleDB is installed and enabled in your PostgreSQL database.&lt;/li&gt;
&lt;li&gt;Check that the &lt;code&gt;timescaledb&lt;/code&gt; extension is created: &lt;code&gt;CREATE EXTENSION IF NOT EXISTS timescaledb;&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;data-appears-delayed-or-missing-recent-points&#34;&gt;Data appears delayed or missing recent points&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The visualization doesn&amp;rsquo;t show the most recent data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the dashboard time range and refresh settings.&lt;/li&gt;
&lt;li&gt;Verify the &lt;strong&gt;Min time interval&lt;/strong&gt; is not set too high.&lt;/li&gt;
&lt;li&gt;Ensure data has been committed to the database (not in an uncommitted transaction).&lt;/li&gt;
&lt;li&gt;Check for clock synchronization issues between Grafana and PostgreSQL.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;get-additional-help&#34;&gt;Get additional help&lt;/h2&gt;
&lt;p&gt;If you continue to experience issues after following this troubleshooting guide:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the &lt;a href=&#34;https://www.postgresql.org/docs/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PostgreSQL documentation&lt;/a&gt; for database-specific guidance.&lt;/li&gt;
&lt;li&gt;Review the &lt;a href=&#34;https://community.grafana.com/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana community forums&lt;/a&gt; for similar issues.&lt;/li&gt;
&lt;li&gt;Contact Grafana Support if you are a Cloud Pro, Cloud Contracted, or Enterprise user.&lt;/li&gt;
&lt;li&gt;When reporting issues, include:
&lt;ul&gt;
&lt;li&gt;Grafana version&lt;/li&gt;
&lt;li&gt;PostgreSQL version&lt;/li&gt;
&lt;li&gt;Error messages (redact sensitive information)&lt;/li&gt;
&lt;li&gt;Steps to reproduce&lt;/li&gt;
&lt;li&gt;Relevant configuration such as data source settings, TLS mode, and connection limits (redact passwords and other credentials)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
]]></content><description>&lt;h1 id="troubleshoot-postgresql-data-source-issues">Troubleshoot PostgreSQL data source issues&lt;/h1>
&lt;p>This document provides troubleshooting information for common errors you may encounter when using the PostgreSQL data source in Grafana.&lt;/p>
&lt;h2 id="connection-errors">Connection errors&lt;/h2>
&lt;p>The following errors occur when Grafana cannot establish or maintain a connection to PostgreSQL.&lt;/p></description></item></channel></rss>