To get the current time without date, you use CURRENT_TIME: timetz Or you can use the CURRENT_TIMESTAMP: SELECT CURRENT_TIMESTAMP Code language: SQL (Structured Query Language) ( sql ) To get the current timestamp you use the NOW() function as follows: SELECT NOW() Code language: SQL (Structured Query Language) ( sql ) now To handle timestamp data effectively, PostgreSQL provides some handy functions as follows: Getting the current time Generally, it is a good practice to use the timestamptz datatype to store the timestamp data. The value in the timestamp column does not change, whereas the value in the timestamptz column is adjusted to the new time zone of 'America/New_York'. SET timezone = 'America/New_York' Code language: SQL (Structured Query Language) ( sql ) SELECT The query returns the same timestamp values as the inserted values.įinally, change the timezone of the current session to America/New_York and query data again. Timestamp_demo Code language: SQL (Structured Query Language) ( sql ) ts | tstz VALUES( ' 19:10:25-07', ' 19:10:25-07') Code language: SQL (Structured Query Language) ( sql )Īfter that, query data from the timestamp and timestamptz columns. Then, insert a new row into the timstamp_demo table: INSERT INTO timestamp_demo (ts, tstz) (1 row) Code language: SQL (Structured Query Language) ( sql ) SET timezone = 'America/Los_Angeles' Code language: SQL (Structured Query Language) ( sql )īy the way, you can see the current time zone using the SHOW TIMEZONE command: SHOW TIMEZONE Code language: SQL (Structured Query Language) ( sql ) TimeZone Next, set the time zone of the database server to America/Los_Angeles. ) Code language: SQL (Structured Query Language) ( sql ) Let’s take a look at an example of using the timestamp and timestamptz to have a better understanding of how PostgreSQL handles them.įirst, create a table that consists of both timestamp the timestamptz columns. PostgreSQL does not store any timezone data with the timestamptz value. It’s important to note that timestamptz value is stored as a UTC value. (2 rows) Code language: SQL (Structured Query Language) ( sql ) Typname ~ '^timestamp' Code language: SQL (Structured Query Language) ( sql ) typname | typlen Notice that both timestamp and timestamptz uses 8 bytes for storing the timestamp values as shown in the following query: SELECT
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |