How to convert a string to timestamp in a desired timezone

First find your default timezone. by using following query.

Select current_setting('timezone');

In my case Asia/Karachi

k, now just try following query.

Select Cast('1990-01-25' as Date) at time zone '<Your Default Timezone>' at time zone 'utc';

In my case.

Select Cast('1990-01-25' as Date) at time zone 'Asia/Karachi' at time zone 'utc';

You could run set time zone UTC; before your query:

set time zone UTC;
SELECT to_timestamp(field1, 'YYYY-MM-DD hh24:mi:ss')::timestamp without time zone at time zone 'Etc/UTC';

Does this solve your issue ?


My question is different than OP's question. My string already knows its timezone. I just want to convert to the timestamptz datatype.

In other words: "how to convert a string in a desired timezone, to a timestamp". I could use the approach described here and here; casting with ::timestamptz

SELECT '2016-01-01 00:00+10'::timestamptz;

Tags:

Postgresql