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
DATE 'YYYY-MM-DD'
'YYYY-MM-DD'::date
YYYY-MM-DD: the year, the month and the day, with hyphens, such as2026-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:
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
SELECT DATE '2026-11-27';
date
------------
2026-11-27
(1 row)
Step 3: The Same Date as a Cast
SELECT '2026-11-27'::date;
date
------------
2026-11-27
(1 row)
Step 4: Avoid Day and Month Orders
A date with slashes depends on a server setting, DateStyle:
SELECT '03/04/2026'::date;
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
| Mistake | What happens | Do this instead |
|---|---|---|
| A date that doesn’t exist | ERROR: date/time field value out of range: "2026-02-30" | Check the day: February has 28 or 29 days. |
Slashes, such as 03/04/2026 | The 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
- PostgreSQL 19 documentation: Date/Time Input (opens in a new tab)
- PostgreSQL 19 documentation: DateStyle (opens in a new tab)
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