Omid Sayfun
Omid SayfunComputer Geek
Home
Notebook
Journey

Online

Github
Linkedin
Hevy
Plyatomic
Notebook
The trap of making everything dynamic
March 01, 2024
A try at type-safe groupBy function in TypeScript
April 10, 2025
Email special headers
November 20, 2024
Adding prettier to eslint
April 10, 2025
Storing Vector in Postgres with Drizzle ORM
March 21, 2024
Upgrading my blog to Next 15
April 05, 2025
Canvas macOS issue
February 20, 2024
tsx doesn’t support decorators
March 26, 2025
Validating NestJS env vars with zod
February 06, 2025
Extending Window: types vs interfaces
March 21, 2025
Loading env file into Node process
February 06, 2025
Add update date field to Postgres
February 27, 2024
Using node API for delay
February 06, 2025
React Component Lifecycle
November 28, 2024
How CQRS is different than Event Sourcing
August 18, 2024
RabbitMQ exchange vs queue
August 14, 2024
PgVector similarity search distance functions
August 13, 2024
PgVector indexing options for vector similarity search
July 31, 2024
Using puppeteer executable for GSTS
June 08, 2024
Why EQ is Your Next Career Upgrade
May 13, 2024
Counting GPT tokens
June 30, 2024
Logging route handler responses in Next.js 14
June 19, 2024
Redirect www subdomain with Cloudflare
June 17, 2024
Logging requests in Express app
June 16, 2024
Move Docker volume to bind mount
June 12, 2024
Next.js Hydration Window Issue
May 29, 2024
Using Git rebase without creating chaos in your repo
May 16, 2024
Implementing RPC Calls with RabbitMQ in TypeScript
March 16, 2024
Optimize webpage load with special tags
March 15, 2024
What the hell is Open Graph?
March 13, 2024
My go-to Next.js ESlint config
March 10, 2024
List of useful Chrome args
March 10, 2024
Combining RxJS observables - Part 1
February 20, 2024

Add update date field to Postgres

February 27, 2024 · Updated on March 07, 2025

Transitioning from MySQL to PostgreSQL can be a bit of a rollercoaster. One of the quirks you'll encounter is how each handles automatic timestamp updates. In MySQL, you can easily set up a table with timestamp columns that auto-update whenever a row changes. Here's a typical MySQL setup:

CREATE TABLE Users (
	...
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

But PostgreSQL doesn't support the ON UPDATE CURRENT_TIMESTAMP syntax directly. So, how do you achieve the same functionality?

Triggers

In PostgreSQL, you can use triggers to automatically update a field. Triggers are functions that execute automatically when certain events occur on a table, like inserts, updates, or deletes. They're great for enforcing business rules, auditing, or maintaining data integrity.

To mimic MySQL's ON UPDATE CURRENT_TIMESTAMP behavior, you'll need to create a trigger. Here's a trigger function that updates the updated_at field to the current timestamp whenever a row is updated:

CREATE OR REPLACE FUNCTION trigger_updated_at()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();


Continue Reading

RETURN
NEW;
END;
$$ LANGUAGE plpgsql;

Activate Trigger

After creating the trigger function, you need to activate it on your table. Here's how to set it up for the Users table:

CREATE TRIGGER update_users_updated_on
    BEFORE UPDATE
    ON
        Users
    FOR EACH ROW
EXECUTE PROCEDURE trigger_updated_at();

This setup ensures that the updated_at column is automatically updated to the current timestamp every time a row is modified. You don't need to worry about inserts because the default value handles that.

tl;dr

To auto-update a timestamp in PostgreSQL like MySQL's ON UPDATE CURRENT_TIMESTAMP, use a trigger. Create a trigger function to set updated_at to NOW() on updates, then activate it on your table with a BEFORE UPDATE trigger.

  • 02-20-2026

    The trap of making everything dynamic

  • 04-11-2025

    A try at type-safe groupBy function in TypeScript

  • 04-10-2025

    Email special headers

  • 04-10-2025

    Adding prettier to eslint

  • 04-09-2025

    Storing Vector in Postgres with Drizzle ORM