strcmp(argv[1], "-?") == 0)
                {
                        usage();
-                       exit_nicely(false);
+                       exit(0);
                }
                if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
                {
                        pg_log(PG_REPORT, "pg_upgrade " PG_VERSION "\n");
-                       exit_nicely(false);
+                       exit(0);
                }
        }
 
                                if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL)
                                {
                                        pg_log(PG_FATAL, "cannot open debug file\n");
-                                       exit_nicely(false);
+                                       exit(1);
                                }
                                break;
 
                                if ((old_cluster.port = atoi(optarg)) <= 0)
                                {
                                        pg_log(PG_FATAL, "invalid old port number\n");
-                                       exit_nicely(false);
+                                       exit(1);
                                }
                                break;
 
                                if ((new_cluster.port = atoi(optarg)) <= 0)
                                {
                                        pg_log(PG_FATAL, "invalid new port number\n");
-                                       exit_nicely(false);
+                                       exit(1);
                                }
                                break;
 
 
  *
  *     Connects to the desired database on the designated server.
  *     If the connection attempt fails, this function logs an error
- *     message and calls exit_nicely() to kill the program.
+ *     message and calls exit() to kill the program.
  */
 PGconn *
 connectToServer(ClusterInfo *cluster, const char *db_name)
                if (conn)
                        PQfinish(conn);
 
-               exit_nicely(true);
+               printf("Failure, exiting\n");
+               exit(1);
        }
 
        return conn;
  *
  *     Formats a query string from the given arguments and executes the
  *     resulting query.  If the query fails, this function logs an error
- *     message and calls exit_nicely() to kill the program.
+ *     message and calls exit() to kill the program.
  */
 PGresult *
 executeQueryOrDie(PGconn *conn, const char *fmt,...)
                           PQerrorMessage(conn));
                PQclear(result);
                PQfinish(conn);
-               exit_nicely(true);
-               return NULL;                    /* Never get here, but keeps compiler happy */
+               printf("Failure, exiting\n");
+               exit(1);
        }
        else
                return result;
 }
 
 
+static void
+#ifdef HAVE_ATEXIT
+stop_postmaster_atexit(void)
+#else
+stop_postmaster_on_exit(int exitstatus, void *arg)
+#endif
+{
+       stop_postmaster(true, true);
+
+}
+
+
 void
 start_postmaster(ClusterInfo *cluster, bool quiet)
 {
        const char *bindir;
        const char *datadir;
        unsigned short port;
+       bool            exit_hook_registered = false;
 
        bindir = cluster->bindir;
        datadir = cluster->pgdata;
        port = cluster->port;
 
+       if (!exit_hook_registered)
+       {
+#ifdef HAVE_ATEXIT
+               atexit(stop_postmaster_atexit);
+#else
+               on_exit(stop_postmaster_on_exit);
+#endif
+               exit_hook_registered = true;
+       }
+
        /*
         * On Win32, we can't send both pg_upgrade output and pg_ctl output to the
         * same file because we get the error: "The process cannot access the file
 
                case PG_FATAL:
                        printf("%s", "\n");
                        printf("%s", _(message));
-                       exit_nicely(true);
+                       printf("Failure, exiting\n");
+                       exit(1);
                        break;
 
                case PG_DEBUG:
 }
 
 
-void
-exit_nicely(bool need_cleanup)
-{
-       stop_postmaster(true, true);
-
-       pg_free(log_opts.filename);
-
-       if (log_opts.fd)
-               fclose(log_opts.fd);
-
-       if (log_opts.debug_fd)
-               fclose(log_opts.debug_fd);
-
-       /* terminate any running instance of postmaster */
-       if (os_info.postmasterPID != 0)
-               kill(os_info.postmasterPID, SIGTERM);
-       
-       if (need_cleanup)
-       {
-               printf("Failure, exiting\n");
-               /*
-                * FIXME must delete intermediate files
-                */
-               exit(1);
-       }
-       else
-               exit(0);
-}
-
-
 void *
 pg_malloc(int n)
 {