export DATADIR="/tmp/foo/bar/baz/"
export BASENAME=`basename $DATADIR`
export PARENT_DIR=`dirname $DATADIR`

# Just for clean up
if [ -d $DATADIR ]; then
  pg_ctl -D $DATADIR stop
  rm -rf $DATADIR || exit 0
fi

mkdir -p $DATADIR || exit 0

# Run initdb and start up a fresh PostgreSQL instance:
cd $PARENT_DIR || exit 0

initdb -D $BASENAME || exit 0
pg_ctl -D $BASENAME start || exit 0

# Let the server come up
sleep 3

# Works fine:
pg_ctl -D `basename $DATADIR` status || exit 0
pg_ctl -D `basename $DATADIR` restart || exit 0
sleep 3

cd `dirname $PARENT_DIR` || exit 0

# Displays status fine
pg_ctl -D $DATADIR status || exit 0

# ERROR: shuts down the database successfully, but will complain:
# postgres cannot access the server configuration file "/tmp/foo/baz/postgresql.conf": 
# No such file or directory
pg_ctl -D $DATADIR restart
