Setting up postgreSql database

Hi

I have a small bash script which I want to run on an Amazon EC2 Ubuntu instance for setting up a postgreSQL database:

#!/bin/bash
USERNAME='postgres'
start=$SECONDS
TMP_DIR=/local/test/4g4d
PORT=23456

rm -rf $TMP_DIR/db
mkdir -p $TMP_DIR/

echo "creating database..."
~/postgresql/bin/initdb -D $TMP_DIR/db
if [ $? -ne 0 ]
  then
    echo "Could not create the database"
fi

sed -i "s/#port = 5432/port = $PORT/" $TMP_DIR/db/postgresql.conf
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/" $TMP_DIR/db/postgresql.conf
echo "host all all  0.0.0.0/0 md5" >> $TMP_DIR/db/pg_hba.conf

echo "starting postgresql server..."
~/postgresql/bin/pg_ctl -D $TMP_DIR/db -l $TMP_DIR/db/logfile start

if [ $? -ne 0 ]
  then
    echo "Could not start the postgresql server"
fi

sleep 10

echo "creating database..."
~/postgresql/bin/createdb -U $USERNAME -p $PORT asldb

echo "adding schema to database"
~/postgresql/bin/psql asldb -U $USERNAME -f ~/tpch/DBSetup.sql -p $PORT

end=$SECONDS
echo "Time: $((end - start)) secs."

hostname=`hostname`
pid=$$

while [ true ]
do
  echo "writing log..."
  echo `date +"%T"` >> who-$pid@$hostname.log
  echo `date +"%T"` >> top-$pid@$hostname.log
  echo `date +"%T"` >> ps-$pid@$hostname.log
  who >> who-$pid@$hostname.log
  top -n 1 -b >> top-$pid@$hostname.log
  ps aux >> ps-$pid@$hostname.log
  echo "" >> who-$pid@$hostname.log
  echo "" >> top-$pid@$hostname.log
  echo "" >> ps-$pid@$hostname.log
  sleep 10
done

#read -p "Press any key to shutdown server."
# shut server down
#~/postgresql/bin/pg_ctl stop -D $TMP_DIR/db

# delete database
#rm -rf $TMP_DIR

Is this ok? How can I automatically install postgreSQL in a first step (apt-get install...)?

What exactly does the "writting log" loop at the end, i.e. what is logged and where exactly is the log saved?

You should really avoid installing postgreSQL I'd suggest checking it is installed and reporting an issue if not.

The loop at the end of this script never ends as it uses a while [ true ] loop

the loop creates three files :
who-<PID>@<HOST>.log file with output from who command
top-<PID>@<HOST>.log file with output from top command
ps-<PID>@<HOST>.log file with output from ps command

these are written into the current directory and appended every 10 seconds.

Thank you Chubler.

What does output from the who, top and ps command mean, i.e. what will these files contain?

These are standard unix programs (run them from the command line to see their output):