64

I have a PostgreSQL database in which one table rapidly grows very large (several million rows every month or so) so I'd like to periodically archive the contents of that table into a separate table.

I'm intending to use a cron job to execute a .sql file nightly to archive all rows that are older than one month into the other table.

I have the query working fine, but I need to know how to dynamically create a timestamp of one month prior.

The time column is stored in the format 2013-10-27 06:53:12 and I need to know what to use in an SQL query to build a timestamp of exactly one month prior. For example, if today is October 27, 2013, I want the query to match all rows where time < 2013-09-27 00:00:00

3
  • 1
    "The time column is stored in the format 2013-10-27 06:53:12" - you should use a timestamp column for that, not a formatted string, as it will make things like this a lot easier and more efficient.
    – IMSoP
    Commented Oct 27, 2013 at 12:38
  • 1
    "30 days" is more precise than "one month", and "exactly one month" is essentially meaningless. In SQL, if you subtract exactly one month from 2013-03-28, 03-29, 03-30, and 03-31, and you get the same answer: 2013-02-28. Does that matter? Commented Oct 27, 2013 at 22:47
  • @IMSoP Unfortunately, I don't have control over the database structure. I'm running a Quassel IRC Core, and having 18 million rows in the backlog table results in the client loading very slowly, so I need to archive all but recent rows in order to maintain client stability.
    – Dr. McKay
    Commented Oct 29, 2013 at 4:40

3 Answers 3

97

Question was answered by a friend in IRC:

'now'::timestamp - '1 month'::interval

Having the timestamp return 00:00:00 wasn't terrible important, so this works for my intentions.

2
72
select date_trunc('day', NOW() - interval '1 month')

This query will return date one month ago from now and round time to 00:00:00.

1
  • 2
    Note that this will return an actual timestamp; from the wording of the question, the actual column has a string, so you will need to cast it to compare: WHERE CAST("time" as timestamp) < date_trunc('day', now() - interval '1 month')
    – IMSoP
    Commented Oct 27, 2013 at 12:41
4

When you need to query for the data of previous month, then you need to query for the respective date column having month values as (current_month-1).

SELECT * 
FROM {table_name}
WHERE {column_name} >= date_trunc('month', current_date-interval '1' month)
    AND {column_name} < date_trunc('month', current_date)

The first condition of where clause will search the date greater than the first day (00:00:00 Day 1 of Previous Month)of previous month and second clause will search for the date less than the first day of current month(00:00:00 Day 1 of Current Month). This will includes all the results where date lying in previous month.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.