PostgreSQL Utility for EnterpriseDB Installation
PostgreSQL Installer provided by EnterpriseDB is so handy and make your life very easy for OS X Desktop installation.
Maybe I just don’t know the handy utility but I wrote a shell script to control/display various tasks hence I wanted to share this with you.
The utility contains: start | stop | restart | stopfast | stopnow | status | vac_status | show_connections | version | restore backup
NODE_NAME=`uname -n`NODE_NAME=${NODE_NAME%%\.*local}NODE_NUMBER=${NODE_NAME##*SQL-}PGBIN=/Library/PostgreSQL/9.1/binPGDATA=/Library/PostgreSQL/9.1/datapidof(){# usage: `pidof`ps -ef | grep postgres | awk '{if ($3==1)print $2}'}start(){pid=`pidof`if [ "$pid" == "" ] ; thenecho "Starting postgres..."su - postgres -c "${PGBIN}/pg_ctl -w start -D ${PGDATA} -l ${PGDATA}/pg_log/startup.log"elsestatusfi}stop(){pid=`pidof`if [ "$pid" != "" ] ; thenecho "Stopping postgres..."su - postgres -c "${PGBIN}/pg_ctl stop -m smart -w -D ${PGDATA}"elseecho "Postgres is not running"fi}stopfast(){pid=`pidof`if [ "$pid" != "" ] ; thenecho "Stopping postgres..."su - postgres -c "${PGBIN}/pg_ctl stop -m fast -w -D ${PGDATA}"elseecho "Postgres is not running"fi}stopnow(){pid=`pidof`if [ "$pid" != "" ] ; thenecho "Stopping postgres..."su - postgres -c "${PGBIN}/pg_ctl stop -m immediate -w -D ${PGDATA}"elseecho "Postgres is not running"fi}status(){pid=`pidof`if [ "$pid" != "" ] ; thenecho "postgres is running"echo `ps -ef | grep postgres | grep -v grep | awk '{if ($3==1) print "Postgres: " $8 ", Data Directory: " $10}'`elseecho "postgres is not running"fi}vac_status(){su - postgres -c "${PGBIN}/psql -c \"select relname, last_autovacuum, last_autoanalyze from pg_stat_user_tables where last_autovacuum IS NOT NULL order by last_autovacuum desc\""}show_connections(){su - postgres -c "${PGBIN}/psql -c \"SELECT datname, usename, procpid, client_addr, waiting, query_start, current_query FROM pg_stat_activity;\""}version(){su - postgres -c "${PGBIN}/psql -c \"SELECT version()\""}restore_bak(){echo "backup file path : "read BACKUPFILEPATHecho "database name : "read DBNMAEchmod -R 777 $BACKUPFILEPATHcat ${BACKUPFILEPATH} | gunzip | ${PGBIN}/psql -U postgres ${DBNMAE}${PGBIN}/psql -d ${DBNMAE} -U postgres < /users.sql}case "$1" instart)start;;stop)stop;;status)status;;restart)stopstart;;stopfast)stopfast;;stopnow)stopnow;;vac_status)vac_status;;show_connections)show_connections;;restore_bak)restore_bak;;version)version;;*)echo "Usage: $0 { start | stop | restart | stopfast | stopnow | status | vac_status | show_connections | version | restore_bak }"exit 1esacexit 0