2

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.

1
  • I really don't understand this use case. How is the user going to execute this script? What OS? If you're using psql, you can use a bash script or bat file to prompt user for duration and then run your query. Commented May 26, 2018 at 5:32

2 Answers 2

3

Edit: postgres has built in support for parameters in scripts (the -v flag). See the answer from op below.

In xyz.sh:

#!/usr/bin/env bash

. ./.config

sudo -u user psql -d db -c "DELETE FROM abc WHERE eventtime::DATE <=(CURRENT_TIMESTAMP - INTERVAL '${DURATION} days')::DATE"

You'd have to quote the duration in your .config file because there's a space in it:

# Interval in the past to keep
DURATION="7 DAY"

If you want to keep the script in a separate file you'll have to do token replacement on it before feeding it to psql. A utility like sed can be used:

In xyz.sh:

#!/usr/bin/env bash

. ./.config

cat delete.sql | sed -e "s/DURATION/${DURATION}/g" | sudo -u user psql -d db
Sign up to request clarification or add additional context in comments.

6 Comments

I have updated the query. I have added my shell script from where my sql script is being called.
Is there an objection to including the SQL in the bash script? If you include it inline you will be able to template it as above. It will be more work to read it from a separate file.
yes i was able to achieve from the shell script giving it inline but i was looking for someway to configure sql script.
You could use sed to do token replacement. I've updated the example.
sed is being used here to replace the word DURATION with the value from the config file. The actual sed command being executed is s/DURATION/7 DAY/g - bash does the variable substitution in the command before executing it. In the land of sed s means substitute and g means global (every occurrence).
|
1

I was able to try one more way which does not involve sed command

.config file

# Interval in the past to keep
DURATION='7 DAYS'

xyz.sh

#!/bin/bash

source /<<path>>/file.config

sudo -u user psql -d db -f delete.sql -v DURATION="'${DURATION}'"

"'${DURATION}'" returns the output as '7 DAYS'

delete.sql

DELETE from abc where eventtime::date <=(CURRENT_TIMESTAMP-INTERVAL :DURATION)::date;
COMMIT;

So, now when you execute the xyz.sh file it will met the expected scenario.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.