{"id":749,"date":"2024-12-08T11:47:19","date_gmt":"2024-12-08T04:47:19","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=749"},"modified":"2025-01-02T13:55:06","modified_gmt":"2025-01-02T06:55:06","slug":"postgresql-time","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/","title":{"rendered":"PostgreSQL TIME Data Type"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the PostgreSQL <code>TIME<\/code> data type and how to use it to store time data in databases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-started-with-the-postgresql-time-data-type'>Getting Started with the PostgreSQL TIME data type <a href=\"#getting-started-with-the-postgresql-time-data-type\" class=\"anchor\" id=\"getting-started-with-the-postgresql-time-data-type\" title=\"Anchor for Getting Started with the PostgreSQL TIME data type\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, you use the <code>TIME<\/code> data type to store time data (without <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-date\/\">date<\/a>) in tables.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for defining a <code>TIME<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> (\n    <span class=\"hljs-built_in\">column_name<\/span> <span class=\"hljs-type\">TIME<\/span>,\n    ...\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>A time column can store a time value from <code>00:00:00<\/code> to <code>24:00:00<\/code>. PostgreSQL uses 8-byte to store a time value.<\/p>\n\n\n\n<p>To set a default value for a <code>TIME<\/code> column, you use the <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-default-constraint\/\">DEFAULT<\/a><\/code> constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span>(\n    <span class=\"hljs-built_in\">column_name<\/span> <span class=\"hljs-type\">TIME<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> default_time,\n    ...\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to set the current time as the default value, you can use the <code>CURRENT_TIME<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span>(\n    <span class=\"hljs-built_in\">column_name<\/span> <span class=\"hljs-type\">TIME<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-built_in\">CURRENT_TIME<\/span>,\n    ...\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='time-input'>Time Input <a href=\"#time-input\" class=\"anchor\" id=\"time-input\" title=\"Anchor for Time Input\">#<\/a><\/h3>\n\n\n\n<p>PostgreSQL accepts various time formats, giving you flexibility. However, it is recommended to use ISO 8601, which is <code>HH:MM:SS<\/code> for time input, for example, <code>09:30:45<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='time-output'>Time Output <a href=\"#time-output\" class=\"anchor\" id=\"time-output\" title=\"Anchor for Time Output\">#<\/a><\/h3>\n\n\n\n<p>PostgreSQL allows you to set various time output formats like the time input formats. The default is the ISO 8601 format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='time-with-precisions'>Time with precisions <a href=\"#time-with-precisions\" class=\"anchor\" id=\"time-with-precisions\" title=\"Anchor for Time with precisions\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL allows you to store time with fractional seconds precision, up to 6 digits.<\/p>\n\n\n\n<p>The following statement defines a <code>TIME<\/code> column with &nbsp;fractional seconds precision:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> (\n    <span class=\"hljs-built_in\">column_name<\/span> <span class=\"hljs-type\">TIME<\/span>(<span class=\"hljs-type\">precision<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The valid value of precision is 1 to 6.<\/p>\n\n\n\n<p>To use a time value with fractional seconds precision, you use the following format:<\/p>\n\n\n\n<p><code>HH:MI:SS.pppppp<\/code><\/p>\n\n\n\n<p>In this syntax, <code>p<\/code> determines the precision. For example,<code> 09:30:45.123456<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='getting-the-current-time'>Getting the current time <a href=\"#getting-the-current-time\" class=\"anchor\" id=\"getting-the-current-time\" title=\"Anchor for Getting the current time\">#<\/a><\/h2>\n\n\n\n<p>To get the current time, you use the <code>curent_time<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-built_in\">current_time<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">    <span class=\"hljs-selector-tag\">current_time<\/span>\n<span class=\"hljs-selector-tag\">--------------------<\/span>\n 11<span class=\"hljs-selector-pseudo\">:42<\/span><span class=\"hljs-selector-pseudo\">:54.539691-07<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>current_time<\/code> returns the time with the timezone where the PostgreSQL is running and fractional second precision.<\/p>\n\n\n\n<p>To remove the fractional second precision, you pass the precision as zero to the <code>current_time<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-built_in\">current_time<\/span>(<span class=\"hljs-number\">0<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"> <span class=\"hljs-selector-tag\">current_time<\/span>\n<span class=\"hljs-selector-tag\">--------------<\/span>\n 11<span class=\"hljs-selector-pseudo\">:43<\/span><span class=\"hljs-selector-pseudo\">:07-07<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get the current local time, you use the <code>localtime<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-built_in\">localtime<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">    <span class=\"hljs-selector-tag\">localtime<\/span>\n<span class=\"hljs-selector-tag\">-----------------<\/span>\n 11<span class=\"hljs-selector-pseudo\">:43<\/span><span class=\"hljs-selector-pseudo\">:18.576448<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And without fractional seconds:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-built_in\">localtime<\/span>(<span class=\"hljs-number\">0<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"> <span class=\"hljs-selector-tag\">localtime<\/span>\n<span class=\"hljs-selector-tag\">-----------<\/span>\n 11<span class=\"hljs-selector-pseudo\">:43<\/span><span class=\"hljs-selector-pseudo\">:32<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-time-data-type-example'>PostgreSQL TIME data type example <a href=\"#postgresql-time-data-type-example\" class=\"anchor\" id=\"postgresql-time-data-type-example\" title=\"Anchor for PostgreSQL TIME data type example\">#<\/a><\/h2>\n\n\n\n<p>The following example <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">creates a table<\/a> called <code>schedules<\/code> that includes a <code>TIME<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> schedules (\n  id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  employee <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  start_at <span class=\"hljs-type\">TIME<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  end_at <span class=\"hljs-type\">TIME<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The schedules table has two <code>TIME<\/code> columns, start and end, that represent the starting and ending times of the schedule.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='inserting-time-values'>Inserting time values <a href=\"#inserting-time-values\" class=\"anchor\" id=\"inserting-time-values\" title=\"Anchor for Inserting time values\">#<\/a><\/h2>\n\n\n\n<p>The following example inserts a new row into the schedules table with an input time value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  schedules (employee, start_at, end_at)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'John Doe'<\/span>, <span class=\"hljs-string\">'08:00:00'<\/span>, <span class=\"hljs-string\">'12:00:00'<\/span>),\n  (<span class=\"hljs-string\">'John Doe'<\/span>, <span class=\"hljs-string\">'13:00:00'<\/span>, <span class=\"hljs-string\">'17:00:00'<\/span>) \n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=time&amp;q=SU5TRVJUIElOVE8gc2NoZWR1bGVzIChlbXBsb3llZSwgc3RhcnRfYXQsIGVuZF9hdCkgVkFMVUVTICgnSm9obiBEb2UnLCAnMDg6MDA6MDAnLCAnMTI6MDA6MDAnKSwgKCdKb2huIERvZScsICcxMzowMDowMCcsICcxNzowMDowMCcpIFJFVFVSTklORyAqOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> id | employee | start_at |  end_at\n----+----------+----------+----------\n  1 | John Doe | 08:00:00 | 12:00:00\n  2 | John Doe | 13:00:00 | 17:00:00<\/code><\/span><\/pre>\n\n\n<p>In this example, we explicitly use time values in ISO 8601 format <code>'hh:mm:ss'<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='extracting-hour-minute-and-second-from-a-time-value'>Extracting hour, minute, and second from a time value <a href=\"#extracting-hour-minute-and-second-from-a-time-value\" class=\"anchor\" id=\"extracting-hour-minute-and-second-from-a-time-value\" title=\"Anchor for Extracting hour, minute, and second from a time value\">#<\/a><\/h2>\n\n\n\n<p>The <code>extract()<\/code> function extracts a field that can be an hour, minute, and second from a time value.<\/p>\n\n\n\n<p>For example, the following statement uses the <code>extract()<\/code> function to extract hour, minute, and second from the <code>start_at<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee,\n  start_at,\n  extract(<span class=\"hljs-type\">hour<\/span>  <span class=\"hljs-keyword\">from<\/span> start_at) h,\n  extract(<span class=\"hljs-type\">minute<\/span> <span class=\"hljs-keyword\">from<\/span> start_at) m,\n  extract(<span class=\"hljs-type\">second<\/span> <span class=\"hljs-keyword\">from<\/span> start_at) s\n<span class=\"hljs-keyword\">FROM<\/span>\n  schedules;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=time&amp;q=U0VMRUNUIGVtcGxveWVlLCBzdGFydF9hdCwgZXh0cmFjdChob3VyIGZyb20gc3RhcnRfYXQpIGgsIGV4dHJhY3QobWludXRlIGZyb20gc3RhcnRfYXQpIG0sIGV4dHJhY3Qoc2Vjb25kIGZyb20gc3RhcnRfYXQpIHMgRlJPTSBzY2hlZHVsZXM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> employee | start_at | h  | m |    s\n----------+----------+----+---+----------\n John Doe | 08:00:00 |  8 | 0 | 0.000000\n John Doe | 13:00:00 | 13 | 0 | 0.000000<\/code><\/span><\/pre>\n\n\n<p>The second includes the fraction part. To remove it, you can <em>cast <\/em>the seconds to integers using the cast operator <code>::<\/code><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee,\n  start_at,\n  extract(<span class=\"hljs-type\">hour<\/span>  <span class=\"hljs-keyword\">from<\/span> start_at) h,\n  extract(<span class=\"hljs-type\">minute<\/span> <span class=\"hljs-keyword\">from<\/span> start_at) m,\n  extract(<span class=\"hljs-type\">second<\/span> <span class=\"hljs-keyword\">from<\/span> start_at)::<span class=\"hljs-type\">INT<\/span> s\n<span class=\"hljs-keyword\">FROM<\/span>\n  schedules;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=time&amp;q=U0VMRUNUIGVtcGxveWVlLCBzdGFydF9hdCwgZXh0cmFjdChob3VyIGZyb20gc3RhcnRfYXQpIGgsIGV4dHJhY3QobWludXRlIGZyb20gc3RhcnRfYXQpIG0sIGV4dHJhY3Qoc2Vjb25kIGZyb20gc3RhcnRfYXQpOjpJTlQgcyBGUk9NIHNjaGVkdWxlczs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> employee | start_at | h  | m | s\n<span class=\"hljs-comment\">----------+----------+----+---+---<\/span>\n John Doe | <span class=\"hljs-number\">08<\/span>:<span class=\"hljs-number\">00<\/span>:<span class=\"hljs-number\">00<\/span> |  <span class=\"hljs-number\">8<\/span> | <span class=\"hljs-number\">0<\/span> | <span class=\"hljs-number\">0<\/span>\n John Doe | <span class=\"hljs-number\">13<\/span>:<span class=\"hljs-number\">00<\/span>:<span class=\"hljs-number\">00<\/span> | <span class=\"hljs-number\">13<\/span> | <span class=\"hljs-number\">0<\/span> | <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use PostgreSQL <code>TIME<\/code> data type to store time values in a table.<\/li>\n\n\n\n<li>Use the <code>extract()<\/code> function to extract hour, minute, and second from a time value.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=time\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"749\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL TIME Data Type\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"749\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL TIME Data Type\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":44,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-749","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL TIME Data Type<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL TIME Data Type\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-02T06:55:06+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-time\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-time\\\/\",\"name\":\"PostgreSQL TIME Data Type\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-12-08T04:47:19+00:00\",\"dateModified\":\"2025-01-02T06:55:06+00:00\",\"description\":\"In this tutorial, you'll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-time\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-time\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-time\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL TIME Data Type\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL TIME Data Type","description":"In this tutorial, you'll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL TIME Data Type","og_description":"In this tutorial, you'll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-02T06:55:06+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/","name":"PostgreSQL TIME Data Type","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-12-08T04:47:19+00:00","dateModified":"2025-01-02T06:55:06+00:00","description":"In this tutorial, you'll learn about the PostgreSQL TIME data type and how to use it to store time data in databases.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL TIME Data Type"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=749"}],"version-history":[{"count":5,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/749\/revisions"}],"predecessor-version":[{"id":1361,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/749\/revisions\/1361"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}