How to set up a monthly reset function in a database

37 views Asked by At

I am trying to create a function in my postgres database to reset, at the beginning of each month, the column nbr_requests of my table users, the structure of which is as follows:

users : {
    id: SERIAL,
    full_name: VARCHAR(255),
    nbr_requests: INT,
    max_requests: INT
}

I tried to install pg_cron, a postgres extension to do this and I created a schedule: SELECT * FROM cron.schedule('0 0 1 * *', $$UPDATE users SET nbr_requests = 0$$); But that doesn't do anything and I encounter an error "connection failed"... while I added these lines in my pg_hba.conf file:

host   all             all             127.0.0.1/32            trust
host   all             all             0.0.0.0/0               md5

I added these lines in my postgresql.conf file:

shared_preload_libraries = 'pg_cron'
cron.database_name = 'nv_api_db'
cron.timezone = 'CET'

And before running the query CREATE EXTENSION pg_cron;, I restarted the postgres service and logged in database as superuser with psql.

0

There are 0 answers