How to insert time into redshift - postgresql

Redshift only support TIMESTAMP format, I want to dump some data that is originally stored in Postgres in time without time zone format. My data looks like this: 15:30:00
When I COPY my data into Redshift, it says Invalid timestamp format or value [YYYY-MM-DD HH24:MI:SS], error code 1206.
My current workaround is to create that column in Redshift as CHAR(8)
Thanks

There's no other solution that to use the char(8).

Redshift does not support timezones.
Workaround would be to parse data to UTC and store time difference in separate column. You will be able to calculate local time by adding/substracting hours to the UTC data.

Related

Store source timezone in Postgresql database

In the datawarehouse we need to capture date and time value with timezone as received from source application DB. But Postgresql is converting those values into native timezone every time. Is there a way to maintain source provided timestamp with timezone value in Postgresql database as is?
E.g. from source DB if we get the timezone value as "01/20/2010 11:15:33.000000 -06:00" , want to store this value as it is in warehouse which is at different timezone. Is there any way to do that in Postgresql?
There is no native way to store the source time zone, all timestamps with time zone are stored as UTC. You can extract the time zone and store it in a separate column, and then use a view to provide the data at the time zone you want.

What is the best way to store formatted timestamp in Postgresql

What is the best way to store a timestamp value in Postgresql in a specific format.
For example I would like to store a TIMESTAMP '2020-07-09 17:29:30.873513Z' down to the minute and ignore seconds value.
I can drop the seconds by using date_trunc('minute', TIMESTAMP '2020-07-09 17:29:30.873513Z') Is there anyway for me to specify this format in the column itself when I create a table?
Don't store formatted timestamps in the database, use timestamp with time zone or timestamp without time zone. You would lose powerful datetime arithmetic, value checking and waste storage space.
To have the values truncated to minute precision, use a BEFORE INSERT trigger that uses date_trunc on the value.
If you want to ascertain that only such values are stored, add a check constraint.
I would like to recommend not to drop seconds or anything from the stored data. Because it will create issues while you process the data later. And if you have to eliminate anything, you may eliminate it while retrieving the data.
Use the following code while creation of table
col_name timestamp without time zone DEFAULT timezone('gmt'::text, now())
This will give you a result as shown in the following image:
Good Luck.

Redshift varchar column time convert to 12 hours format

I have a varchar column in redshift table where the time is stored in 24 hours format, e.g, 17:00, I want to query the table and convert the format to 12 hours format showing AM or PM in time. When I test like to_char('17:00'::time,'HH12:MI AM') it works fine but when I put column name in place of hardcoded value querying the table,
SELECT to_char(prepoll_start::time,'HH12:MI AM')
FROM votecast.poll_hours AS ph
WHERE ph.prepoll_start is not null
and state = 'AL'
AND tab_elec_type = 'primary'
It won't work, gives an error
Invalid operation: Specified types or functions (one per INFO message) not supported on Redshift tables.;
Postgres version is 8.0.2
Please let me know what am doing wrong :(
First I had to create a timestamp value out of the time available. Then fetch the time in a 12 hour format.
select to_char( to_timestamp('1900-01-01 '||prepoll_start||':00' ,'YYYY/MM/DD HH:MI:SS') , 'HH12:MI AM')
from votecast.poll_hours;
Amazon Redshift does not support a TIME data type.
See: Datetime Types - Amazon Redshift
However, you are correct that it seems to support TIME for non-table related operations.
I tried playing around with string manipulation but was unable to get beyond the error you experienced. I think it is because TIME is recognized on the leader node, but fails to run on the compute nodes. (This is similar to the behaviour of time_series().)
Thus, you won't be able to use the TIME data type for anything that relies on table data.

Postgres prevent timestamp with timezone conversion

I have a table that I am using to store iso dates with timezones. I realize that dates should "always" be stored as utc but I have an exception to that rule. The timestamps aren't in any way related to the server they are running on. I want to be able to store an iso date like this:
2016-03-06T01:15:52-06:00
And regardless of the time zone of the server or anything else I want the timestamp returned as:
2016-03-06T01:15:52-06:00
Currently if I insert an iso date it automatically converts it to whatever the server timezone is. My above date gets converted to:
2016-03-06 07:15:52+00 (server is utc)
The only thing I can think of is storing the timezone offset in a separate column, storing my date as utc and then converting using the offset column, horribly messy. Surely there is a way to store my date in one column and get it out the way it was originally created?
Your proposed solution is correct. Or more precisely, it is one of several correct implementations. Any of the following would work:
Store the UTC timestamp in one field, store the offset in another.
Store the local timestamp in one field, store the offset in another.
Store the local date in one field, and store a time with time zone in another. (though time with time zone is generally discouraged...)
Store the UTC timestamps in one field and the local timestamp in another.
The easiest by far is the first one, which you already proposed.
I'd avoid against storing timestamps in text fields, as they tend not to be very efficiently searchable.
Also note - if you're coming from a SQL Server background, you might recall its datetimeoffset type, which stores the local datetime and offset in the field, and uses the UTC equivalent during indexing. It's common to think that Postgres and MySQL's timestamp with time zone would have the same behavior, but they don't. They simply use the session time zone to convert to/from UTC. SQL Server has no concept of a session time zone, and thus the discrepancy.
Be sure to read this part of the Postgres docs.

Format of date / time values in database tables

I am reading a csv file with date fields of formatted mm/dd/yyyy. I expected the same kind of format from a Postgres table after the import, but I see yyyy-mm-dd hh:mm:ss.
The date fields in my table are defined as timestamp without time zone data type.
How do I maintain the same format of data? I am using PostgreSQL 9.3.
Postgresql only stores the value, it doesn't store formatting (which would waste space).
You can use the to_char function in your query if you like to get the output formatted in a special way. Details are in the manual.