Hi,
New to the forum and PostgreSQL.
I have used Grafana in the past with SQLite and the same data, which worked correctly in V9.3.2 and created a chart with time & reading to display a graph.
Now I am using PostgreSQL, but cannot get it to work, looked all over internet but can’t get it to work, I am a newbie at most of this, so any help greatly appreciated, thanks … Gaz
What Grafana version and what operating system are you using?
10.2.2
What are you trying to achieve?
A time series chart of a ISO timestamp & data readings
How are you trying to achieve it?
SELECT
device_data → ‘meter_reading’ AS meter_reading,
device_data → ‘iso’ AS time
FROM energy
WHERE device = ‘IP22’
What happened?
Status: 500. Message: converting time columns failed: failed to convert time column: column type “*string” is not convertible to time.Time
The data in my PostgreSQL database
{
"cost": 0.6,
"iso": "2023-12-05T09:12:38.046Z",
"location": "Garage - Electicity",
"meter_reading": 1.358,
"timestamp": 1701767558,
"total_cost": 0
}
Grafana needs timeseries = that time dimension is important. It looks like your iso column is a string type, which contains datetime. But you need timestamp type, so try to use TO_TIMESTAMP to convert string type to timestamp type or you have already timestamp column which looks like a timestamp type, so you can use it.
Be familiar with PostgreSQL data types: timestamp vs string.
I changed a few things as you mentioned now using timestamp from database and in grafana the AS time has to be changed to AS timestamp then I get data.
BUT I cannot get a graph from it only a Table ?
When I try the graph option I get Data is missing a number field
At least it’s good progress than you for your help, any suggestion what I am now doing wrong ?
Column in table view have icon of types which was detected by Grafana:
But you can already CAST type on SQL level, e.g. SELECT column::INTEGER (so you don’t need transformation to change data type in Grafana).
Typical good writen query has also aggregation, e.g.
SELECT
$__timeGroupAlias(time_column, $__interval),
column_a::text AS "metric",
AVG(column_b) AS "value"
FROM tables
WHERE $__timeFilter(time_column)
GROUP BY 1,2
ORDER BY 1
Check Grafana doc to know more about macros, e.g. $__timeGroupAlias in my example.
Just a follow up … I did get this working, I improved my query.
I pieced together lots of small clues I found on the internet and came up with this that works ! Do I understand it … not at the moment ?
Any comments on the query welcome
Thanks for all your help.
My latest query
SELECT (energy.device_data->>'meter_reading')::float AS "Meter reading",
(energy.device_data->>'timestamp')::int AS Time
FROM energy WHERE device = 'IP22'
ORDER BY Time ASC ;