Delete Records From Postgres Via Docker And Bash Script
When you create a large enterprise software solution for web or mobile, you required a shell script to automate certain tasks like sending Notification (Email, Push, and SMS), deleting records from DB, scheduling backup, etc.
We have deployed our software solution on EC2 with Docker and Postgres is installed via Docker. We are using the Docker swarm container orchestration tool. To access the Postgres container install docker have the docker exec command.
docker exec -it $(docker ps -q -f name=postgres) bash
docker ps -q -f name=postgres – This will give us container ID because container ID changes every time when you make new changes to code and redeploy. After executing the above command you will enter into a bash of Postgres container.
Once you get into postgres container you can access postgres DB using psql command. To log into postgres DB you can write – psql -U $DB_USER -d $DB_NAME
here $DB_USER AND $SB_NAME is environment variables.
The above image state that whenever we want to execute any query we will call the execute_sql() function and get desired result.
If you want to schedule this script to run at a specific time or date, you can do it easily.