Postgres aging out: Similar to S3 delete old tqaables after no longer touched for x days

48 Views Asked by At

Is there any convenience functionality for Postgres (14,15) which would delete old data i.e. similarly to S3 where data older than X days might be deleted automatically after aging off I would want to delete old (no longer touched) tables (not the rows actually the tables) in Postgres.

1

There are 1 best solutions below

1
Ponsakorn30214 On

I believe PostgreSQL doesn't have any configuration to delete rows based on the last time it is updated.
You can configure cron to delete row when it run, something like:

0 0 * * * psql -U username -d database_name -c "DELETE FROM my_table WHERE created_at < NOW() - INTERVAL '1 week';"

Alternatively, you can set up something like pg_cron to do it for you from inside PostgreSQL.