EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL NOW()
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL NOW()

By Sohel SayyadSohel Sayyad

PostgreSQL NOW()

Introduction to PostgreSQL NOW()

We can get the current date and time by using the PostgreSQL NOW() function. The PostgreSQL NOW() function’s return type is the timestamp with the time zone. Depending upon the current database server’s time zone setting, the PostgreSQL NOW() function returns us the current date and time. We can adjust the result returned by the PostgreSQL NOW() function to other timezones as well. Also, we can get the current date and time without a timezone. This function can be used for giving the default value to the column of the table.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

NOW();

This function’s return value is the current date and time value as per the timezone setting of the current database server.

How does PostgreSQL NOW() Function works?

Here we see how the NOW() function returns the current date and time, which depend upon the timezone setting of the current database server’s timezone.

Syntax:

Now()

Example:

Code:

Select Now();

Output:

postgreSQL NOW()1

Examples of PostgreSQL NOW()

Given below are the examples:

Example #1

Now we will change the timezone to ‘America/Chicago’ and then will use the PostgreSQL NOW() function as follows:

Code:

set timezone = 'America/Chicago';
select now()

Output:

Illustrate the result of the above statement by using the following snapshot.

postgreSQL NOW()2

Example #2

We can get the current date and time without the timezone by using PostgreSQL NOW() function with timestamp defined explicitly as follows:

Code:

SELECT NOW()::timestamp;

Output:

Illustrate the result of the above statement by using the following snapshot.

current date and time without the timezone

Example #3

We can use the PostgreSQL NOW() function with the date and time operators.

a. Like consider the following example where we will try to get the result as 5 hours from now:

Code:

SELECT (NOW() + interval '5 hour') AS extra_five_hour;

Output:

Illustrate the result of the above statement by using the following snapshot.

the result as 5 hours

b. We can add an extra day to the current time as well as follows:

Code:

SELECT (NOW() + interval '2 day') AS this_time_after_two_days;

Output:

Illustrate the result of the above statement by using the following snapshot.

postgreSQL NOW()6

c. Also, we can get the previous time as well by using the minus (-) operator with time as follows:

Code:

SELECT now() - interval '3 hours 15 minutes' AS three_hours_15_min_go;

Output:

Illustrate the result of the above statement by using the following snapshot.

previous time as well by using minus (-) operator

Example #4

We can use the PostgreSQL NOW() function for giving default value to the columns of the table. We will create a table named ‘student’ by using the following CREATE table statement. The column ‘created_date’ will contain the result returned by the function PostgreSQL NOW() function in the student’s table.

Code:

CREATE TABLE student
(
stud_id serial PRIMARY KEY,
stud_name VARCHAR(80) NOT NULL,
created_date TIMESTAMPTZ DEFAULT Now()
);

We will then insert some data in the ‘student’ table by using the INSERT INTO statement, and then we will verify the value of the ‘created_date’ column, which will have the default value of the current date time assigned while insertion.

Code:

INSERT INTO student(stud_name)
VALUES ('Jacob'), ('David');

Illustrate the result of the above statement by using the following snapshot and SQL statement.

Code:

select * from student;

Output:

postgreSQL NOW()9

Now again, we will insert some data in the ‘student’ table by using the INSERT INTO statement, and then we will verify the value of the ‘created_date’ column, which will have the default value of the current date and time assigned while insertion.

Code:

INSERT INTO student(stud_name)
VALUES ('John'), ('Robert')

Illustrate the result of the above statement by using the following snapshot and SQL statement.

Code:

select * from student;

Output:

from student

Here we can see that whenever we insert the data in the table accordingly, the ‘created_date’ column contains the default value of the current date and time assigned during insertion.

Example #5

Now we will look at some of the functions which are related to the NOW() function provided by PostgreSQL.

a. We can use different functions to get the current date and time without timezone as follows:

Code:

LOCALTIME or LOCALTIMESTAMP

SELECT LOCALTIME, LOCALTIMESTAMP;

Output:

Illustrate the result of the above statement by using the following snapshot.

different functions to get the current date and time

b. We can use different functions to get the current date and time with timezone as follows:

Code:

CURRENT_TIME or CURRENT_TIMESTAMP

SELECT CURRENT_TIME, CURRENT_TIMESTAMP;

Output:

Illustrate the result of the above statement by using the following snapshot.

different functions to get the current date and time with timezone

Advantages

Below are the advantages:

  • We can use the NOW() function provided by PostgreSQL to get the current date and time.
  • The PostgreSQL NOW() function operates along with the database server’s timezone, which makes it more useful for getting the local time and date of the database.
  • We can provide the default value to the columns of the table by using the function.
  • We can change the PostgreSQL NOW() function’s result to other timezone’s as well.

Conclusion

Here we saw how to use the PostgreSQL NOW() function and how the PostgreSQL NOW() function works. Also, we have added several examples to understand.

Recommended Articles

This is a guide to PostgreSQL NOW(). Here we discuss the introduction, how does PostgreSQL NOW() function works and examples. You may also have a look at the following articles to learn more –

  1. PostgreSQL Date Functions
  2. PostgreSQL Administration
  3. PostgreSQL COMMIT
  4. PostgreSQL IN Operator
  5. PostgreSQL HAVING | How to Works?
  6. Guide to PostgreSQL RANDOM
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more