# Understanding Tag Historian Timestamps in PostgreSQL

**URL:** <https://forum.inductiveautomation.com/t/understanding-tag-historian-timestamps-in-postgresql/63663>\
**Category:** Ignition\
**Created:** [August 31, 2022, 11:42pm UTC](https://forum.inductiveautomation.com/t/understanding-tag-historian-timestamps-in-postgresql/63663 "2022-08-31T23:42:53Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [September 1, 2022, 5:54pm UTC](https://forum.inductiveautomation.com/t/understanding-tag-historian-timestamps-in-postgresql/63663/5 "2022-09-01T17:54:44Z")

</div>

For comparison, here is an example showing how to query and review similar data using the Script Console (Designer) and the [system.tag.queryTagHistory](https://docs.inductiveautomation.com/display/DOC81/system.tag.queryTagHistory) function.

```

myTagPaths = ['[default]Station Power/Station 4/PM-04-04/Energy' ]
myColumnNames = ['PM-04-04']
myEndDateTime = system.date.parse("2022-08-03 00:00:00", "yyyy-MM-dd HH:mm:ss")
myStartDateTime = system.date.addHours(myEndDateTime, -24)

print myEndDateTime
print myStartDateTime

#======================================================================
#======================================================================
def printDataSet(datasetIn):
	# Get Row and Column counts
	nrows = datasetIn.getRowCount()
	ncols = datasetIn.getColumnCount()
	
	# Get max length of row labels
	rowLen = len(max(['row']+[unicode(i) for i in range(nrows)], key=len))
	# Get column names
	colNames = datasetIn.getColumnNames()
	# initialize lists to process columns
	headerList = []
	colData = []
	maxLen = []
	# Process columns
	for i, col in zip(range(ncols), colNames):
		# Get column as list, converting all elemenst to unicode strings
		colIn = ([unicode(element) for element in list(datasetIn.getColumnAsList(i))])
		# Get max lentgh of the column, including the column name
		maxLen = len(max([col]+colIn, key=len))
		# Append data as left justified strings.
		# ljust() will automatically pad to the max string length
		colData.append([element.ljust(maxLen) for element in colIn])
		# Append column name, also using the max string length
		headerList.append(col.ljust(maxLen))
		
	# Concatenate the header string and print it.
	headerString= 'row'.ljust(rowLen) + ' | ' + ' | '.join(headerList)
	print headerString
	# Print a bunch of dashes the same length as the header string
	print'-' * len(headerString)
	
	# Print the rows
	for row in enumerate(zip(*colData)):
		print unicode(row[0]).ljust(rowLen) + ' | ' + ' | '.join(row[1])
		
#======================================================================
#======================================================================
data = system.tag.queryTagHistory(
	  paths=myTagPaths
	, startDate=myStartDateTime
	, endDate=myEndDateTime
#	, returnSize = 0
#	, aggregationMode="LastValue"
#	, returnFormat
	, columnNames=myColumnNames
#	, intervalHours
#	, intervalMinutes
#	, rangeHours
#	, rangeMinutes
#	, aggregationModes
#	, includeBoundingValues=1
#	, validateSCExec
	, noInterpolation=1 # prevent the return of rows that are interpolated
#	, ignoreBadQuality
	, timeout = 10000
#	, intervalSeconds
#	, rangeSeconds
)

printDataSet(data)

```

**Results:**

```
>>> 
Wed Aug 03 00:00:00 UTC 2022
Tue Aug 02 00:00:00 UTC 2022
row | t_stamp | PM-04-04     
--------------------------------------------------
0 | Tue Aug 02 00:06:17 UTC 2022 | 17.8490009308
1 | Tue Aug 02 01:06:23 UTC 2022 | 18.2940006256
2 | Tue Aug 02 02:06:29 UTC 2022 | 18.7299995422
3 | Tue Aug 02 03:06:33 UTC 2022 | 19.1550006866
4 | Tue Aug 02 04:06:37 UTC 2022 | 19.577999115 
5 | Tue Aug 02 05:06:43 UTC 2022 | 19.9960002899
6 | Tue Aug 02 06:06:49 UTC 2022 | 20.4130001068
7 | Tue Aug 02 07:06:55 UTC 2022 | 20.8269996643
8 | Tue Aug 02 08:07:00 UTC 2022 | 21.2390003204
9 | Tue Aug 02 09:07:06 UTC 2022 | 21.6520004272
10 | Tue Aug 02 10:07:13 UTC 2022 | 22.0620002747
...
```

To me this actually seems quite a bit less versatile than querying the database directly but it does have the advantage of transparently handling the history data partitioned across database tables.

There is probably a way, but it is not immediately apparent, how one might use localized time in the query and show localized time in the returned data.

The `printDataSet` function was lifted from a [forum post](https://forum.inductiveautomation.com/t/view-dataset-from-script-console/36704/4) by @JordanCClark, (thank you).

---

_[View the full topic](https://forum.inductiveautomation.com/t/understanding-tag-historian-timestamps-in-postgresql/63663)._
