Change initdb to not delete PGDATA directory unless it was created by
authorPeter Eisentraut <peter_e@gmx.net>
Fri, 1 Sep 2000 13:15:27 +0000 (13:15 +0000)
committerPeter Eisentraut <peter_e@gmx.net>
Fri, 1 Sep 2000 13:15:27 +0000 (13:15 +0000)
initdb itself. Refuse to run on existing but non-empty PGDATA directory.

src/bin/initdb/Makefile
src/bin/initdb/initdb.sh

index 459c5d4bf6efcee24dc8629d2eaf022151c20b19..4ac5063798b5fe728cdc776d46b56e825bf5bea5 100644 (file)
@@ -15,11 +15,14 @@ include $(top_builddir)/src/Makefile.global
 all: initdb
 
 initdb: initdb.sh $(top_builddir)/src/Makefile.global
-       sed -e 's/__MULTIBYTE__/$(MULTIBYTE)/g' \
-           -e 's/__VERSION__/$(VERSION)/g' \
-           -e 's:__bindir__:$(bindir):g' \
-           -e 's:__datadir__:$(datadir):g' \
-         < $< > $@
+       rm -f $@ $@.tmp
+       sed -e 's/@MULTIBYTE@/$(MULTIBYTE)/g' \
+           -e 's/@VERSION@/$(VERSION)/g' \
+           -e 's,@bindir@,$(bindir),g' \
+           -e 's,@datadir@,$(datadir),g' \
+         $< >$@.tmp
+       chmod a+x $@.tmp
+       mv $@.tmp $@
 
 install: all installdirs
        $(INSTALL_SCRIPT) initdb $(bindir)/initdb
index a1816e44a2da4bfca084e5fb98e49755a979b628..4fd542c221cb511859336f59bf5bf738cfd38f87 100644 (file)
@@ -37,8 +37,10 @@ exit_nicely(){
     echo
     echo "$CMDNAME failed."
     if [ "$noclean" != yes ]; then
-        echo "Removing $PGDATA."
-        rm -rf "$PGDATA" || echo "Failed."
+        if [ "$template_only" != yes ] && [ "$made_new_pgdata" = yes ]; then
+            echo "Removing $PGDATA."
+            rm -rf "$PGDATA" || echo "Failed."
+        fi
         echo "Removing temp file $TEMPFILE."
         rm -rf "$TEMPFILE" || echo "Failed."
     else
@@ -51,13 +53,13 @@ exit_nicely(){
 CMDNAME=`basename $0`
 
 # Placed here during build
-VERSION=__VERSION__
-bindir='__bindir__'
+VERSION='@VERSION@'
+bindir='@bindir@'
 # Note that "datadir" is not the directory we're initializing, it's
 # merely how Autoconf names PREFIX/share.
-datadir='__datadir__'
+datadir='@datadir@'
 # as set by configure --enable-multibyte[=XXX].
-MULTIBYTE=__MULTIBYTE__
+MULTIBYTE='@MULTIBYTE@'
 
 if [ "$TMPDIR" ]; then
     TEMPFILE="$TMPDIR/initdb.$$"
@@ -107,7 +109,7 @@ for prog in postgres pg_id
 do
         if [ ! -x "$PGPATH/$prog" ]
        then
-                echo "The program $prog needed by $CMDNAME could not be found. It was"
+                echo "The program \`$prog' needed by $CMDNAME could not be found. It was"
                 echo "expected at:"
                 echo "    $PGPATH/$prog"
                 echo "If this is not the correct directory, please start $CMDNAME"
@@ -368,24 +370,23 @@ echo
 # umask must disallow access to group, other for files and dirs
 umask 077
 
-if [ -f "$PGDATA"/PG_VERSION ]
+# find out if directory is empty
+pgdata_contents=`ls -A "$PGDATA" 2>/dev/null`
+if [ x"$pgdata_contents" != x ]
 then
     if [ "$template_only" != yes ]
     then
-        echo "$CMDNAME: The file $PGDATA/PG_VERSION already exists."
-        echo "This probably means initdb has already been run and the"
-        echo "database system already exists."
-        echo 
-        echo "If you want to create a new database system, either remove"
-        echo "the directory $PGDATA or run initdb with a --pgdata argument"
+        echo "$CMDNAME: The directory $PGDATA is exists but is not empty."
+        echo "If you want to create a new database system, either remove or empty"
+        echo "the directory $PGDATA or run initdb with an argument"
         echo "other than $PGDATA."
         exit 1
     fi
 else
-    if [ ! -d "$PGDATA" ]
-       then
+    if [ ! -d "$PGDATA" ]; then
         echo "Creating directory $PGDATA"
         mkdir "$PGDATA" || exit_nicely
+        made_new_pgdata=yes
     else
         echo "Fixing permissions on existing directory $PGDATA"
        chmod go-rwx "$PGDATA" || exit_nicely