What is the postgres equivalent of the below mysql code
CREATE TABLE t1 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t2 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
As per Alex Brasetvik answer below, it seems i should go with triggers, my problem is i have a number of tables t1, t2... with created and modified fields, is it possible to write a generalized procedure?
--update Almost ready
CREATE FUNCTION update_timestamp() RETURNS trigger AS $update_timestamp$
BEGIN
NEW.modified := current_timestamp;
RETURN NEW;
END;
$update_timestamp$ LANGUAGE plpgsql;
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t1
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t2
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
Postgresql now() The NOW() function in Postgresql is used to get the current date and time. The return type of the NOW() function is the timestamp with the time zone. We can fetch the current date and time by using the PostgreSQL NOW() function. This function has a return type i.e. the timestamp with the time zone.
What is PostgreSQL Now Function? The Now() function is used to return the current date and time of the time zone (default or user-defined). Its return type is the timestamp with the time zone.
The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.
SELECT CURRENT_TIME, CURRENT_TIMESTAMP; Let's have a look at using the CURRENT_DATE keyword within the SELECT query to check the current date in PostgreSQL. So, we have tried the below query to get the time and date for the current region, e.g. Asia. The output shows the date and time with a time zone of Asia.
Just make sure all tables have the same columnname:
CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE TRIGGER t_name
BEFORE UPDATE
ON tablename
FOR EACH ROW
EXECUTE PROCEDURE upd_timestamp();
Thank you for the information Mithun and Alex Brasetvik.
I'd like to add one minor tweak to the trigger. Since we mostly likely want the modified column to store the timestamp when the row was last changed, not when it was the target of an UPDATE statement, we have to compare the new and the old value of the row. We update the modified column only if these two values differ.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
IF (NEW != OLD) THEN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END IF;
RETURN OLD;
END;
$$;
This trigger ensures that the modified column is updated only if the UPDATE operation actually changes the values stored in the row.
Update it with a trigger. Documentation and examples.
To further improve, the answer given by @Lauri Silvennoinen:
This trigger uses the WHEN
clause as recommended by the official docs to check for changes in the row even before calling the specified function.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER tg_update_modified
AFTER UPDATE ON table_name
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE FUNCTION update_modified_timestamp();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With