In this quick article, we are presenting you with the shell script to start and stop PostgreSQL DB instance.
You can use these scripts as part of rc.d
or init.d
to be executed during the server shutdown and boot up.
Feel free to modify it further to suit your needs. Hope this helps.

Shell script to STOP PostgreSQL instance
You need to update a few key values on the script before executing.
- PATH - the path where the PostgreSQL binaries are present
- PGDATA - Data directory of your Postgres DB instance
- PG_LOG_FILE - log file location for this DB instance.
# script to stop Postgres Database
export PATH=/cdwapps/local/pgsql/bin:$PATH
export PGDATA=/cdwsapps/airflow/database
PG_LOG_FILE=/cdwapps/airflow/logs/postgres.log
while [[ ${num_trys} -lt 3 ]];do
echo -e "postgre is running for PGDATA Location ${PGDATA} \n"
echo -e "Attempt: ${num_trys} to stop..."
pg_ctl stop -D ${PGDATA} -t 180
echo -e "Stop command Successful."
echo -e "Stop command Failed"
echo -e "Waiting 30 seconds for DB Gracefully shutdown..."
echo -e "PG ${PGDATA} has been stopped successfully..."
#!/usr/bin/env bash
# script to stop Postgres Database
export PATH=/cdwapps/local/pgsql/bin:$PATH
export PGDATA=/cdwsapps/airflow/database
PG_LOG_FILE=/cdwapps/airflow/logs/postgres.log
num_trys=0
while [[ ${num_trys} -lt 3 ]];do
if ! pg_ctl status; then
echo -e "postgre is running for PGDATA Location ${PGDATA} \n"
echo -e "Attempt: ${num_trys} to stop..."
pg_ctl stop -D ${PGDATA} -t 180
if [ $? -eq 0 ]
then
echo -e "Stop command Successful."
else
echo -e "Stop command Failed"
fi
echo -e "Waiting 30 seconds for DB Gracefully shutdown..."
sleep 30
if ! pg_ctl status; then
echo -e "PG ${PGDATA} has been stopped successfully..."
break
fi
((num_trys++))
else
break
fi
done
#!/usr/bin/env bash
# script to stop Postgres Database
export PATH=/cdwapps/local/pgsql/bin:$PATH
export PGDATA=/cdwsapps/airflow/database
PG_LOG_FILE=/cdwapps/airflow/logs/postgres.log
num_trys=0
while [[ ${num_trys} -lt 3 ]];do
if ! pg_ctl status; then
echo -e "postgre is running for PGDATA Location ${PGDATA} \n"
echo -e "Attempt: ${num_trys} to stop..."
pg_ctl stop -D ${PGDATA} -t 180
if [ $? -eq 0 ]
then
echo -e "Stop command Successful."
else
echo -e "Stop command Failed"
fi
echo -e "Waiting 30 seconds for DB Gracefully shutdown..."
sleep 30
if ! pg_ctl status; then
echo -e "PG ${PGDATA} has been stopped successfully..."
break
fi
((num_trys++))
else
break
fi
done
Shell script to start PostgreSQL instance
you need to update the following variables before using the script
- PATH - the path where the PostgreSQL binaries are present
- PGDATA - Data directory of your Postgres DB instance
- PG_LOG_FILE - log file location for this DB instance.
export PATH=/usr/lib/postgresql/10/bin:$PATH
export PGDATA=/var/lib/postgresql/10/main
PG_LOG_FILE=/tmp/postgres.log
while [[ ${num_trys} -lt 3 ]];do
echo -e "postgre is running for PGDATA Location ${PGDATA} \n"
echo -e "Airflow PG ${PGDATA} not running \n"
echo -e "Attempt: ${num_trys} to restart..."
pg_ctl start -D ${PGDATA} -l ${PG_LOG_FILE}
echo -e "Waiting 30 seconds for DB to come up..."
echo -e "PG ${PGDATA} has been started successfully..."
#!/usr/bin/env bash
export PATH=/usr/lib/postgresql/10/bin:$PATH
export PGDATA=/var/lib/postgresql/10/main
PG_LOG_FILE=/tmp/postgres.log
num_trys=0
while [[ ${num_trys} -lt 3 ]];do
if pg_ctl status; then
echo -e "postgre is running for PGDATA Location ${PGDATA} \n"
break
else
((num_trys++))
echo -e "Airflow PG ${PGDATA} not running \n"
echo -e "Attempt: ${num_trys} to restart..."
pg_ctl start -D ${PGDATA} -l ${PG_LOG_FILE}
echo -e "Waiting 30 seconds for DB to come up..."
sleep 30
if pg_ctl status; then
echo -e "PG ${PGDATA} has been started successfully..."
break
fi
fi
done
#!/usr/bin/env bash
export PATH=/usr/lib/postgresql/10/bin:$PATH
export PGDATA=/var/lib/postgresql/10/main
PG_LOG_FILE=/tmp/postgres.log
num_trys=0
while [[ ${num_trys} -lt 3 ]];do
if pg_ctl status; then
echo -e "postgre is running for PGDATA Location ${PGDATA} \n"
break
else
((num_trys++))
echo -e "Airflow PG ${PGDATA} not running \n"
echo -e "Attempt: ${num_trys} to restart..."
pg_ctl start -D ${PGDATA} -l ${PG_LOG_FILE}
echo -e "Waiting 30 seconds for DB to come up..."
sleep 30
if pg_ctl status; then
echo -e "PG ${PGDATA} has been started successfully..."
break
fi
fi
done
Are you in need of any Automation? you give us the challenge/problem statement we will solve it for you. reach out to us at hello@gritfy.com
Cheers
Sarav AK
Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content
More from Middleware Inventory
-
Ansible Shell Module Examples Ansible shell module is designed to execute Shell commands against the target Unix based hosts. Unlike the Ansible command module, Ansible Shell would accept any highly complexed commands with pipes, redirection etc and you can also execute Shell scripts using Ansible Shell module. The Advantage of Ansible Shell module…
-
Weblogic Start All Managed Servers WLST ScriptThe Objective In this post, we are going to share the WLST jython script we have created to start all the managed servers in the weblogic domain. To be more precise, The Script will start the servers which are not already started or down from the specific weblogic domain. Once…
-
Shell Script to SSH with Password - How to Handle Password PromptAre you looking for a Shell Script that connects to remote server and executes some commands or another script? Are you looking for a Shell Script which SCP files to a remote server? Are you looking for Shell Script to SSH with Password and Automatically handle the Password Prompt? Wondering…
-
weblogic server status script - WLSTA Small and Simple script to get all the server status from the weblogic domain, Including AdminServer & Managed Server. The Jython Script How to Execute this script Copy the preceding script content to a file, let's say /tmp/get_wls_serverstate.py cd domain/bin . ./setDomainEnv.sh java weblogic.WLST /tmp/get_wls_serverstate.py Script Output Hope this…
-
Weblogic Active Gridlink Datasource creation script WLSTThe Objective The post is about how to do Weblogic Active Grid Link Data source creation using WLST. We are giving WLST script here to accomplish the same. It has some nice features like Duplicate Validation Test Connection Database Credential validation Bulk Datasource creation. We hope you would find it…