2 #-------------------------------------------------------------------------
5 # Create a secondary PostgreSQL database storage area.
7 # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 # Portions Copyright (c) 1994, Regents of the University of California
12 # $Header: /cvsroot/pgsql/src/bin/initlocation/Attic/initlocation.sh,v 1.11 2001/02/18 18:33:59 momjian Exp $
14 #-------------------------------------------------------------------------
17 echo "$CMDNAME failed."
24 EffectiveUser=`id -n -u 2>/dev/null || whoami 2>/dev/null`
26 if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ]
28 echo "You cannot run $CMDNAME as root. Please log in (using, e.g., 'su')"
29 echo "as the (unprivileged) user that will own the server process."
38 # These options are not really necessary, but what the heck.
40 Location=`echo $1 | sed 's/^--pgdata=//'`
54 echo "$CMDNAME: invalid option: $1" 1>&2
55 echo "Try '$CMDNAME --help' for more information." 1>&2
67 echo "$CMDNAME initializes an alternative filesystem location for database creation."
70 echo " $CMDNAME LOCATION"
72 echo "Please read the description of the CREATE DATABASE command for details."
74 echo "Report bugs to <pgsql-bugs@postgresql.org>."
79 if [ -z "$Location" ]; then
80 echo "$CMDNAME: missing required argument LOCATION" 1>&2
81 echo "Try '$CMDNAME -?' for help." 1>&2
87 # Here's what's going on:
89 # You can call initlocation ENVAR (no dollar sign), then ENVAR will
90 # (a) be tested whether it is valid as a path, or
91 # (b) be resolved as an environment variable.
92 # The latter has been the traditional behaviour.
94 # You can call initlocation $ENVAR, which will of course be resolved
95 # by the shell, or initlocation some/path (containing at least one slash).
96 # Then you just take that path.
97 # This should appease users who are confused by the above behavour.
100 echo "$Location" | grep '/' >/dev/null 2>&1
102 if [ "$?" -ne 0 -a ! -d "$Location" ]; then
103 PGALTDATA=`printenv $Location 2> /dev/null`
104 if [ -z "$PGALTDATA" ]; then
105 echo "$CMDNAME: environment variable $Location not set" 1>&2
110 PGALTDATA="$Location"
114 echo "The location will be initialized with username \"$EffectiveUser\"."
115 echo "This user will own all the files and must also own the server process."
118 # -----------------------------------------------------------------------
119 # Create the data directory if necessary
120 # -----------------------------------------------------------------------
122 # don't want to leave anything lying around
123 trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
125 # umask must disallow access to group, other for files and dirs
128 if [ ! -d $PGALTDATA ]; then
129 echo "Creating directory $PGALTDATA"
131 if [ $? -ne 0 ]; then
132 echo "$CMDNAME: could not create $PGALTDATA" 1>&2
133 echo "Make sure $PGALTDATA is a valid path and that you have permission to access it." 1>&2
137 echo "Fixing permissions on pre-existing directory $PGALTDATA"
138 chmod go-rwx "$PGALTDATA" || exit_nicely
142 if [ ! -d $PGALTDATA/base ]; then
143 echo "Creating directory $PGALTDATA/base"
144 mkdir "$PGALTDATA/base"
145 if [ $? -ne 0 ]; then
146 echo "$CMDNAME: could not create $PGALTDATA/base" 1>&2
147 echo "Make sure $PGALTDATA/base is a valid path and that you have permission to access it." 1>&2
151 echo "Fixing permissions on pre-existing directory $PGALTDATA/base"
152 chmod go-rwx "$PGALTDATA/base" || exit_nicely
156 echo "$CMDNAME is complete."
157 # We can only suggest them these commands if they used the environment
158 # variable notation. Otherwise they would be induced to use an absolute
159 # path, which the backend won't allow by default.
160 if [ "$haveenv" = "t" ]; then
161 echo "You can now create a database using"
162 echo " CREATE DATABASE <name> WITH LOCATION = '$Location'"
164 echo " createdb <name> -D '$Location'"
165 echo "from the shell."