I know that I can the session timezone with
SET TIMEZONE TO 'CET';
But is there a way to get it back? I want something like
SELECT SESSIONTIMEZONE, SYSDATE, SYSDATE::timestamptz, SYSDATE::timestamptz AT TIME ZONE 'UTC', SYSDATE::timestamptz AT TIME ZONE 'CET';
which I would expect to return
UTC| ... 12:23:55.963747 | ... 12:23:55.963747+00 | ... 12:23:55.963747+00 | ... 12:23:55.963747+01
Is there a way to get it? I don't see any functio CURRENT_TIMEZONE or GET_TIMEZONE.
You can use CURRENT_SETTING('timezone') to get it or SHOW TIMEZONE;
select current_setting('timezone'), sysdate, sysdate::timestamptz, sysdate::timestamptz at time zone 'utc', sysdate::timestamptz at time zone 'cet';
UTC 2021-03-19 12:48:09.989592 2021-03-19 12:48:09.989592+00 2021-03-19 12:48:09.989592 2021-03-19 13:48:09.989592
Related
I have a timestamp stored this way in database:
2021-08-14 12:19:58+00
I need to display it in this format
2021-08-14 12:19:58 UTC
Use the to_char function with the TZ format:
SELECT to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS TZ');
to_char
══════════════════════════
2021-09-21 23:06:15 CEST
(1 row)
I am currently working with raw data that have timestamps in GMT and I want to convert them to CST. I am trying to use the cast function to change the timestamp, but it is not working- the times are not affected. Most of what I have read about timezones in postgresql assumes that the default timezone is UTC so I'm not sure if there is a different syntax needed for when the data I'm trying to convert is GMT. Any help is greatly appreciated!
WITH RECURSIVE "child" AS (
SELECT "ConsultantDisplayID",
"JoinDate",
"ParentPersonDisplayID"
FROM "public"."flight_export_consultant"
WHERE "ConsultantDisplayID" = '4019'
UNION
SELECT c."ConsultantDisplayID",
CAST(c."JoinDate" at time zone 'america/chicago' as timestamp) as "JoinDate"
c."ParentPersonDisplayID"
FROM "public"."flight_export_consultant" AS c
JOIN "child" AS cd
ON c."ParentPersonDisplayID" = cd."ConsultantDisplayID"),
"sponsor" AS (
SELECT
"child".*,
c1."ConsultantDisplayID",
Cast(c."JoinDate" at time zone 'america/chicago' as timestamp) as "Sponsor JoinDate"
FROM "public"."flight_export_consultant" AS c1
LEFT JOIN "child"
ON c1."ConsultantDisplayID" = "child"."ParentPersonDisplayID")
SELECT * FROM "sponsor"
As #Mike Organek pointed out a field of type timestamp assumes local time on entry. So first thing you need to establish is where the dates are being entered from and whether they are are actually being entered as GMT. For the moment assuming they are you could do the following:
select 'September 24, 2018, 4:01PM'::timestamp at time zone 'utc' at time zone 'america/chicago';
timezone
---------------------
09/24/2018 11:01:00
-- Or if you want to stick to GMT
select 'September 24, 2018, 4:01PM'::timestamp at time zone 'gmt' at time zone 'america/chicago';
timezone
---------------------
09/24/2018 11:01:00
Basically you are 'anchoring' the timestamp at UTC/GMT and then converting to 'america/chicago'. In other words replicating what a timestamptz field does.
Given that JoinDate is type timestamp, this should be a good workaround for your situation now that we have established that the values in JoinDate of type timestamp represent UTC/GMT timestamps, and your server is not in UTC/GMT. The ideal solution is to use timestamptz columns.
The trick here is to cast JoinDate to text, append a z to it to make it UTC, and then cast it to timestamptz. Once that is done, you can use at time zone 'us/chicago' to do the conversion for you.
WITH RECURSIVE "child" AS (
SELECT "ConsultantDisplayID",
"JoinDate",
"ParentPersonDisplayID"
FROM "public"."flight_export_consultant"
WHERE "ConsultantDisplayID" = '4019'
UNION
SELECT c."ConsultantDisplayID",
"JoinDate",
c."ParentPersonDisplayID"
FROM "public"."flight_export_consultant" AS c
JOIN "child" AS cd
ON c."ParentPersonDisplayID" = cd."ConsultantDisplayID"),
"sponsor" AS (
SELECT
"child".*,
c1."ConsultantDisplayID",
c."JoinDate" as "Sponsor JoinDate"
FROM "public"."flight_export_consultant" AS c1
LEFT JOIN "child"
ON c1."ConsultantDisplayID" = "child"."ParentPersonDisplayID")
SELECT "ConsultantDisplayID",
("JoinDate"::text||'z')::timestamptz at 'america/chicago' as "JoinDate",
"ParentPersonDisplayID",
"ConsultantDisplayID",
("JoinDate"::text||'z')::timestamptz at 'america/chicago' as "Sponsor JoinDate"
FROM "sponsor";
The table schema is like this:
Column | Type | Modifiers
--------+--------------------------+--------------------------------------------------
time | timestamp with time zone | default now()
The time format is like this:
time
------------------------
2016-07-11 18:58:28+00
2016-07-11 18:58:37+00
2016-07-12 00:59:31+00
How to group by date with time truncated?
I would like to see the result as:
date
------------------------
2016-07-11
2016-07-11
2016-07-12
If you want compare or group by dates instead of timestamps you can cast to DATE:
SELECT time::DATE, ... FROM ... GROUP BY time::DATE;
or simpler
SELECT time::DATE, ... FROM ... GROUP BY 1;
Take a look at the current Postgresql documentation for datetime functions: https://www.postgresql.org/docs/current/static/functions-datetime.html
You can use date_trunc, extract, to_char or the simplest way is to cast to date (as I would do):
SELECT time::date; // 2016-07-11 18:58:28+00 -> 2016-07-11
Cheers!
use date_trunc:
select date_trunc('day', time)
I'm having an issue with datetime handling in RPostgreSQL. Specifically it relates to POSIXct objects with a UTC timezone being automatically adjusted to daylight saving during upload to a postgres database. A simple example:
library(RPostgreSQL)
example = data.frame(date=as.POSIXct('2016-08-14 15:50:00',tz='UTC'))
con = dbConnect(dbDriver("PostgreSQL"),
dbname="mydb",
host="localhost",
port="5432",
user="me",
password="password")
dbWriteTable(con,name=c('myschema','mytable'),example,overwrite=T)
example2 = dbReadTable(con,name=c('myschema','mytable'))
dbDisconnect(con)
example2 # 2016-08-14 14:50:00
In this case the time is exported as 15:50 but read back in as 14:50, suggesting that British Summer Time daylight saving has been applied. I've tried adjusting my system settings to UTC, setting the timezone in R to UTC using Sys.setenv(TZ='UTC') and setting the timezone in Postgres to UTC using SET timezone TO 'UTC', all to no avail.
Does anybody know where in the process the conversion is likely to be happening and where dbWriteTable is taking its timezone from? Are there any suggestions on other settings that might need adjusting?
I also get strange issues with RPostgreSQL (with UTC somehow being UTC -4:00). But things seem fine using RPostgres.
Note that the time zone displayed in R is in local time. If you go into PostgreSQL (say, psql) after running the R code and SET TIME ZONE 'GMT';, you see that the 2016-08-14 16:50:00 displayed in R is actually stored in the database as 2016-08-14 15:50:00 UTC. In other words, 2016-08-14 16:50:00 displayed in R is correct for rubbish_alt in my example.
crsp=# SET TIME ZONE 'GMT';
SET
crsp=# SELECT * FROM rubbish;
row.names | date
-----------+------------------------
1 | 2016-08-14 19:50:00+00
(1 row)
crsp=# SELECT * FROM rubbish_alt;
date
------------------------
2016-08-14 15:50:00+00
(1 row)
crsp=# \d rubbish
Table "public.rubbish"
Column | Type | Modifiers
-----------+--------------------------+-----------
row.names | text |
date | timestamp with time zone |
crsp=# \d rubbish_alt
Table "public.rubbish_alt"
Column | Type | Modifiers
--------+--------------------------+-----------
date | timestamp with time zone |
R code (note using Sys.setenv(PGHOST="myhost", PGDATABASE="mydb"), etc, elsewhere makes this reprex()-generated code run for anyone):
Sys.setenv(TZ='Europe/London')
# With RPostgreSQL ----
library(RPostgreSQL)
#> Loading required package: DBI
example <- data.frame(date=as.POSIXct('2016-08-14 15:50:00', tz='UTC'))
con = dbConnect(PostgreSQL())
dbWriteTable(con, 'rubbish', example, overwrite=TRUE)
#> [1] TRUE
example2 <- dbReadTable(con, name="rubbish")
dbDisconnect(con)
#> [1] TRUE
example2
#> date
#> 1 2016-08-14 20:50:00
# With RPostgres ----
library(RPostgres)
example <- data.frame(date=as.POSIXct('2016-08-14 15:50:00', tz='UTC'))
con = dbConnect(Postgres())
dbWriteTable(con, 'rubbish_alt', example, overwrite=TRUE)
example2 <- dbReadTable(con, name="rubbish_alt")
dbDisconnect(con)
example2
#> date
#> 1 2016-08-14 16:50:00
example2$date[1]
#> [1] "2016-08-14 16:50:00 BST"
So in my database i have a table with startimestamp that is "Timestamp with time zone" type
but what i want to display the timestamp without the time zone so i thought this would work
Select to_char("StartTimestamp",'YYYY/MM/DD HH24:MM:SS')from "Samples" where "ID" = 20
and a i get
"2013/08/02 14:08:04"
but the when i do it without the to_char and just call the timestamp for the same id like this
select "StartTimestamp" from "Samples" where "ID"=20
i get this which is the correct one
"2013-08-02 14:31:04-07"
I'm i missing something the to_char statement? Thanks
Change MM to MI in the minutes place
select
to_char(now(),'YYYY/MM/DD HH24:MM:SS'),
to_char(now(),'YYYY/MM/DD HH24:MI:SS');
to_char | to_char
---------------------+---------------------
2013/08/21 15:08:00 | 2013/08/21 15:51:00