2 #-------------------------------------------------------------------------
5 # Create a secondary PostgreSQL database storage area.
7 # Copyright (c) 1994, Regents of the University of California
11 # $Header: /cvsroot/pgsql/src/bin/initlocation/Attic/initlocation.sh,v 1.10 2000/11/25 19:05:43 petere Exp $
13 #-------------------------------------------------------------------------
16 echo "$CMDNAME failed."
23 EffectiveUser=`id -n -u 2>/dev/null || whoami 2>/dev/null`
25 if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ]
27 echo "You cannot run $CMDNAME as root. Please log in (using, e.g., 'su')"
28 echo "as the (unprivileged) user that will own the server process."
37 # These options are not really necessary, but what the heck.
39 Location=`echo $1 | sed 's/^--pgdata=//'`
53 echo "$CMDNAME: invalid option: $1" 1>&2
54 echo "Try '$CMDNAME --help' for more information." 1>&2
66 echo "$CMDNAME initializes an alternative filesystem location for database creation."
69 echo " $CMDNAME LOCATION"
71 echo "Please read the description of the CREATE DATABASE command for details."
73 echo "Report bugs to <pgsql-bugs@postgresql.org>."
78 if [ -z "$Location" ]; then
79 echo "$CMDNAME: missing required argument LOCATION" 1>&2
80 echo "Try '$CMDNAME -?' for help." 1>&2
86 # Here's what's going on:
88 # You can call initlocation ENVAR (no dollar sign), then ENVAR will
89 # (a) be tested whether it is valid as a path, or
90 # (b) be resolved as an environment variable.
91 # The latter has been the traditional behaviour.
93 # You can call initlocation $ENVAR, which will of course be resolved
94 # by the shell, or initlocation some/path (containing at least one slash).
95 # Then you just take that path.
96 # This should appease users who are confused by the above behavour.
99 echo "$Location" | grep '/' >/dev/null 2>&1
101 if [ "$?" -ne 0 -a ! -d "$Location" ]; then
102 PGALTDATA=`printenv $Location 2> /dev/null`
103 if [ -z "$PGALTDATA" ]; then
104 echo "$CMDNAME: environment variable $Location not set" 1>&2
109 PGALTDATA="$Location"
113 echo "The location will be initialized with username \"$EffectiveUser\"."
114 echo "This user will own all the files and must also own the server process."
117 # -----------------------------------------------------------------------
118 # Create the data directory if necessary
119 # -----------------------------------------------------------------------
121 # don't want to leave anything lying around
122 trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
124 # umask must disallow access to group, other for files and dirs
127 if [ ! -d $PGALTDATA ]; then
128 echo "Creating directory $PGALTDATA"
130 if [ $? -ne 0 ]; then
131 echo "$CMDNAME: could not create $PGALTDATA" 1>&2
132 echo "Make sure $PGALTDATA is a valid path and that you have permission to access it." 1>&2
136 echo "Fixing permissions on pre-existing directory $PGALTDATA"
137 chmod go-rwx "$PGALTDATA" || exit_nicely
141 if [ ! -d $PGALTDATA/base ]; then
142 echo "Creating directory $PGALTDATA/base"
143 mkdir "$PGALTDATA/base"
144 if [ $? -ne 0 ]; then
145 echo "$CMDNAME: could not create $PGALTDATA/base" 1>&2
146 echo "Make sure $PGALTDATA/base is a valid path and that you have permission to access it." 1>&2
150 echo "Fixing permissions on pre-existing directory $PGALTDATA/base"
151 chmod go-rwx "$PGALTDATA/base" || exit_nicely
155 echo "$CMDNAME is complete."
156 # We can only suggest them these commands if they used the environment
157 # variable notation. Otherwise they would be induced to use an absolute
158 # path, which the backend won't allow by default.
159 if [ "$haveenv" = "t" ]; then
160 echo "You can now create a database using"
161 echo " CREATE DATABASE <name> WITH LOCATION = '$Location'"
163 echo " createdb <name> -D '$Location'"
164 echo "from the shell."