Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL current_date Function: How to Use It

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The PostgreSQL current_date function returns the current date according to the server’s time zone, without any time component. It’s useful for filtering records by today’s date, setting default values in tables, or logging events without timestamps. No installation or setup is needed beyond having a working PostgreSQL connection, as current_date is a built-in function available in all modern versions.

What You'll Need

  • A working PostgreSQL server (version 9.0 or later)
  • Access to a database via psql, pgAdmin, DBeaver, or any SQL client
  • Basic understanding of SQL syntax and executing queries
  • Permission to run SELECT queries on the database

Step-by-Step

  1. Connect to your PostgreSQL database using your preferred client. For example, open a terminal and type psql -U username -d database_name, then enter your password when prompted.
  2. Once connected, execute a simple query to retrieve the current date: SELECT current_date; Press Enter. The result will display the date in YYYY-MM-DD format, such as 2024-06-15.
  3. To use current_date in a WHERE clause to filter records, run a query like: SELECT * FROM orders WHERE order_date = current_date; This returns all rows where the order_date column matches today’s date.
  4. Set current_date as a default value for a date column when creating a table. For example: CREATE TABLE logs (id SERIAL PRIMARY KEY, event_date DATE DEFAULT current_date, message TEXT); Any new row inserted without a specified event_date will automatically use today’s date.
  5. Combine current_date with date arithmetic. For example, to find records from the last seven days: SELECT * FROM sales WHERE sale_date >= current_date - 7; PostgreSQL allows simple integer subtraction to subtract days from a date.
  6. Compare current_date with a timestamp column by casting the timestamp to date: SELECT * FROM events WHERE event_timestamp::date = current_date; This ensures you’re comparing date-only values, avoiding mismatches due to time components.
  7. Use current_date in an INSERT statement: INSERT INTO users (name, registration_date) VALUES ('Alice', current_date); This avoids hardcoding dates and ensures accuracy regardless of when the query runs.
  8. Test the function in different time zones by checking your server’s time zone setting: SHOW timezone; The value returned affects what current_date outputs. If your server is in UTC and you’re in New York, current_date may reflect yesterday’s date in your local time.
  9. For debugging, combine current_date with other functions: SELECT current_date, current_date + 1 AS tomorrow, current_date - 30 AS one_month_ago; This helps validate date arithmetic is working as expected.

Common Mistakes

  • Confusing current_date with now() or current_timestamp — these include time and will not match a date-only column unless cast.
  • Using current_date as a string literal, such as WHERE date_column = 'current_date' — this treats it as text, not a function, and will return no results.
  • Assuming current_date uses the client’s local time — it always uses the server’s system time zone, not the user’s device or browser time.
  • Trying to update a column with current_date using an UPDATE statement without proper syntax: UPDATE users SET last_login = current_date WHERE id = 1; is correct, but UPDATE users SET last_login = 'current_date' is not.
  • Expecting current_date to change during a long-running transaction — it returns the date when the transaction started, not the date when the query executes in a multi-statement transaction.

Tips

  • Use current_date for date-only comparisons; use current_timestamp if you need both date and time precision.
  • When building reports, alias the result for clarity: SELECT current_date AS report_date;
  • Combine current_date with interval calculations: current_date + interval '1 week' gives you the date one week from now.
  • For applications where users are in multiple time zones, store dates in UTC and convert for display — avoid relying on current_date for user-facing local dates.
  • Use current_date in triggers to auto-populate audit fields, like created_at or updated_at, if you only care about the date, not the exact time.
  • Always test date logic with known values. For example, if today is June 15, run SELECT current_date = '2024-06-15'::date; to confirm the function returns the expected value.
  • When joining tables, ensure both sides are the same type — compare DATE to DATE, not DATE to TIMESTAMP.

Troubleshooting

  • If SELECT current_date; returns an error like function current_date does not exist, your PostgreSQL version is very old (pre-9.0). Upgrade to a supported version.
  • If your query returns no rows when you expect data, check if your table’s date column contains time components. Cast it with ::date to match current_date.
  • If the date appears to be off by one day, check your server’s time zone with SHOW timezone; and compare it to your local time. Adjust the server’s timezone setting if needed using ALTER DATABASE database_name SET timezone TO 'America/New_York';
  • If current_date works in psql but not in your application, ensure your application’s database connection is not overriding the time zone or using a different connection pool with different settings.
  • If you get a syntax error near current_date, double-check for typos — it’s one word, no parentheses: current_date, not current_date().
  • When using an ORM like Django or Hibernate, ensure the framework isn’t wrapping current_date in quotes or converting it to a string. Use raw SQL or the ORM’s built-in date functions instead.
  • If you’re using a cloud database (like AWS RDS or Google Cloud SQL), confirm the instance’s time zone hasn’t been manually changed from the default. Contact your provider’s support if unsure.

Frequently Asked Questions

Does current_date change during a long-running transaction?

No. current_date is evaluated once at the start of the transaction and remains constant throughout, even if the transaction runs for hours.

Can I use current_date in a view?

Yes. You can include current_date in a view definition. Every time the view is queried, it returns the current date at the time of the query.

Is current_date affected by daylight saving time?

Yes. Since it uses the server’s time zone setting, any daylight saving time changes configured on the server will affect the date returned.

How is current_date different from now()::date?

They return the same date, but current_date is more efficient and clearer in intent. now()::date requires converting a timestamp to date, while current_date is a direct date value.

Can I use current_date in a stored procedure?

Yes. You can use current_date in PL/pgSQL functions just like in regular SQL queries. It will return the server’s current date when the function executes.

Why does my application show a different date than current_date returns?

Your application may be running in a different time zone than the PostgreSQL server. Always check the server’s timezone setting and ensure your application uses UTC or matches the database’s time zone for consistency.