How to Use PostgreSQL Date Functions
Introduction
If you’re using PostgreSQL to store and query your data, there will likely be times when you need to obtain the current date or the current time for your applications. You may even want to calculate a student or employee’s age. Fortunately, PostgreSQL offers a number of useful date and time functions to help you accomplish all of these tasks. In this article, we’ll take a closer look at PostgreSQL date functions and show you a few different ways you can use them.
Prerequisites
Before we get any deeper into a discussion of how to use PostgreSQL date functions, let’s take a moment to look at a few prerequisites that are required for this tutorial:
- First, you’ll need to ensure that PostgreSQL server is properly installed and configured, and that it’s running in the background.
If you’re using Windows or Linux, you can download PostgreSQL here.
- To start the PostgreSQL server on a Linux machine, use the following command:
1 | sudo service postgresql start |
- You can verify that the service is running using this command:
1 | service postgresql status |
You should see output that looks like the following:
1 2 3 4 5 6 7 8 9 | â— postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese Active: active (exited) since Thu 2019-08-01 14:51:20 PST; 36min ago Process: 1230 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 1230 (code=exited, status=0/SUCCESS) Aug 01 14:51:20 user-UX330UAK systemd[1]: Starting PostgreSQL RDBMS... Aug 01 14:51:20 user-UX330UAK systemd[1]: Started PostgreSQL RDBMS. lines 1-8/8 (END) |
- If you’re using a Windows machine, the instructions for starting, stopping and restarting PostgreSQL server are shown below:
- Open the Control Panel
- Open Administrative Tools
- Open Services
- Within Services, locate the PostgreSQL Server service
- Finally, stop, start or restart the service
PostgreSQL AGE Function
In many applications, there’s a need to calculate age. You may need to determine employee, student or client ages, or you may need to calculate years of tenure in an organization. Regardless of the particular circumstances, it’s easy to accomplish these tasks with the help of the PostgreSQL age()
function.
We use the following syntax for the PostgreSQL age()
function:
1 | age(TIMESTAMP, TIMESTAMP); |
The function performs a subtraction using the values of the two arguments; the result is the difference between the two. The result is expressed in years, months and days, instead of days only.
Let’s look at an example of this function in actions:
1 2 3 4 5 | postgres=# SELECT age('2018-07-15','2011-03-24'); age ------------------------ 7 years 3 mons 22 days (1 ROW) |
There’s a variation on the syntax for the age()
function that allows us to use the current date as the first timestamp
argument.
For instance, let’s say we’d like to calculate the years of tenure of a certain employee. To do this, we can use the following syntax:
1 2 3 4 5 | postgres=# SELECT CURRENT_DATE, AGE(TIMESTAMP '2009-04-07'); CURRENT_DATE | age --------------+------------------------- 2019-09-04 | 10 years 4 mons 27 days (1 ROW) |
PostgreSQL CURRENT_DATE Function
The CURRENT_DATE
function is one of the simplest of the PostgreSQL date functions since it doesn’t require any arguments when using it. The syntax is:
1 | CURRENT_DATE |
This function will return a DATE value that represents the present date. We can see how this works in the example below:
1 2 3 4 5 | postgres=# SELECT CURRENT_DATE; CURRENT_DATE -------------- 2019-09-04 (1 ROW) |
In the example shown above, we see that the output gives us a DATE
value: “2019-09-04”.
PostgreSQL CURRENT_TIME Function
Another important PostgreSQL date function is CURRENT_TIME
. The CURRENT_TIME
function returns the present time within a particular time zone.
The syntax of this function is shown below.
1 | CURRENT_TIME(PRECISION) |
We can see that this function takes an argument called precision
. This optional argument will cause the result’s seconds field to be rounded to that number of fractional digits.
Let’s take a look at an example that doesn’t use the precision
argument:
1 2 3 4 5 | postgres=# SELECT CURRENT_TIME; CURRENT_TIME -------------------- 12:50:16.855779+08 (1 ROW) |
Note that the example shown above includes the result with no rounding.
The next example will show how to use the CURRENT_TIME
function with precision set to 3:
1 2 3 4 5 | postgres=# SELECT CURRENT_TIME(3); CURRENT_TIME ----------------- 12:52:36.368+08 (1 ROW) |
PostgreSQL CURRENT_TIMESTAMP() Function
This PostgreSQL function returns the present date and time along with the time zone where the transaction was initiated.
The syntax of this function is shown below:
1 | CURRENT_TIMESTAMP(PRECISION) |
Note that this function also accepts a precision
argument. If you omit this argument, the function will return a TIMESTAMP
with a time zone where the value is not rounded at all.
Let’s look at an example that shows how to use the function to retrieve the current date and time:
1 2 3 4 5 | postgres=# SELECT CURRENT_TIMESTAMP; CURRENT_TIMESTAMP ------------------------------- 2019-09-04 13:06:00.224465+08 (1 ROW) |
As expected, the function returns the current date and time, and the returned value is not rounded at all since we did not include a precision
argument.
Conclusion
Being able to retrieve the current date and time and calculating values such as age are essential skills for many applications. The PostgreSQL date functions make these tasks simple to accomplish. In this article, we looked at several PostgreSQL date functions and provided some real-life examples of their use. With the information provided in this tutorial, you’ll be ready to make use of these functions in your own applications. Jump to top
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started