I believe there is a bug, or at least a not very nice feature in initdb.

If initdb does not succeed, it attempts to "exit_nicely".   By default
this involved deleting the $PGDATA directory!!  So if you have put other
things in you $PGDATA directory or (as in my case) you attempt to create
a database in an existing directory that contains lots of stuff that you
really, really wanted to keep, and initdb fails, initdb very "nicely"
deletes them for you if it fails.

It seems like it would be a whole lot "nicer" if initdb only deleted the
files that it attempted to create OR if the default was not to delete
anything.

In any case, I have changed the default on my installation to NEVER
"clean up" for me.

Jeff Collins





Wed, 17 May 2000 15:19:47 -0400
Jeffery Collins <collins@onyx-technologies.com>
initdb and "exit_nicely"...
Jeffery Collins writes:

> It seems like it would be a whole lot "nicer" if initdb only deleted
> the files that it attempted to create OR if the default was not to
> delete anything.

Okay, I could go for the former. What do others think?

-- 
Peter Eisentraut                  Sernanders vï¿½g 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden


Thu, 18 May 2000 00:11:48 +0200 (CEST)
Peter Eisentraut <peter_e@gmx.net>
Re: initdb and "exit_nicely"...
Peter Eisentraut <peter_e@gmx.net> writes:
>> It seems like it would be a whole lot "nicer" if initdb only deleted
>> the files that it attempted to create OR if the default was not to
>> delete anything.

> Okay, I could go for the former. What do others think?

It'd be a bit of a pain but I can see the problem.  You certainly
shouldn't delete the $PGDATA directory itself unless you created it,
IMHO.  Doing more than that would imply that initdb's cleanup
function would have to know the list of files/subdirectories that
normally get created during initdb, so as to remove only those and
not anything else that might be lying around in the directory.
That'd be a considerable maintenance headache since most of said files
are not created directly by the script...

BTW, what about collisions?  Suppose the reason the initdb fails
is that there's already a (bogus) pg_log, or some such --- is
the script supposed to know not to delete it?  That strikes me
as way too difficult, since now the script has to know not only
which files get created but exactly when.

A slightly more reasonable example is where the admin has already
inserted his own pg_hba.conf in the directory; would be nice if initdb
didn't overwrite it (nor delete it on failure), but I'm not sure it's
worth the trouble.

Something that would be a lot simpler is to refuse to run at all
if the $PGDATA dir exists and is nonempty ;-)

			regards, tom lane

Wed, 17 May 2000 20:04:22 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: initdb and "exit_nicely"... 
> A slightly more reasonable example is where the admin has already
> inserted his own pg_hba.conf in the directory; would be nice if initdb
> didn't overwrite it (nor delete it on failure), but I'm not sure it's
> worth the trouble.

I am inclined to leave it as is too.  I can imagine many bug reports if
that directory is not flushed on failure.

-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Wed, 17 May 2000 22:28:39 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: initdb and "exit_nicely"...
Tom Lane wrote:

> Peter Eisentraut <peter_e@gmx.net> writes:
> >> It seems like it would be a whole lot "nicer" if initdb only deleted
> >> the files that it attempted to create OR if the default was not to
> >> delete anything.
>
> > Okay, I could go for the former. What do others think?
>
> It'd be a bit of a pain but I can see the problem.  You certainly
> shouldn't delete the $PGDATA directory itself unless you created it,
> IMHO.  Doing more than that would imply that initdb's cleanup
> function would have to know the list of files/subdirectories that
> normally get created during initdb, so as to remove only those and
> not anything else that might be lying around in the directory.
> That'd be a considerable maintenance headache since most of said files
> are not created directly by the script...
>

I agree this would be a pain.  After thinking about it, I think the best
approach is to do one or both of the following:

    - Change the default to not delete $PGDATA.
    - Prompt the user for confirmation before deleting $PGDATA.

Anything else sounds like a pain in the *ss and very error prone.

Jeff Collins



Thu, 18 May 2000 08:39:46 -0400
Jeffery Collins <collins@onyx-technologies.com>
Re: initdb and "exit_nicely"...
I wrote:

> Jeffery Collins writes:
> > It seems like it would be a whole lot "nicer" if initdb only deleted
> > the files that it attempted to create OR if the default was not to
> > delete anything.
> 
> Okay, I could go for the former. What do others think?

Here's a patch that might do what you need but I'm somewhat suspicious of
this situation. Recycling an old PGDATA directory is not supported, in
facts it's explicitly prevented with certain checks in initdb. So
apparently you precreated the data directory and put "interesting
things" of your own in it, which is not necessarily something that's
encouraged either.

It does make sense to leave the PGDATA directory and only clean out the
_contents_ on failure, that is, use `rm -rf $PGDATA/*' instead of `rm -rf
$PGDATA' but I doubt that that can be done portably.

-- 
Peter Eisentraut                  Sernanders v�g 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden
Fri, 19 May 2000 01:50:54 +0200 (CEST)
Peter Eisentraut <peter_e@gmx.net>
Re: initdb and "exit_nicely"...
Tom Lane writes:

> Something that would be a lot simpler is to refuse to run at all
> if the $PGDATA dir exists and is nonempty ;-)

Yes, why not? Initialize first, customize second. I thought initdb
provides a default pg_hba.conf file etc. so there's little to no reason
to do it the other way around.

-- 
Peter Eisentraut                  Sernanders vï¿½g 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden


Fri, 19 May 2000 01:58:04 +0200 (CEST)
Peter Eisentraut <peter_e@gmx.net>
Re: initdb and "exit_nicely"... 