I’m working on a project that heavily relies on timestamp-based operations, and I need to write some tests that involve the now() function in PostgreSQL. For testing purposes, I want to be able to set a specific, hardcoded value for now() (let’s say December 31st 2027 noon) that persists until I manually reset it.
Specifically, I’m looking for a way to:
- Set a custom value for now() at the beginning of my test suite
- Have all calls to now() return this custom value throughout my tests
- Reset now() to its normal behavior after the test(s) are complete
I only need this functionality in my test database that I run locally, not in production.
Is there a way to achieve this in PostgreSQL? I’ve looked into using SET and ALTER commands or creating custom functions, but there are tons of problems, either it’s not working for the current session, or it gets reset almost immediately.
It seems like a very basic functionality for a proper test setup so I wonder if I’m doing something wrong.