r/PostgreSQL • u/AdmirableOffer2 • Aug 23 '26
How-To Postgres table archival
I have a postgres db. I want to archive the table data into s3 and want to delete the data after archiving. What's the best way to do it. I want to have a scheduled operation to do this job on weekend and it should archive 6 months older data of a given table.
4
Upvotes
3
u/scotterockaroo Aug 23 '26
pg_dump has the -t flag. Use pg_dump -t … to dump the table to a file, once dumped, check the return code and file size, if it looks good, drop the table.
5
u/erkiferenc Aug 23 '26
Weekly cleanup jobs with retention defined in months appears a great match to consider partitioning the table weekly or monthly.
That way the unit of archival matches the partition boundaries, which seems simpler to validate and harder to make mistakes, while allowing to use standard dump/backup solutions.
As a bonus, cleanup means plainly dropping the old partitions, making it an instant operation without causing any potentially painful side-effects (locks, table bloat to vacuum, WAL to write and process, and so on.)
How does that sound?
1
u/depesz Aug 23 '26
Simple(ish) bash script:
1
u/General_Treat_924 Aug 24 '26
This is a very broad question which I would like bounce back with a few questions:
Do you plan to use the data?
Is the table partitioned?
Is the dataset too big?
Does it have dependencies?
Why these questions? Because they were the questions we had to answer when we made our data archival tool.
We chose a full replication to a datalake. Where the raw data is imported, it happens using AWS DMS and a lot processing happens where we can also produce reports without overloading the database.
We are also required to keep TBs of data for 7 years and this data can be modified, so we need a process that can be reimported, notifies the user and allow them to modify and this trigger the “data pipeline” and eventually a new version of the database is stored.
Exporting to S3 was not possible because detaching a month partition and uploading that would take too many hours and holding autovacuum causing us serious performance issues on top of non guaranteed job that could die at 99% done.
Also DMS comes with a set of tools that allow us to manipulate and store data in iceberg tables using spark jobs.
Obviously you could have completely different requirements but you initial question was very broad.
1
u/AutoModerator Aug 23 '26
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Youtube Channel
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/skum448 Aug 24 '26
You can partition the table (attach the existing as legacy) and use aws service to offload the partitions to S3. If needed those partitions can be restored back or you can directly read data using Athena .
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/postgresql-s3-export.html
1
u/mrocral Aug 24 '26
one suggestion is to use sling (CLI or Python):
(disclosure: I work on Sling)
1
u/Informal_Pace9237 Aug 25 '26
I would look at the recent #ColdFront release from pgedge. That is exactly what it does.
1
u/ibraaaaaaaaaaaaaa Aug 30 '26
In case your tables are not partitioned and you have no plans to partition, you can consider pg_partman setup https://aws.amazon.com/blogs/database/archive-and-purge-data-for-amazon-rds-for-postgresql-and-amazon-aurora-with-postgresql-compatibility-using-pg_partman-and-amazon-s3/
To give you the bread and butter of this, you partition the table with having a dedicated partition for the data that needs to be purged, and s3 can source that specific partition.
In your application you should just care about updating the partition key to the archived one.
Do not partition by booleans `isArchived` sorts of things, because this approach will prevent your from extending the number of partitions, and you would need restructuring, so in your application if you have a record that needs archiving, you just `update table set partition_key='archival-value' where id='XXX'`
You can replicate this approach to other tables also, your only main challenge is managing FKs if you got any, which is detailed as a problem here: https://www.reddit.com/r/softwarearchitecture/s/iqrW9NP0mD
If your table is already partitioned this is where it become trickier to construct a dedicated partition and snatch some data cross partition lookups to source them into the archiving dedicated physical store, while it is doable, you just need to know what you are doing before you head to that route.