1+++
2title = "PostgreSQL"
3description = "Guide for using PostgreSQL in Grafana"
4keywords = ["grafana", "postgresql", "guide"]
5aliases = ["/docs/grafana/latest/features/datasources/postgres/"]
6weight = 1200
7+++
8
9# PostgreSQL data source
10
11Grafana ships with a built-in PostgreSQL data source plugin that allows you to query and visualize data from a PostgreSQL compatible database. This topic explains options, variables, querying, and other options specific to this data source. For instructions about how to add a data source to Grafana, refer to [Add a data source]({{< relref "add-a-data-source.md" >}}). Only users with the organization admin role can add data sources.
12
13## PostgreSQL settings
14
15To access PostgreSQL settings, hover your mouse over the **Configuration** (gear) icon, then click **Data Sources**, and then click the PostgreSQL data source.
16
17| Name                      | Description                                                                                                                                                                                                                             |
18| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
19| `Name`                    | The data source name. This is how you refer to the data source in panels and queries.                                                                                                                                                   |
20| `Default`                 | Default data source means that it will be pre-selected for new panels.                                                                                                                                                                  |
21| `Host`                    | The IP address/hostname and optional port of your PostgreSQL instance. _Do not_ include the database name. The connection string for connecting to Postgres will not be correct and it may cause errors.                                |
22| `Database`                | Name of your PostgreSQL database.                                                                                                                                                                                                       |
23| `User`                    | Database user's login/username                                                                                                                                                                                                          |
24| `Password`                | Database user's password                                                                                                                                                                                                                |
25| `SSL Mode`                | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. When SSL Mode is disabled, SSL Method and Auth Details would not be visible.                                                |
26| `SSL Auth Details Method` | Determines whether the SSL Auth details will be configured as a file path or file content. Grafana v7.5+                                                                                                                                |
27| `SSL Auth Details Value`  | File path or file content of SSL root certificate, client certificate and client key                                                                                                                                                    |
28| `Max open`                | The maximum number of open connections to the database, default `unlimited` (Grafana v5.4+).                                                                                                                                            |
29| `Max idle`                | The maximum number of connections in the idle connection pool, default `2` (Grafana v5.4+).                                                                                                                                             |
30| `Max lifetime`            | The maximum amount of time in seconds a connection may be reused, default `14400`/4 hours (Grafana v5.4+).                                                                                                                              |
31| `Version`                 | Determines which functions are available in the query builder (only available in Grafana 5.3+).                                                                                                                                         |
32| `TimescaleDB`             | A time-series database built as a PostgreSQL extension. When enabled, Grafana uses `time_bucket` in the `$__timeGroup` macro to display TimescaleDB specific aggregate functions in the query builder (only available in Grafana 5.3+). |
33
34### Min time interval
35
36A lower limit for the [$__interval]({{< relref "../variables/variable-types/_index.md#the-interval-variable" >}}) and [$__interval_ms]({{< relref "../variables/variable-types/_index.md#the-interval-ms-variable" >}}) variables.
37Recommended to be set to write frequency, for example `1m` if your data is written every minute.
38This option can also be overridden/configured in a dashboard panel under data source options. It's important to note that this value **needs** to be formatted as a
39number followed by a valid time identifier, e.g. `1m` (1 minute) or `30s` (30 seconds). The following time identifiers are supported:
40
41| Identifier | Description |
42| ---------- | ----------- |
43| `y`        | year        |
44| `M`        | month       |
45| `w`        | week        |
46| `d`        | day         |
47| `h`        | hour        |
48| `m`        | minute      |
49| `s`        | second      |
50| `ms`       | millisecond |
51
52### Database user permissions (Important!)
53
54The database user you specify when you add the data source should only be granted SELECT permissions on
55the specified database and tables you want to query. Grafana does not validate that the query is safe. The query
56could include any SQL statement. For example, statements like `DELETE FROM user;` and `DROP TABLE user;` would be
57executed. To protect against this we **highly** recommend you create a specific PostgreSQL user with restricted permissions.
58
59Example:
60
61```sql
62 CREATE USER grafanareader WITH PASSWORD 'password';
63 GRANT USAGE ON SCHEMA schema TO grafanareader;
64 GRANT SELECT ON schema.table TO grafanareader;
65```
66
67Make sure the user does not get any unwanted privileges from the public role.
68
69## Query editor
70
71{{< figure src="/static/img/docs/v53/postgres_query_still.png" class="docs-image--no-shadow" animated-gif="/static/img/docs/v53/postgres_query.gif" >}}
72
73You find the PostgreSQL query editor in the metrics tab in Graph or Singlestat panel's edit mode. You enter edit mode by clicking the
74panel title, then edit.
75
76The query editor has a link named `Generated SQL` that shows up after a query has been executed, while in panel edit mode. Click on it and it will expand and show the raw interpolated SQL string that was executed.
77
78### Select table, time column and metric column (FROM)
79
80When you enter edit mode for the first time or add a new query Grafana will try to prefill the query builder with the first table that has a timestamp column and a numeric column.
81
82In the FROM field, Grafana will suggest tables that are in the `search_path` of the database user. To select a table or view not in your `search_path`
83you can manually enter a fully qualified name (schema.table) like `public.metrics`.
84
85The Time column field refers to the name of the column holding your time values. Selecting a value for the Metric column field is optional. If a value is selected, the Metric column field will be used as the series name.
86
87The metric column suggestions will only contain columns with a text datatype (char,varchar,text).
88If you want to use a column with a different datatype as metric column you may enter the column name with a cast: `ip::text`.
89You may also enter arbitrary SQL expressions in the metric column field that evaluate to a text datatype like
90`hostname || ' ' || container_name`.
91
92### Columns, window, and aggregation functions (SELECT)
93
94In the `SELECT` row you can specify what columns and functions you want to use.
95In the column field you may write arbitrary expressions instead of a column name like `column1 * column2 / column3`.
96
97The available functions in the query editor depend on the PostgreSQL version you selected when configuring the data source.
98If you use aggregate functions you need to group your resultset. The editor will automatically add a `GROUP BY time` if you add an aggregate function.
99
100The editor tries to simplify and unify this part of the query. For example:<br>
101![](/static/img/docs/v53/postgres_select_editor.png)<br>
102
103The above will generate the following PostgreSQL `SELECT` clause:
104
105```sql
106avg(tx_bytes) OVER (ORDER BY "time" ROWS 5 PRECEDING) AS "tx_bytes"
107```
108
109You may add further value columns by clicking the plus button and selecting `Column` from the menu. Multiple value columns will be plotted as separate series in the graph panel.
110
111### Filter data (WHERE)
112
113To add a filter click the plus icon to the right of the `WHERE` condition. You can remove filters by clicking on
114the filter and selecting `Remove`. A filter for the current selected timerange is automatically added to new queries.
115
116### Group by
117
118To group by time or any other columns click the plus icon at the end of the GROUP BY row. The suggestion dropdown will only show text columns of your currently selected table but you may manually enter any column.
119You can remove the group by clicking on the item and then selecting `Remove`.
120
121If you add any grouping, all selected columns need to have an aggregate function applied. The query builder will automatically add aggregate functions to all columns without aggregate functions when you add groupings.
122
123#### Gap filling
124
125Grafana can fill in missing values when you group by time. The time function accepts two arguments. The first argument is the time window that you would like to group by, and the second argument is the value you want Grafana to fill missing items with.
126
127### Text editor mode (RAW)
128
129You can switch to the raw query editor mode by clicking the hamburger icon and selecting `Switch editor mode` or by clicking `Edit SQL` below the query.
130
131> If you use the raw query editor, be sure your query at minimum has `ORDER BY time` and a filter on the returned time range.
132
133## Macros
134
135Macros can be used within a query to simplify syntax and allow for dynamic parts.
136
137| Macro example                                         | Description                                                                                                                                                                                                  |
138| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
139| `$__time(dateColumn)`                                 | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, _UNIX_TIMESTAMP(dateColumn) as time_sec_                                                  |
140| `$__timeEpoch(dateColumn)`                            | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, _UNIX_TIMESTAMP(dateColumn) as time_sec_                                                  |
141| `$__timeFilter(dateColumn)`                           | Will be replaced by a time range filter using the specified column name. For example, _dateColumn BETWEEN FROM_UNIXTIME(1494410783) AND FROM_UNIXTIME(1494410983)_                                           |
142| `$__timeFrom()`                                       | Will be replaced by the start of the currently active time selection. For example, _FROM_UNIXTIME(1494410783)_                                                                                               |
143| `$__timeTo()`                                         | Will be replaced by the end of the currently active time selection. For example, _FROM_UNIXTIME(1494410983)_                                                                                                 |
144| `$__timeGroup(dateColumn,'5m')`                       | Will be replaced by an expression usable in GROUP BY clause. For example, *cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) as signed)*300 as signed),\*                                                           |
145| `$__timeGroup(dateColumn,'5m', 0)`                    | Same as above but with a fill parameter so missing points in that series will be added by grafana and 0 will be used as value.                                                                               |
146| `$__timeGroup(dateColumn,'5m', NULL)`                 | Same as above but NULL will be used as value for missing points.                                                                                                                                             |
147| `$__timeGroup(dateColumn,'5m', previous)`             | Same as above but the previous value in that series will be used as fill value if no value has been seen yet NULL will be used (only available in Grafana 5.3+).                                             |
148| `$__timeGroupAlias(dateColumn,'5m')`                  | Will be replaced identical to $\_\_timeGroup but with an added column alias (only available in Grafana 5.3+).                                                                                                |
149| `$__unixEpochFilter(dateColumn)`                      | Will be replaced by a time range filter using the specified column name with times represented as Unix timestamp. For example, _dateColumn > 1494410783 AND dateColumn < 1494497183_                         |
150| `$__unixEpochFrom()`                                  | Will be replaced by the start of the currently active time selection as Unix timestamp. For example, _1494410783_                                                                                            |
151| `$__unixEpochTo()`                                    | Will be replaced by the end of the currently active time selection as Unix timestamp. For example, _1494497183_                                                                                              |
152| `$__unixEpochNanoFilter(dateColumn)`                  | Will be replaced by a time range filter using the specified column name with times represented as nanosecond timestamp. For example, _dateColumn > 1494410783152415214 AND dateColumn < 1494497183142514872_ |
153| `$__unixEpochNanoFrom()`                              | Will be replaced by the start of the currently active time selection as nanosecond timestamp. For example, _1494410783152415214_                                                                             |
154| `$__unixEpochNanoTo()`                                | Will be replaced by the end of the currently active time selection as nanosecond timestamp. For example, _1494497183142514872_                                                                               |
155| `$__unixEpochGroup(dateColumn,'5m', [fillmode])`      | Same as $\_\_timeGroup but for times stored as Unix timestamp (only available in Grafana 5.3+).                                                                                                              |
156| `$__unixEpochGroupAlias(dateColumn,'5m', [fillmode])` | Same as above but also adds a column alias (only available in Grafana 5.3+).                                                                                                                                 |
157
158We plan to add many more macros. If you have suggestions for what macros you would like to see, please [open an issue](https://github.com/grafana/grafana) in our GitHub repo.
159
160## Table queries
161
162If the `Format as` query option is set to `Table` then you can basically do any type of SQL query. The table panel will automatically show the results of whatever columns and rows your query returns.
163
164Query editor with example query:
165
166![](/static/img/docs/v46/postgres_table_query.png)
167
168The query:
169
170```sql
171SELECT
172  title as "Title",
173  "user".login as "Created By",
174  dashboard.created as "Created On"
175FROM dashboard
176INNER JOIN "user" on "user".id = dashboard.created_by
177WHERE $__timeFilter(dashboard.created)
178```
179
180You can control the name of the Table panel columns by using regular `as ` SQL column selection syntax.
181
182The resulting table panel:
183
184![postgres table](/static/img/docs/v46/postgres_table.png)
185
186## Time series queries
187
188If you set Format as to _Time series_, then the query must have a column named time that returns either a SQL datetime or any numeric datatype representing Unix epoch in seconds. In addition, result sets of time series queries must be sorted by time for panels to properly visualize the result.
189
190A time series query result is returned in a [wide data frame format]({{< relref "../developers/plugins/data-frames.md#wide-format" >}}). Any column except time or of type string transforms into value fields in the data frame query result. Any string column transforms into field labels in the data frame query result.
191
192> For backward compatibility, there's an exception to the above rule for queries that return three columns including a string column named metric. Instead of transforming the metric column into field labels, it becomes the field name, and then the series name is formatted as the value of the metric column. See the example with the metric column below.
193
194You can optionally customize the default series name formatting using instructions in [Standard field options/Display name]({{< relref "../panels/standard-options.md#display-name" >}}).
195
196**Example with `metric` column:**
197
198```sql
199SELECT
200  $__timeGroup("time_date_time",'5m'),
201  min("value_double"),
202  'min' as metric
203FROM test_data
204WHERE $__timeFilter("time_date_time")
205GROUP BY time
206ORDER BY time
207```
208
209Data frame result:
210
211```text
212+---------------------+-----------------+
213| Name: time          | Name: min       |
214| Labels:             | Labels:         |
215| Type: []time.Time   | Type: []float64 |
216+---------------------+-----------------+
217| 2020-01-02 03:05:00 | 3               |
218| 2020-01-02 03:10:00 | 6               |
219+---------------------+-----------------+
220```
221
222**Example using the fill parameter in the $\_\_timeGroup macro to convert null values to be zero instead:**
223
224```sql
225SELECT
226  $__timeGroup("createdAt",'5m',0),
227  sum(value) as value,
228  hostname
229FROM test_data
230WHERE
231  $__timeFilter("createdAt")
232GROUP BY time, hostname
233ORDER BY time
234```
235
236Given the data frame result in the following example and using the graph panel, you will get two series named _value 10.0.1.1_ and _value 10.0.1.2_. To render the series with a name of _10.0.1.1_ and _10.0.1.2_ , use a [Standard field options/Display name]({{< relref "../panels/standard-options.md#display-name" >}}) value of `${__field.labels.hostname}`.
237
238Data frame result:
239
240```text
241+---------------------+---------------------------+---------------------------+
242| Name: time          | Name: value               | Name: value               |
243| Labels:             | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 |
244| Type: []time.Time   | Type: []float64           | Type: []float64           |
245+---------------------+---------------------------+---------------------------+
246| 2020-01-02 03:05:00 | 3                         | 4                         |
247| 2020-01-02 03:10:00 | 6                         | 7                         |
248+---------------------+---------------------------+---------------------------+
249```
250
251**Example with multiple columns:**
252
253```sql
254SELECT
255  $__timeGroup("time_date_time",'5m'),
256  min("value_double") as "min_value",
257  max("value_double") as "max_value"
258FROM test_data
259WHERE $__timeFilter("time_date_time")
260GROUP BY time
261ORDER BY time
262```
263
264Data frame result:
265
266```text
267+---------------------+-----------------+-----------------+
268| Name: time          | Name: min_value | Name: max_value |
269| Labels:             | Labels:         | Labels:         |
270| Type: []time.Time   | Type: []float64 | Type: []float64 |
271+---------------------+-----------------+-----------------+
272| 2020-01-02 03:04:00 | 3               | 4               |
273| 2020-01-02 03:05:00 | 6               | 7               |
274+---------------------+-----------------+-----------------+
275```
276
277## Templating
278
279Instead of hard-coding things like server, application and sensor name in your metric queries you can use variables in their place. Variables are shown as dropdown select boxes at the top of the dashboard. These dropdowns make it easy to change the data being displayed in your dashboard.
280
281Refer to [Templates and variables]({{< relref "../variables/_index.md" >}}) for an introduction to the templating feature and the different types of template variables.
282
283### Query variable
284
285If you add a template variable of the type `Query`, you can write a PostgreSQL query that can
286return things like measurement names, key names or key values that are shown as a dropdown select box.
287
288For example, you can have a variable that contains all values for the `hostname` column in a table if you specify a query like this in the templating variable _Query_ setting.
289
290```sql
291SELECT hostname FROM host
292```
293
294A query can return multiple columns and Grafana will automatically create a list from them. For example, the query below will return a list with values from `hostname` and `hostname2`.
295
296```sql
297SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city
298```
299
300To use time range dependent macros like `$__timeFilter(column)` in your query the refresh mode of the template variable needs to be set to _On Time Range Change_.
301
302```sql
303SELECT event_name FROM event_log WHERE $__timeFilter(time_column)
304```
305
306Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allows you to have a friendly name as text and an id as the value. An example query with `hostname` as the text and `id` as the value:
307
308```sql
309SELECT hostname AS __text, id AS __value FROM host
310```
311
312You can also create nested variables. Using a variable named `region`, you could have
313the hosts variable only show hosts from the current selected region with a query like this (if `region` is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values):
314
315```sql
316SELECT hostname FROM host  WHERE region IN($region)
317```
318
319#### Using `__searchFilter` to filter results in Query Variable
320
321> Available from Grafana 6.5 and above
322
323Using `__searchFilter` in the query field will filter the query result based on what the user types in the dropdown select box.
324When nothing has been entered by the user the default value for `__searchFilter` is `%`.
325
326> Important that you surround the `__searchFilter` expression with quotes as Grafana does not do this for you.
327
328The example below shows how to use `__searchFilter` as part of the query field to enable searching for `hostname` while the user types in the dropdown select box.
329
330Query
331
332```sql
333SELECT hostname FROM my_host  WHERE hostname LIKE '$__searchFilter'
334```
335
336### Using Variables in Queries
337
338From Grafana 4.3.0 to 4.6.0, template variables are always quoted automatically. If your template variables are strings, do not wrap them in quotes in where clauses.
339
340From Grafana 4.7.0, template variable values are only quoted when the template variable is a `multi-value`.
341
342If the variable is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values.
343
344There are two syntaxes:
345
346`$<varname>` Example with a template variable named `hostname`:
347
348```sql
349SELECT
350  atimestamp as time,
351  aint as value
352FROM table
353WHERE $__timeFilter(atimestamp) and hostname in($hostname)
354ORDER BY atimestamp ASC
355```
356
357`[[varname]]` Example with a template variable named `hostname`:
358
359```sql
360SELECT
361  atimestamp as time,
362  aint as value
363FROM table
364WHERE $__timeFilter(atimestamp) and hostname in([[hostname]])
365ORDER BY atimestamp ASC
366```
367
368#### Disabling quoting for multi-value variables
369
370Grafana automatically creates a quoted, comma-separated string for multi-value variables. For example: if `server01` and `server02` are selected then it will be formatted as: `'server01', 'server02'`. To disable quoting, use the csv formatting option for variables:
371
372`${servers:csv}`
373
374Read more about variable formatting options in the [Variables]({{< relref "../variables/_index.md#advanced-formatting-options" >}}) documentation.
375
376## Annotations
377
378[Annotations]({{< relref "../dashboards/annotations.md" >}}) allow you to overlay rich event information on top of graphs. You add annotation queries via the Dashboard menu / Annotations view.
379
380**Example query using time column with epoch values:**
381
382```sql
383SELECT
384  epoch_time as time,
385  metric1 as text,
386  concat_ws(', ', metric1::text, metric2::text) as tags
387FROM
388  public.test_data
389WHERE
390  $__unixEpochFilter(epoch_time)
391```
392
393**Example region query using time and timeend columns with epoch values:**
394
395> Only available in Grafana v6.6+.
396
397```sql
398SELECT
399  epoch_time as time,
400  epoch_time_end as timeend,
401  metric1 as text,
402  concat_ws(', ', metric1::text, metric2::text) as tags
403FROM
404  public.test_data
405WHERE
406  $__unixEpochFilter(epoch_time)
407```
408
409**Example query using time column of native SQL date/time data type:**
410
411```sql
412SELECT
413  native_date_time as time,
414  metric1 as text,
415  concat_ws(', ', metric1::text, metric2::text) as tags
416FROM
417  public.test_data
418WHERE
419  $__timeFilter(native_date_time)
420```
421
422| Name      | Description                                                                                                                       |
423| --------- | --------------------------------------------------------------------------------------------------------------------------------- |
424| `time`    | The name of the date/time field. Could be a column with a native SQL date/time data type or epoch value.                          |
425| `timeend` | Optional name of the end date/time field. Could be a column with a native SQL date/time data type or epoch value. (Grafana v6.6+) |
426| `text`    | Event description field.                                                                                                          |
427| `tags`    | Optional field name to use for event tags as a comma separated string.                                                            |
428
429## Alerting
430
431Time series queries should work in alerting conditions. Table formatted queries are not yet supported in alert rule
432conditions.
433
434## Configure the data source with provisioning
435
436It's now possible to configure data sources using config files with Grafana's provisioning system. You can read more about how it works and all the settings you can set for data sources on the [provisioning docs page]({{< relref "../administration/provisioning/#datasources" >}})
437
438Here are some provisioning examples for this data source.
439
440```yaml
441apiVersion: 1
442
443datasources:
444  - name: Postgres
445    type: postgres
446    url: localhost:5432
447    database: grafana
448    user: grafana
449    secureJsonData:
450      password: 'Password!'
451    jsonData:
452      sslmode: 'disable' # disable/require/verify-ca/verify-full
453      maxOpenConns: 0 # Grafana v5.4+
454      maxIdleConns: 2 # Grafana v5.4+
455      connMaxLifetime: 14400 # Grafana v5.4+
456      postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
457      timescaledb: false
458```
459
460> **Note:** In the above code, the `postgresVersion` value of `10` refers to version PotgreSQL 10 and above.
461
462If you encounter metric request errors or other issues:
463
464- Make sure your data source YAML file parameters exactly match the example. This includes parameter names and use of quotation marks.
465- Make sure the `database` name is not included in the `url`.
466