I have created a sql script to delete a table from a database in which the records is older than 7 days with below schema.
delete.sql
DELETE from abc where eventtime::date <=(CURRENT_TIMESTAMP-INTERVAL '7 DAYS')::date;
COMMIT;
I am executing the above sql script using my shell script as below:
xyz.sh
sudo -u user psql --d db --file=<<path>>/delete.sql
Now, i have to make this script configurable i.e. user can change the duration which can be read by script directly rather than changing the script always. So, i am trying to create some .config file and to use it as a reference in sql script. But i am unable to do it. Any help with how i can pass the variable from .config file to postgreSQL script file.
Expected scenario:
.config file
# duration to delete the records from the database
DURATION =7 DAY
delete.sql
DELETE from abc where eventtime::date <=(CURRENT_TIMESTAMP-INTERVAL '$DURATION')::date;
COMMIT;
Thanks in Advance.
psql, you can use a bash script or bat file to prompt user for duration and then run your query.