-void
-DoChdirAndInitDatabaseNameAndPath(char *name, /* name of database */
- char *path) /* full path to database */
-{
- /* ----------------
- * check the path
- * ----------------
- */
- if (path)
- SetDatabasePath(path);
- else
- elog(FATAL, "DoChdirAndInitDatabaseNameAndPath: path:%s is not valid",
- path);
-
- /* ----------------
- * check the name
- * ----------------
- */
- if (name)
- SetDatabaseName(name);
- else
- elog(FATAL, "DoChdirAndInitDatabaseNameAndPath: name:%s is not valid",
- name);
-
- /* ----------------
- * change to the directory, or die trying.
- *
- * XXX unless the path hasn't been set because we're bootstrapping.
- * HP-UX doesn't like chdir("") so check for that case before
- * doing anything drastic.
- * ----------------
- */
- if (*path && (chdir(path) < 0))
- elog(FATAL, "DoChdirAndInitDatabaseNameAndPath: chdir(\"%s\"): %m",
- path);
+static void
+DoChdirAndInitDatabaseNameAndPath(char *name) {
+ char *reason;
+ /* Failure reason returned by some function. NULL if no failure */
+#ifndef WIN32
+ struct stat statbuf;
+#else
+ struct _stat statbuf;
+#endif
+ char errormsg[1000];
+
+ if (stat(DataDir, &statbuf) < 0)
+ sprintf(errormsg, "Database system does not exist. "
+ "PGDATA directory '%s' not found. Normally, you "
+ "create a database system by running initdb.",
+ DataDir);
+ else {
+ char myPath[MAXPGPATH]; /* DatabasePath points here! */
+
+ if (strlen(DataDir) + strlen(name) + 10 > sizeof(myPath))
+ sprintf(errormsg, "Internal error in postinit.c: database "
+ "pathname exceeds maximum allowable length.");
+ else {
+ sprintf(myPath, "%s/base/%s", DataDir, name);
+
+ if (stat(myPath, &statbuf) < 0)
+ sprintf(errormsg,
+ "Database '%s' does not exist. "
+ "(We know this because the directory '%s' "
+ "does not exist). You can create a database "
+ "with the SQL command CREATE DATABASE. To see "
+ "what databases exist, look at the subdirectories "
+ "of '%s/base/'.",
+ name, myPath, DataDir);
+ else {
+ ValidatePgVersion(DataDir, &reason);
+ if (reason != NULL)
+ sprintf(errormsg,
+ "InitPostgres could not validate that the database "
+ "system version is compatible with this level of "
+ "Postgres. You may need to run initdb to create "
+ "a new database system. %s",
+ reason);
+ else {
+ ValidatePgVersion(myPath, &reason);
+ if (reason != NULL)
+ sprintf(errormsg,
+ "InitPostgres could not validate that the "
+ "database version is compatible with this level "
+ "of Postgres, even though the database system "
+ "as a whole appears to be at a compatible level. "
+ "You may need to recreate the database with SQL "
+ "commands DROP DATABASE and CREATE DATABASE. "
+ "%s",
+ reason);
+ else {
+ /* The directories and PG_VERSION files are in order.*/
+ int rc; /* return code from some function we call */
+
+ SetDatabasePath(myPath);
+ SetDatabaseName(name);
+ rc = chdir(myPath);
+ if (rc < 0)
+ sprintf(errormsg,
+ "InitPostgres unable to change "
+ "current directory to '%s', errno = %s (%d).",
+ myPath, strerror(errno), errno);
+ else errormsg[0] = '\0';
+ }
+ }
+ }
+ }
+ }
+ if (errormsg[0] != '\0')
+ elog(FATAL, errormsg);
+ /* Above does not return */