Skip to main content
0 reactions

How to Write a Date in PostgreSQL

Featured card for How to Write a Date in PostgreSQL, with a terminal window listing DATE ‘2026-11-27’, ‘2026-11-27’::date, 03/04/2026 read as 4 March and 2026-02-30 refused.
How to write a date in PostgreSQL.

Write a date in PostgreSQL as text in year-month-day order, 'YYYY-MM-DD', and mark it as a date: DATE '2026-11-27'. Year first is the safe order, because every PostgreSQL server reads it the same way.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
DATE 'YYYY-MM-DD'
'YYYY-MM-DD'::date
  • YYYY-MM-DD: the year, the month and the day, with hyphens, such as 2026-11-27.
  • DATE '…': the text is a date.
  • '…'::date: the same, written as a cast.

See Date/Time Input (opens in a new tab) in the PostgreSQL documentation, which recommends this ISO 8601 format.

The Example

The same day, 27 November 2026, written in different ways.

Step 1: Connect

Open Terminal and connect to your PostgreSQL server with psql as the postgres user:

TerminalBash
psql -U postgres

Type the user’s password when psql asks for it. psql connects you to the postgres database, and its prompt is postgres=#.

Step 2: Write a Date

postgres=#SQL
SELECT DATE '2026-11-27';
OutputPlain text
    date
------------
 2026-11-27
(1 row)

Step 3: The Same Date as a Cast

postgres=#SQL
SELECT '2026-11-27'::date;
OutputPlain text
    date
------------
 2026-11-27
(1 row)

Step 4: Avoid Day and Month Orders

A date with slashes depends on a server setting, DateStyle:

postgres=#SQL
SELECT '03/04/2026'::date;
OutputPlain text
    date
------------
 2026-03-04
(1 row)

This server reads it as 4 March, because its DateStyle puts the month first. A server set to day first reads the same text as 3 April. Year first removes the guesswork.

Common Mistakes

MistakeWhat happensDo this instead
A date that doesn’t existERROR: date/time field value out of range: "2026-02-30"Check the day: February has 28 or 29 days.
Slashes, such as 03/04/2026The server decides which number is the month, as Step 4 showed.Write 2026-03-04.

FAQ

Does a date have a time?

No. A date holds only the day. For a moment in time, PostgreSQL has timestamptz.

Conclusion

Write dates as DATE 'YYYY-MM-DD', year first. PostgreSQL reads that the same way on every server, and refuses a day that doesn’t exist.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments