In PostgreSQL, you can convert a timestamp (without time zone) to a timestamptz (timestamp with time zone) using the AT TIME ZONE syntax. The AT TIME ZONE construct is used to interpret a timestamp without time zone as a timestamp with time zone.
Here's how you can do it:
Basic Conversion
Assuming you have a timestamp column and you want to convert it to timestamptz by interpreting it in a specific time zone:
SELECT timestamp_column AT TIME ZONE 'UTC' AS timestamptz_columnFROM your_table;
This example interprets the timestamp_column as being in UTC time zone.
Example Usage
Let's say you have the following table:
CREATE TABLE example_table ( id SERIAL PRIMARY KEY, timestamp_column TIMESTAMP);INSERT INTO example_table (timestamp_column)VALUES ('2024-07-10 12:00:00'), ('2024-07-10 15:30:00');To convert the timestamp_column to timestamptz, interpreting it in UTC:
SELECT timestamp_column, timestamp_column AT TIME ZONE 'UTC' AS timestamptz_columnFROM example_table;
Adjusting to Your Local Time Zone
If you want to convert the timestamp to timestamptz in your local time zone, you need to specify your local time zone:
SELECT timestamp_column, timestamp_column AT TIME ZONE 'America/New_York' AS timestamptz_columnFROM example_table;
Changing the Data Type of a Column
If you want to permanently change the data type of a column from timestamp to timestamptz, you can use the ALTER TABLE statement with the USING clause:
ALTER TABLE example_tableALTER COLUMN timestamp_columnSET DATA TYPE TIMESTAMPTZUSING timestamp_column AT TIME ZONE 'UTC';
This will convert all existing timestamp values to timestamptz using the specified time zone.
Detailed Example
Here's a complete example, including table creation, insertion, conversion, and alteration of the column data type:
-- Create tableCREATE TABLE example_table ( id SERIAL PRIMARY KEY, timestamp_column TIMESTAMP);-- Insert sample dataINSERT INTO example_table (timestamp_column)VALUES ('2024-07-10 12:00:00'), ('2024-07-10 15:30:00');-- Select with conversion to timestamptzSELECT timestamp_column, timestamp_column AT TIME ZONE 'UTC' AS timestamptz_columnFROM example_table;-- Alter the column to permanently change its type to timestamptzALTER TABLE example_tableALTER COLUMN timestamp_columnSET DATA TYPE TIMESTAMPTZUSING timestamp_column AT TIME ZONE 'UTC';-- Verify the changeSELECT * FROM example_table;
This example demonstrates how to handle the conversion both temporarily (for queries) and permanently (for altering table structure). Adjust the time zone as needed to fit your requirements.