Make LC_COLLATE and LC_CTYPE database-level settings. Collation and
[PostgreSQL.git] / src / bin / initdb / initdb.c
blob0b25d9ab69b316e9465fe6e9a4f80d8dd4ce03da
1 /*-------------------------------------------------------------------------
3 * initdb --- initialize a PostgreSQL installation
5 * initdb creates (initializes) a PostgreSQL database cluster (site,
6 * instance, installation, whatever). A database cluster is a
7 * collection of PostgreSQL databases all managed by the same server.
9 * To create the database cluster, we create the directory that contains
10 * all its data, create the files that hold the global tables, create
11 * a few other control files for it, and create three databases: the
12 * template databases "template0" and "template1", and a default user
13 * database "postgres".
15 * The template databases are ordinary PostgreSQL databases. template0
16 * is never supposed to change after initdb, whereas template1 can be
17 * changed to add site-local standard data. Either one can be copied
18 * to produce a new database.
20 * For largely-historical reasons, the template1 database is the one built
21 * by the basic bootstrap process. After it is complete, template0 and
22 * the default database, postgres, are made just by copying template1.
24 * To create template1, we run the postgres (backend) program in bootstrap
25 * mode and feed it data from the postgres.bki library file. After this
26 * initial bootstrap phase, some additional stuff is created by normal
27 * SQL commands fed to a standalone backend. Some of those commands are
28 * just embedded into this program (yeah, it's ugly), but larger chunks
29 * are taken from script files.
32 * Note:
33 * The program has some memory leakage - it isn't worth cleaning it up.
35 * This is a C implementation of the previous shell script for setting up a
36 * PostgreSQL cluster location, and should be highly compatible with it.
37 * author of C translation: Andrew Dunstan mailto:andrew@dunslane.net
39 * This code is released under the terms of the PostgreSQL License.
41 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
42 * Portions Copyright (c) 1994, Regents of the University of California
43 * Portions taken from FreeBSD.
45 * $PostgreSQL$
47 *-------------------------------------------------------------------------
50 #include "postgres_fe.h"
52 #include <dirent.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <locale.h>
56 #include <signal.h>
57 #include <time.h>
59 #include "libpq/pqsignal.h"
60 #include "mb/pg_wchar.h"
61 #include "getaddrinfo.h"
62 #include "getopt_long.h"
63 #include "miscadmin.h"
65 #ifndef HAVE_INT_OPTRESET
66 int optreset;
67 #endif
71 * these values are passed in by makefile defines
73 static char *share_path = NULL;
75 /* values to be obtained from arguments */
76 static char *pg_data = "";
77 static char *encoding = "";
78 static char *locale = "";
79 static char *lc_collate = "";
80 static char *lc_ctype = "";
81 static char *lc_monetary = "";
82 static char *lc_numeric = "";
83 static char *lc_time = "";
84 static char *lc_messages = "";
85 static const char *default_text_search_config = "";
86 static char *username = "";
87 static bool pwprompt = false;
88 static char *pwfilename = NULL;
89 static char *authmethod = "";
90 static bool debug = false;
91 static bool noclean = false;
92 static bool show_setting = false;
93 static char *xlog_dir = "";
96 /* internal vars */
97 static const char *progname;
98 static char *encodingid = "0";
99 static char *bki_file;
100 static char *desc_file;
101 static char *shdesc_file;
102 static char *hba_file;
103 static char *ident_file;
104 static char *conf_file;
105 static char *conversion_file;
106 static char *dictionary_file;
107 static char *info_schema_file;
108 static char *features_file;
109 static char *system_views_file;
110 static bool made_new_pgdata = false;
111 static bool found_existing_pgdata = false;
112 static bool made_new_xlogdir = false;
113 static bool found_existing_xlogdir = false;
114 static char infoversion[100];
115 static bool caught_signal = false;
116 static bool output_failed = false;
117 static int output_errno = 0;
119 /* defaults */
120 static int n_connections = 10;
121 static int n_buffers = 50;
122 static int n_fsm_pages = 20000;
125 * Warning messages for authentication methods
127 #define AUTHTRUST_WARNING \
128 "# CAUTION: Configuring the system for local \"trust\" authentication allows\n" \
129 "# any local user to connect as any PostgreSQL user, including the database\n" \
130 "# superuser. If you do not trust all your local users, use another\n" \
131 "# authentication method.\n"
132 static char *authwarning = NULL;
135 * Centralized knowledge of switches to pass to backend
137 * Note: in the shell-script version, we also passed PGDATA as a -D switch,
138 * but here it is more convenient to pass it as an environment variable
139 * (no quoting to worry about).
141 static const char *boot_options = "-F";
142 static const char *backend_options = "--single -F -O -c search_path=pg_catalog -c exit_on_error=true";
145 /* path to 'initdb' binary directory */
146 static char bin_path[MAXPGPATH];
147 static char backend_exec[MAXPGPATH];
149 static void *pg_malloc(size_t size);
150 static char *xstrdup(const char *s);
151 static char **replace_token(char **lines,
152 const char *token, const char *replacement);
154 #ifndef HAVE_UNIX_SOCKETS
155 static char **filter_lines_with_token(char **lines, const char *token);
156 #endif
157 static char **readfile(char *path);
158 static void writefile(char *path, char **lines);
159 static FILE *popen_check(const char *command, const char *mode);
160 static int mkdir_p(char *path, mode_t omode);
161 static void exit_nicely(void);
162 static char *get_id(void);
163 static char *get_encoding_id(char *encoding_name);
164 static char *get_short_version(void);
165 static int check_data_dir(char *dir);
166 static bool mkdatadir(const char *subdir);
167 static void set_input(char **dest, char *filename);
168 static void check_input(char *path);
169 static void set_short_version(char *short_version, char *extrapath);
170 static void set_null_conf(void);
171 static void test_config_settings(void);
172 static void setup_config(void);
173 static void bootstrap_template1(char *short_version);
174 static void setup_auth(void);
175 static void get_set_pwd(void);
176 static void setup_depend(void);
177 static void setup_sysviews(void);
178 static void setup_description(void);
179 static void setup_conversion(void);
180 static void setup_dictionary(void);
181 static void setup_privileges(void);
182 static void set_info_version(void);
183 static void setup_schema(void);
184 static void vacuum_db(void);
185 static void make_template0(void);
186 static void make_postgres(void);
187 static void trapsig(int signum);
188 static void check_ok(void);
189 static char *escape_quotes(const char *src);
190 static int locale_date_order(const char *locale);
191 static bool chklocale(const char *locale);
192 static void setlocales(void);
193 static void usage(const char *progname);
195 #ifdef WIN32
196 static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo);
197 #endif
201 * macros for running pipes to postgres
203 #define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd
205 #define PG_CMD_OPEN \
206 do { \
207 cmdfd = popen_check(cmd, "w"); \
208 if (cmdfd == NULL) \
209 exit_nicely(); /* message already printed by popen_check */ \
210 } while (0)
212 #define PG_CMD_CLOSE \
213 do { \
214 if (pclose_check(cmdfd)) \
215 exit_nicely(); /* message already printed by pclose_check */ \
216 } while (0)
218 #define PG_CMD_PUTS(line) \
219 do { \
220 if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
221 output_failed = true, output_errno = errno; \
222 } while (0)
224 #define PG_CMD_PRINTF1(fmt, arg1) \
225 do { \
226 if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
227 output_failed = true, output_errno = errno; \
228 } while (0)
230 #define PG_CMD_PRINTF2(fmt, arg1, arg2) \
231 do { \
232 if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
233 output_failed = true, output_errno = errno; \
234 } while (0)
236 #ifndef WIN32
237 #define QUOTE_PATH ""
238 #define DIR_SEP "/"
239 #else
240 #define QUOTE_PATH "\""
241 #define DIR_SEP "\\"
242 #endif
245 * routines to check mem allocations and fail noisily.
247 * Note that we can't call exit_nicely() on a memory failure, as it calls
248 * rmtree() which needs memory allocation. So we just exit with a bang.
250 static void *
251 pg_malloc(size_t size)
253 void *result;
255 result = malloc(size);
256 if (!result)
258 fprintf(stderr, _("%s: out of memory\n"), progname);
259 exit(1);
261 return result;
264 static char *
265 xstrdup(const char *s)
267 char *result;
269 result = strdup(s);
270 if (!result)
272 fprintf(stderr, _("%s: out of memory\n"), progname);
273 exit(1);
275 return result;
279 * make a copy of the array of lines, with token replaced by replacement
280 * the first time it occurs on each line.
282 * This does most of what sed was used for in the shell script, but
283 * doesn't need any regexp stuff.
285 static char **
286 replace_token(char **lines, const char *token, const char *replacement)
288 int numlines = 1;
289 int i;
290 char **result;
291 int toklen,
292 replen,
293 diff;
295 for (i = 0; lines[i]; i++)
296 numlines++;
298 result = (char **) pg_malloc(numlines * sizeof(char *));
300 toklen = strlen(token);
301 replen = strlen(replacement);
302 diff = replen - toklen;
304 for (i = 0; i < numlines; i++)
306 char *where;
307 char *newline;
308 int pre;
310 /* just copy pointer if NULL or no change needed */
311 if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
313 result[i] = lines[i];
314 continue;
317 /* if we get here a change is needed - set up new line */
319 newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
321 pre = where - lines[i];
323 strncpy(newline, lines[i], pre);
325 strcpy(newline + pre, replacement);
327 strcpy(newline + pre + replen, lines[i] + pre + toklen);
329 result[i] = newline;
332 return result;
336 * make a copy of lines without any that contain the token
338 * a sort of poor man's grep -v
340 #ifndef HAVE_UNIX_SOCKETS
341 static char **
342 filter_lines_with_token(char **lines, const char *token)
344 int numlines = 1;
345 int i,
346 src,
347 dst;
348 char **result;
350 for (i = 0; lines[i]; i++)
351 numlines++;
353 result = (char **) pg_malloc(numlines * sizeof(char *));
355 for (src = 0, dst = 0; src < numlines; src++)
357 if (lines[src] == NULL || strstr(lines[src], token) == NULL)
358 result[dst++] = lines[src];
361 return result;
363 #endif
366 * get the lines from a text file
368 static char **
369 readfile(char *path)
371 FILE *infile;
372 int maxlength = 0,
373 linelen = 0;
374 int nlines = 0;
375 char **result;
376 char *buffer;
377 int c;
379 if ((infile = fopen(path, "r")) == NULL)
381 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
382 progname, path, strerror(errno));
383 exit_nicely();
386 /* pass over the file twice - the first time to size the result */
388 while ((c = fgetc(infile)) != EOF)
390 linelen++;
391 if (c == '\n')
393 nlines++;
394 if (linelen > maxlength)
395 maxlength = linelen;
396 linelen = 0;
400 /* handle last line without a terminating newline (yuck) */
402 if (linelen)
403 nlines++;
404 if (linelen > maxlength)
405 maxlength = linelen;
407 /* set up the result and the line buffer */
409 result = (char **) pg_malloc((nlines + 2) * sizeof(char *));
410 buffer = (char *) pg_malloc(maxlength + 2);
412 /* now reprocess the file and store the lines */
414 rewind(infile);
415 nlines = 0;
416 while (fgets(buffer, maxlength + 1, infile) != NULL)
418 result[nlines] = xstrdup(buffer);
419 nlines++;
422 fclose(infile);
423 free(buffer);
424 result[nlines] = NULL;
426 return result;
430 * write an array of lines to a file
432 * This is only used to write text files. Use fopen "w" not PG_BINARY_W
433 * so that the resulting configuration files are nicely editable on Windows.
435 static void
436 writefile(char *path, char **lines)
438 FILE *out_file;
439 char **line;
441 if ((out_file = fopen(path, "w")) == NULL)
443 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
444 progname, path, strerror(errno));
445 exit_nicely();
447 for (line = lines; *line != NULL; line++)
449 if (fputs(*line, out_file) < 0)
451 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
452 progname, path, strerror(errno));
453 exit_nicely();
455 free(*line);
457 if (fclose(out_file))
459 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
460 progname, path, strerror(errno));
461 exit_nicely();
466 * Open a subcommand with suitable error messaging
468 static FILE *
469 popen_check(const char *command, const char *mode)
471 FILE *cmdfd;
473 fflush(stdout);
474 fflush(stderr);
475 errno = 0;
476 cmdfd = popen(command, mode);
477 if (cmdfd == NULL)
478 fprintf(stderr, _("%s: could not execute command \"%s\": %s\n"),
479 progname, command, strerror(errno));
480 return cmdfd;
483 /* source stolen from FreeBSD /src/bin/mkdir/mkdir.c and adapted */
486 * this tries to build all the elements of a path to a directory a la mkdir -p
487 * we assume the path is in canonical form, i.e. uses / as the separator
488 * we also assume it isn't null.
490 * note that on failure, the path arg has been modified to show the particular
491 * directory level we had problems with.
493 static int
494 mkdir_p(char *path, mode_t omode)
496 struct stat sb;
497 mode_t numask,
498 oumask;
499 int first,
500 last,
501 retval;
502 char *p;
504 p = path;
505 oumask = 0;
506 retval = 0;
508 #ifdef WIN32
509 /* skip network and drive specifiers for win32 */
510 if (strlen(p) >= 2)
512 if (p[0] == '/' && p[1] == '/')
514 /* network drive */
515 p = strstr(p + 2, "/");
516 if (p == NULL)
517 return 1;
519 else if (p[1] == ':' &&
520 ((p[0] >= 'a' && p[0] <= 'z') ||
521 (p[0] >= 'A' && p[0] <= 'Z')))
523 /* local drive */
524 p += 2;
527 #endif
529 if (p[0] == '/') /* Skip leading '/'. */
530 ++p;
531 for (first = 1, last = 0; !last; ++p)
533 if (p[0] == '\0')
534 last = 1;
535 else if (p[0] != '/')
536 continue;
537 *p = '\0';
538 if (!last && p[1] == '\0')
539 last = 1;
540 if (first)
543 * POSIX 1003.2: For each dir operand that does not name an
544 * existing directory, effects equivalent to those caused by the
545 * following command shall occcur:
547 * mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
548 * dir
550 * We change the user's umask and then restore it, instead of
551 * doing chmod's.
553 oumask = umask(0);
554 numask = oumask & ~(S_IWUSR | S_IXUSR);
555 (void) umask(numask);
556 first = 0;
558 if (last)
559 (void) umask(oumask);
561 /* check for pre-existing directory; ok if it's a parent */
562 if (stat(path, &sb) == 0)
564 if (!S_ISDIR(sb.st_mode))
566 if (last)
567 errno = EEXIST;
568 else
569 errno = ENOTDIR;
570 retval = 1;
571 break;
574 else if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
576 retval = 1;
577 break;
579 if (!last)
580 *p = '/';
582 if (!first && !last)
583 (void) umask(oumask);
584 return retval;
588 * clean up any files we created on failure
589 * if we created the data directory remove it too
591 static void
592 exit_nicely(void)
594 if (!noclean)
596 if (made_new_pgdata)
598 fprintf(stderr, _("%s: removing data directory \"%s\"\n"),
599 progname, pg_data);
600 if (!rmtree(pg_data, true))
601 fprintf(stderr, _("%s: failed to remove data directory\n"),
602 progname);
604 else if (found_existing_pgdata)
606 fprintf(stderr,
607 _("%s: removing contents of data directory \"%s\"\n"),
608 progname, pg_data);
609 if (!rmtree(pg_data, false))
610 fprintf(stderr, _("%s: failed to remove contents of data directory\n"),
611 progname);
614 if (made_new_xlogdir)
616 fprintf(stderr, _("%s: removing transaction log directory \"%s\"\n"),
617 progname, xlog_dir);
618 if (!rmtree(xlog_dir, true))
619 fprintf(stderr, _("%s: failed to remove transaction log directory\n"),
620 progname);
622 else if (found_existing_xlogdir)
624 fprintf(stderr,
625 _("%s: removing contents of transaction log directory \"%s\"\n"),
626 progname, xlog_dir);
627 if (!rmtree(xlog_dir, false))
628 fprintf(stderr, _("%s: failed to remove contents of transaction log directory\n"),
629 progname);
631 /* otherwise died during startup, do nothing! */
633 else
635 if (made_new_pgdata || found_existing_pgdata)
636 fprintf(stderr,
637 _("%s: data directory \"%s\" not removed at user's request\n"),
638 progname, pg_data);
640 if (made_new_xlogdir || found_existing_xlogdir)
641 fprintf(stderr,
642 _("%s: transaction log directory \"%s\" not removed at user's request\n"),
643 progname, xlog_dir);
646 exit(1);
650 * find the current user
652 * on unix make sure it isn't really root
654 static char *
655 get_id(void)
657 #ifndef WIN32
659 struct passwd *pw;
661 pw = getpwuid(geteuid());
663 if (geteuid() == 0) /* 0 is root's uid */
665 fprintf(stderr,
666 _("%s: cannot be run as root\n"
667 "Please log in (using, e.g., \"su\") as the "
668 "(unprivileged) user that will\n"
669 "own the server process.\n"),
670 progname);
671 exit(1);
673 #else /* the windows code */
675 struct passwd_win32
677 int pw_uid;
678 char pw_name[128];
679 } pass_win32;
680 struct passwd_win32 *pw = &pass_win32;
681 DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
683 pw->pw_uid = 1;
684 GetUserName(pw->pw_name, &pwname_size);
685 #endif
687 return xstrdup(pw->pw_name);
690 static char *
691 encodingid_to_string(int enc)
693 char result[20];
695 sprintf(result, "%d", enc);
696 return xstrdup(result);
700 * get the encoding id for a given encoding name
702 static char *
703 get_encoding_id(char *encoding_name)
705 int enc;
707 if (encoding_name && *encoding_name)
709 if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
710 return encodingid_to_string(enc);
712 fprintf(stderr, _("%s: \"%s\" is not a valid server encoding name\n"),
713 progname, encoding_name ? encoding_name : "(null)");
714 exit(1);
718 * Support for determining the best default text search configuration.
719 * We key this off the first part of LC_CTYPE (ie, the language name).
721 struct tsearch_config_match
723 const char *tsconfname;
724 const char *langname;
727 static const struct tsearch_config_match tsearch_config_languages[] =
729 {"danish", "da"},
730 {"danish", "Danish"},
731 {"dutch", "nl"},
732 {"dutch", "Dutch"},
733 {"english", "C"},
734 {"english", "POSIX"},
735 {"english", "en"},
736 {"english", "English"},
737 {"finnish", "fi"},
738 {"finnish", "Finnish"},
739 {"french", "fr"},
740 {"french", "French"},
741 {"german", "de"},
742 {"german", "German"},
743 {"hungarian", "hu"},
744 {"hungarian", "Hungarian"},
745 {"italian", "it"},
746 {"italian", "Italian"},
747 {"norwegian", "no"},
748 {"norwegian", "Norwegian"},
749 {"portuguese", "pt"},
750 {"portuguese", "Portuguese"},
751 {"romanian", "ro"},
752 {"russian", "ru"},
753 {"russian", "Russian"},
754 {"spanish", "es"},
755 {"spanish", "Spanish"},
756 {"swedish", "sv"},
757 {"swedish", "Swedish"},
758 {"turkish", "tr"},
759 {"turkish", "Turkish"},
760 {NULL, NULL} /* end marker */
764 * Look for a text search configuration matching lc_ctype, and return its
765 * name; return NULL if no match.
767 static const char *
768 find_matching_ts_config(const char *lc_type)
770 int i;
771 char *langname,
772 *ptr;
775 * Convert lc_ctype to a language name by stripping everything after an
776 * underscore. Just for paranoia, we also stop at '.' or '@'.
778 if (lc_type == NULL)
779 langname = xstrdup("");
780 else
782 ptr = langname = xstrdup(lc_type);
783 while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@')
784 ptr++;
785 *ptr = '\0';
788 for (i = 0; tsearch_config_languages[i].tsconfname; i++)
790 if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
792 free(langname);
793 return tsearch_config_languages[i].tsconfname;
797 free(langname);
798 return NULL;
803 * get short version of VERSION
805 static char *
806 get_short_version(void)
808 bool gotdot = false;
809 int end;
810 char *vr;
812 vr = xstrdup(PG_VERSION);
814 for (end = 0; vr[end] != '\0'; end++)
816 if (vr[end] == '.')
818 if (end == 0)
819 return NULL;
820 else if (gotdot)
821 break;
822 else
823 gotdot = true;
825 else if (vr[end] < '0' || vr[end] > '9')
827 /* gone past digits and dots */
828 break;
831 if (end == 0 || vr[end - 1] == '.' || !gotdot)
832 return NULL;
834 vr[end] = '\0';
835 return vr;
839 * make sure the directory either doesn't exist or is empty
841 * Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
842 * or -1 if trouble accessing directory
844 static int
845 check_data_dir(char *dir)
847 DIR *chkdir;
848 struct dirent *file;
849 int result = 1;
851 errno = 0;
853 chkdir = opendir(dir);
855 if (!chkdir)
856 return (errno == ENOENT) ? 0 : -1;
858 while ((file = readdir(chkdir)) != NULL)
860 if (strcmp(".", file->d_name) == 0 ||
861 strcmp("..", file->d_name) == 0)
863 /* skip this and parent directory */
864 continue;
866 else
868 result = 2; /* not empty */
869 break;
873 #ifdef WIN32
876 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
877 * released version
879 if (GetLastError() == ERROR_NO_MORE_FILES)
880 errno = 0;
881 #endif
883 closedir(chkdir);
885 if (errno != 0)
886 result = -1; /* some kind of I/O error? */
888 return result;
892 * make the data directory (or one of its subdirectories if subdir is not NULL)
894 static bool
895 mkdatadir(const char *subdir)
897 char *path;
899 path = pg_malloc(strlen(pg_data) + 2 +
900 (subdir == NULL ? 0 : strlen(subdir)));
902 if (subdir != NULL)
903 sprintf(path, "%s/%s", pg_data, subdir);
904 else
905 strcpy(path, pg_data);
907 if (mkdir_p(path, 0700) == 0)
908 return true;
910 fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
911 progname, path, strerror(errno));
913 return false;
918 * set name of given input file variable under data directory
920 static void
921 set_input(char **dest, char *filename)
923 *dest = pg_malloc(strlen(share_path) + strlen(filename) + 2);
924 sprintf(*dest, "%s/%s", share_path, filename);
928 * check that given input file exists
930 static void
931 check_input(char *path)
933 struct stat statbuf;
935 if (stat(path, &statbuf) != 0)
937 if (errno == ENOENT)
939 fprintf(stderr,
940 _("%s: file \"%s\" does not exist\n"), progname, path);
941 fprintf(stderr,
942 _("This might mean you have a corrupted installation or identified\n"
943 "the wrong directory with the invocation option -L.\n"));
945 else
947 fprintf(stderr,
948 _("%s: could not access file \"%s\": %s\n"), progname, path,
949 strerror(errno));
950 fprintf(stderr,
951 _("This might mean you have a corrupted installation or identified\n"
952 "the wrong directory with the invocation option -L.\n"));
954 exit(1);
956 if (!S_ISREG(statbuf.st_mode))
958 fprintf(stderr,
959 _("%s: file \"%s\" is not a regular file\n"), progname, path);
960 fprintf(stderr,
961 _("This might mean you have a corrupted installation or identified\n"
962 "the wrong directory with the invocation option -L.\n"));
963 exit(1);
968 * write out the PG_VERSION file in the data dir, or its subdirectory
969 * if extrapath is not NULL
971 static void
972 set_short_version(char *short_version, char *extrapath)
974 FILE *version_file;
975 char *path;
977 if (extrapath == NULL)
979 path = pg_malloc(strlen(pg_data) + 12);
980 sprintf(path, "%s/PG_VERSION", pg_data);
982 else
984 path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13);
985 sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath);
987 version_file = fopen(path, PG_BINARY_W);
988 if (version_file == NULL)
990 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
991 progname, path, strerror(errno));
992 exit_nicely();
994 if (fprintf(version_file, "%s\n", short_version) < 0 ||
995 fclose(version_file))
997 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
998 progname, path, strerror(errno));
999 exit_nicely();
1001 free(path);
1005 * set up an empty config file so we can check config settings by launching
1006 * a test backend
1008 static void
1009 set_null_conf(void)
1011 FILE *conf_file;
1012 char *path;
1014 path = pg_malloc(strlen(pg_data) + 17);
1015 sprintf(path, "%s/postgresql.conf", pg_data);
1016 conf_file = fopen(path, PG_BINARY_W);
1017 if (conf_file == NULL)
1019 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1020 progname, path, strerror(errno));
1021 exit_nicely();
1023 if (fclose(conf_file))
1025 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1026 progname, path, strerror(errno));
1027 exit_nicely();
1029 free(path);
1033 * Determine platform-specific config settings
1035 * Use reasonable values if kernel will let us, else scale back. Probe
1036 * for max_connections first since it is subject to more constraints than
1037 * shared_buffers.
1039 static void
1040 test_config_settings(void)
1043 * These macros define the minimum shared_buffers we want for a given
1044 * max_connections value, and the max_fsm_pages setting to be used for a
1045 * given shared_buffers value. The arrays show the settings to try.
1048 #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
1049 #define FSM_FOR_BUFS(nbuffers) ((nbuffers) > 1000 ? 50 * (nbuffers) : 20000)
1051 static const int trial_conns[] = {
1052 100, 50, 40, 30, 20, 10
1054 static const int trial_bufs[] = {
1055 4096, 3584, 3072, 2560, 2048, 1536,
1056 1000, 900, 800, 700, 600, 500,
1057 400, 300, 200, 100, 50
1060 char cmd[MAXPGPATH];
1061 const int connslen = sizeof(trial_conns) / sizeof(int);
1062 const int bufslen = sizeof(trial_bufs) / sizeof(int);
1063 int i,
1064 status,
1065 test_conns,
1066 test_buffs,
1067 test_max_fsm,
1068 ok_buffers = 0;
1071 printf(_("selecting default max_connections ... "));
1072 fflush(stdout);
1074 for (i = 0; i < connslen; i++)
1076 test_conns = trial_conns[i];
1077 test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
1078 test_max_fsm = FSM_FOR_BUFS(test_buffs);
1080 snprintf(cmd, sizeof(cmd),
1081 SYSTEMQUOTE "\"%s\" --boot -x0 %s "
1082 "-c max_connections=%d "
1083 "-c shared_buffers=%d "
1084 "-c max_fsm_pages=%d "
1085 "< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
1086 backend_exec, boot_options,
1087 test_conns, test_buffs, test_max_fsm,
1088 DEVNULL, DEVNULL);
1089 status = system(cmd);
1090 if (status == 0)
1092 ok_buffers = test_buffs;
1093 break;
1096 if (i >= connslen)
1097 i = connslen - 1;
1098 n_connections = trial_conns[i];
1100 printf("%d\n", n_connections);
1102 printf(_("selecting default shared_buffers/max_fsm_pages ... "));
1103 fflush(stdout);
1105 for (i = 0; i < bufslen; i++)
1107 /* Use same amount of memory, independent of BLCKSZ */
1108 test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
1109 if (test_buffs <= ok_buffers)
1111 test_buffs = ok_buffers;
1112 break;
1114 test_max_fsm = FSM_FOR_BUFS(test_buffs);
1116 snprintf(cmd, sizeof(cmd),
1117 SYSTEMQUOTE "\"%s\" --boot -x0 %s "
1118 "-c max_connections=%d "
1119 "-c shared_buffers=%d "
1120 "-c max_fsm_pages=%d "
1121 "< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
1122 backend_exec, boot_options,
1123 n_connections, test_buffs, test_max_fsm,
1124 DEVNULL, DEVNULL);
1125 status = system(cmd);
1126 if (status == 0)
1127 break;
1129 n_buffers = test_buffs;
1130 n_fsm_pages = FSM_FOR_BUFS(n_buffers);
1132 if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1133 printf("%dMB/%d\n", (n_buffers * (BLCKSZ / 1024)) / 1024, n_fsm_pages);
1134 else
1135 printf("%dkB/%d\n", n_buffers * (BLCKSZ / 1024), n_fsm_pages);
1139 * set up all the config files
1141 static void
1142 setup_config(void)
1144 char **conflines;
1145 char repltok[100];
1146 char path[MAXPGPATH];
1148 fputs(_("creating configuration files ... "), stdout);
1149 fflush(stdout);
1151 /* postgresql.conf */
1153 conflines = readfile(conf_file);
1155 snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
1156 conflines = replace_token(conflines, "#max_connections = 100", repltok);
1158 if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1159 snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
1160 (n_buffers * (BLCKSZ / 1024)) / 1024);
1161 else
1162 snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
1163 n_buffers * (BLCKSZ / 1024));
1164 conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
1166 snprintf(repltok, sizeof(repltok), "max_fsm_pages = %d", n_fsm_pages);
1167 conflines = replace_token(conflines, "#max_fsm_pages = 204800", repltok);
1169 #if DEF_PGPORT != 5432
1170 snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
1171 conflines = replace_token(conflines, "#port = 5432", repltok);
1172 #endif
1174 snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
1175 escape_quotes(lc_messages));
1176 conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
1178 snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
1179 escape_quotes(lc_monetary));
1180 conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
1182 snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
1183 escape_quotes(lc_numeric));
1184 conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
1186 snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
1187 escape_quotes(lc_time));
1188 conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
1190 switch (locale_date_order(lc_time))
1192 case DATEORDER_YMD:
1193 strcpy(repltok, "datestyle = 'iso, ymd'");
1194 break;
1195 case DATEORDER_DMY:
1196 strcpy(repltok, "datestyle = 'iso, dmy'");
1197 break;
1198 case DATEORDER_MDY:
1199 default:
1200 strcpy(repltok, "datestyle = 'iso, mdy'");
1201 break;
1203 conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
1205 snprintf(repltok, sizeof(repltok),
1206 "default_text_search_config = 'pg_catalog.%s'",
1207 escape_quotes(default_text_search_config));
1208 conflines = replace_token(conflines,
1209 "#default_text_search_config = 'pg_catalog.simple'",
1210 repltok);
1212 snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1214 writefile(path, conflines);
1215 chmod(path, 0600);
1217 free(conflines);
1220 /* pg_hba.conf */
1222 conflines = readfile(hba_file);
1224 #ifndef HAVE_UNIX_SOCKETS
1225 conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
1226 #else
1227 conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
1228 #endif
1230 #ifdef HAVE_IPV6
1233 * Probe to see if there is really any platform support for IPv6, and
1234 * comment out the relevant pg_hba line if not. This avoids runtime
1235 * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1236 * useful on Windows, where executables built on a machine with IPv6 may
1237 * have to run on a machine without.
1240 struct addrinfo *gai_result;
1241 struct addrinfo hints;
1242 int err = 0;
1244 #ifdef WIN32
1245 /* need to call WSAStartup before calling getaddrinfo */
1246 WSADATA wsaData;
1248 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1249 #endif
1251 /* for best results, this code should match parse_hba() */
1252 hints.ai_flags = AI_NUMERICHOST;
1253 hints.ai_family = PF_UNSPEC;
1254 hints.ai_socktype = 0;
1255 hints.ai_protocol = 0;
1256 hints.ai_addrlen = 0;
1257 hints.ai_canonname = NULL;
1258 hints.ai_addr = NULL;
1259 hints.ai_next = NULL;
1261 if (err != 0 ||
1262 getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1263 conflines = replace_token(conflines,
1264 "host all all ::1",
1265 "#host all all ::1");
1267 #else /* !HAVE_IPV6 */
1268 /* If we didn't compile IPV6 support at all, always comment it out */
1269 conflines = replace_token(conflines,
1270 "host all all ::1",
1271 "#host all all ::1");
1272 #endif /* HAVE_IPV6 */
1274 /* Replace default authentication methods */
1275 conflines = replace_token(conflines,
1276 "@authmethod@",
1277 authmethod);
1279 conflines = replace_token(conflines,
1280 "@authcomment@",
1281 strcmp(authmethod, "trust") ? "" : AUTHTRUST_WARNING);
1283 snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1285 writefile(path, conflines);
1286 chmod(path, 0600);
1288 free(conflines);
1290 /* pg_ident.conf */
1292 conflines = readfile(ident_file);
1294 snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1296 writefile(path, conflines);
1297 chmod(path, 0600);
1299 free(conflines);
1301 check_ok();
1306 * run the BKI script in bootstrap mode to create template1
1308 static void
1309 bootstrap_template1(char *short_version)
1311 PG_CMD_DECL;
1312 char **line;
1313 char *talkargs = "";
1314 char **bki_lines;
1315 char headerline[MAXPGPATH];
1316 char buf[64];
1318 printf(_("creating template1 database in %s/base/1 ... "), pg_data);
1319 fflush(stdout);
1321 if (debug)
1322 talkargs = "-d 5";
1324 bki_lines = readfile(bki_file);
1326 /* Check that bki file appears to be of the right version */
1328 snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1329 short_version);
1331 if (strcmp(headerline, *bki_lines) != 0)
1333 fprintf(stderr,
1334 _("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
1335 "Check your installation or specify the correct path "
1336 "using the option -L.\n"),
1337 progname, bki_file, PG_VERSION);
1338 exit_nicely();
1341 /* Substitute for various symbols used in the BKI file */
1343 sprintf(buf, "%d", NAMEDATALEN);
1344 bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1346 bki_lines = replace_token(bki_lines, "FLOAT4PASSBYVAL",
1347 FLOAT4PASSBYVAL ? "true" : "false");
1349 bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1350 FLOAT8PASSBYVAL ? "true" : "false");
1352 bki_lines = replace_token(bki_lines, "POSTGRES", username);
1354 bki_lines = replace_token(bki_lines, "ENCODING", encodingid);
1356 bki_lines = replace_token(bki_lines, "LC_COLLATE", lc_collate);
1358 bki_lines = replace_token(bki_lines, "LC_CTYPE", lc_ctype);
1361 * Pass correct LC_xxx environment to bootstrap.
1363 * The shell script arranged to restore the LC settings afterwards, but
1364 * there doesn't seem to be any compelling reason to do that.
1366 snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate);
1367 putenv(xstrdup(cmd));
1369 snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
1370 putenv(xstrdup(cmd));
1372 unsetenv("LC_ALL");
1374 /* Also ensure backend isn't confused by this environment var: */
1375 unsetenv("PGCLIENTENCODING");
1377 snprintf(cmd, sizeof(cmd),
1378 "\"%s\" --boot -x1 %s %s",
1379 backend_exec, boot_options, talkargs);
1381 PG_CMD_OPEN;
1383 for (line = bki_lines; *line != NULL; line++)
1385 PG_CMD_PUTS(*line);
1386 free(*line);
1389 PG_CMD_CLOSE;
1391 free(bki_lines);
1393 check_ok();
1397 * set up the shadow password table
1399 static void
1400 setup_auth(void)
1402 PG_CMD_DECL;
1403 const char **line;
1404 static const char *pg_authid_setup[] = {
1406 * Create triggers to ensure manual updates to shared catalogs will be
1407 * reflected into their "flat file" copies.
1409 "CREATE TRIGGER pg_sync_pg_database "
1410 " AFTER INSERT OR UPDATE OR DELETE ON pg_database "
1411 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1412 "CREATE TRIGGER pg_sync_pg_authid "
1413 " AFTER INSERT OR UPDATE OR DELETE ON pg_authid "
1414 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1415 "CREATE TRIGGER pg_sync_pg_auth_members "
1416 " AFTER INSERT OR UPDATE OR DELETE ON pg_auth_members "
1417 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1420 * The authid table shouldn't be readable except through views, to
1421 * ensure passwords are not publicly visible.
1423 "REVOKE ALL on pg_authid FROM public;\n",
1424 NULL
1427 fputs(_("initializing pg_authid ... "), stdout);
1428 fflush(stdout);
1430 snprintf(cmd, sizeof(cmd),
1431 "\"%s\" %s template1 >%s",
1432 backend_exec, backend_options,
1433 DEVNULL);
1435 PG_CMD_OPEN;
1437 for (line = pg_authid_setup; *line != NULL; line++)
1438 PG_CMD_PUTS(*line);
1440 PG_CMD_CLOSE;
1442 check_ok();
1446 * get the superuser password if required, and call postgres to set it
1448 static void
1449 get_set_pwd(void)
1451 PG_CMD_DECL;
1453 char *pwd1,
1454 *pwd2;
1455 char pwdpath[MAXPGPATH];
1456 struct stat statbuf;
1458 if (pwprompt)
1461 * Read password from terminal
1463 pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
1464 pwd2 = simple_prompt("Enter it again: ", 100, false);
1465 if (strcmp(pwd1, pwd2) != 0)
1467 fprintf(stderr, _("Passwords didn't match.\n"));
1468 exit_nicely();
1470 free(pwd2);
1472 else
1475 * Read password from file
1477 * Ideally this should insist that the file not be world-readable.
1478 * However, this option is mainly intended for use on Windows where
1479 * file permissions may not exist at all, so we'll skip the paranoia
1480 * for now.
1482 FILE *pwf = fopen(pwfilename, "r");
1483 char pwdbuf[MAXPGPATH];
1484 int i;
1486 if (!pwf)
1488 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1489 progname, pwfilename, strerror(errno));
1490 exit_nicely();
1492 if (!fgets(pwdbuf, sizeof(pwdbuf), pwf))
1494 fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
1495 progname, pwfilename, strerror(errno));
1496 exit_nicely();
1498 fclose(pwf);
1500 i = strlen(pwdbuf);
1501 while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
1502 pwdbuf[--i] = '\0';
1504 pwd1 = xstrdup(pwdbuf);
1507 printf(_("setting password ... "));
1508 fflush(stdout);
1510 snprintf(cmd, sizeof(cmd),
1511 "\"%s\" %s template1 >%s",
1512 backend_exec, backend_options,
1513 DEVNULL);
1515 PG_CMD_OPEN;
1517 PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n",
1518 username, escape_quotes(pwd1));
1520 /* MM: pwd1 is no longer needed, freeing it */
1521 free(pwd1);
1523 PG_CMD_CLOSE;
1525 check_ok();
1527 snprintf(pwdpath, sizeof(pwdpath), "%s/global/pg_auth", pg_data);
1528 if (stat(pwdpath, &statbuf) != 0 || !S_ISREG(statbuf.st_mode))
1530 fprintf(stderr,
1531 _("%s: The password file was not generated. "
1532 "Please report this problem.\n"),
1533 progname);
1534 exit_nicely();
1539 * set up pg_depend
1541 static void
1542 setup_depend(void)
1544 PG_CMD_DECL;
1545 const char **line;
1546 static const char *pg_depend_setup[] = {
1548 * Make PIN entries in pg_depend for all objects made so far in the
1549 * tables that the dependency code handles. This is overkill (the
1550 * system doesn't really depend on having every last weird datatype,
1551 * for instance) but generating only the minimum required set of
1552 * dependencies seems hard.
1554 * Note that we deliberately do not pin the system views, which
1555 * haven't been created yet. Also, no conversions, databases, or
1556 * tablespaces are pinned.
1558 * First delete any already-made entries; PINs override all else, and
1559 * must be the only entries for their objects.
1561 "DELETE FROM pg_depend;\n",
1562 "VACUUM pg_depend;\n",
1563 "DELETE FROM pg_shdepend;\n",
1564 "VACUUM pg_shdepend;\n",
1566 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1567 " FROM pg_class;\n",
1568 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1569 " FROM pg_proc;\n",
1570 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1571 " FROM pg_type;\n",
1572 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1573 " FROM pg_cast;\n",
1574 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1575 " FROM pg_constraint;\n",
1576 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1577 " FROM pg_attrdef;\n",
1578 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1579 " FROM pg_language;\n",
1580 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1581 " FROM pg_operator;\n",
1582 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1583 " FROM pg_opclass;\n",
1584 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1585 " FROM pg_opfamily;\n",
1586 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1587 " FROM pg_amop;\n",
1588 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1589 " FROM pg_amproc;\n",
1590 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1591 " FROM pg_rewrite;\n",
1592 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1593 " FROM pg_trigger;\n",
1596 * restriction here to avoid pinning the public namespace
1598 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1599 " FROM pg_namespace "
1600 " WHERE nspname LIKE 'pg%';\n",
1602 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1603 " FROM pg_ts_parser;\n",
1604 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1605 " FROM pg_ts_dict;\n",
1606 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1607 " FROM pg_ts_template;\n",
1608 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1609 " FROM pg_ts_config;\n",
1610 "INSERT INTO pg_shdepend SELECT 0, 0, 0, tableoid, oid, 'p' "
1611 " FROM pg_authid;\n",
1612 NULL
1615 fputs(_("initializing dependencies ... "), stdout);
1616 fflush(stdout);
1618 snprintf(cmd, sizeof(cmd),
1619 "\"%s\" %s template1 >%s",
1620 backend_exec, backend_options,
1621 DEVNULL);
1623 PG_CMD_OPEN;
1625 for (line = pg_depend_setup; *line != NULL; line++)
1626 PG_CMD_PUTS(*line);
1628 PG_CMD_CLOSE;
1630 check_ok();
1634 * set up system views
1636 static void
1637 setup_sysviews(void)
1639 PG_CMD_DECL;
1640 char **line;
1641 char **sysviews_setup;
1643 fputs(_("creating system views ... "), stdout);
1644 fflush(stdout);
1646 sysviews_setup = readfile(system_views_file);
1649 * We use -j here to avoid backslashing stuff in system_views.sql
1651 snprintf(cmd, sizeof(cmd),
1652 "\"%s\" %s -j template1 >%s",
1653 backend_exec, backend_options,
1654 DEVNULL);
1656 PG_CMD_OPEN;
1658 for (line = sysviews_setup; *line != NULL; line++)
1660 PG_CMD_PUTS(*line);
1661 free(*line);
1664 PG_CMD_CLOSE;
1666 free(sysviews_setup);
1668 check_ok();
1672 * load description data
1674 static void
1675 setup_description(void)
1677 PG_CMD_DECL;
1679 fputs(_("loading system objects' descriptions ... "), stdout);
1680 fflush(stdout);
1682 snprintf(cmd, sizeof(cmd),
1683 "\"%s\" %s template1 >%s",
1684 backend_exec, backend_options,
1685 DEVNULL);
1687 PG_CMD_OPEN;
1689 PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_description ( "
1690 " objoid oid, "
1691 " classname name, "
1692 " objsubid int4, "
1693 " description text) WITHOUT OIDS;\n");
1695 PG_CMD_PRINTF1("COPY tmp_pg_description FROM E'%s';\n",
1696 escape_quotes(desc_file));
1698 PG_CMD_PUTS("INSERT INTO pg_description "
1699 " SELECT t.objoid, c.oid, t.objsubid, t.description "
1700 " FROM tmp_pg_description t, pg_class c "
1701 " WHERE c.relname = t.classname;\n");
1703 PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_shdescription ( "
1704 " objoid oid, "
1705 " classname name, "
1706 " description text) WITHOUT OIDS;\n");
1708 PG_CMD_PRINTF1("COPY tmp_pg_shdescription FROM E'%s';\n",
1709 escape_quotes(shdesc_file));
1711 PG_CMD_PUTS("INSERT INTO pg_shdescription "
1712 " SELECT t.objoid, c.oid, t.description "
1713 " FROM tmp_pg_shdescription t, pg_class c "
1714 " WHERE c.relname = t.classname;\n");
1716 PG_CMD_CLOSE;
1718 check_ok();
1722 * load conversion functions
1724 static void
1725 setup_conversion(void)
1727 PG_CMD_DECL;
1728 char **line;
1729 char **conv_lines;
1731 fputs(_("creating conversions ... "), stdout);
1732 fflush(stdout);
1734 snprintf(cmd, sizeof(cmd),
1735 "\"%s\" %s template1 >%s",
1736 backend_exec, backend_options,
1737 DEVNULL);
1739 PG_CMD_OPEN;
1741 conv_lines = readfile(conversion_file);
1742 for (line = conv_lines; *line != NULL; line++)
1744 if (strstr(*line, "DROP CONVERSION") != *line)
1745 PG_CMD_PUTS(*line);
1746 free(*line);
1749 free(conv_lines);
1751 PG_CMD_CLOSE;
1753 check_ok();
1757 * load extra dictionaries (Snowball stemmers)
1759 static void
1760 setup_dictionary(void)
1762 PG_CMD_DECL;
1763 char **line;
1764 char **conv_lines;
1766 fputs(_("creating dictionaries ... "), stdout);
1767 fflush(stdout);
1770 * We use -j here to avoid backslashing stuff
1772 snprintf(cmd, sizeof(cmd),
1773 "\"%s\" %s -j template1 >%s",
1774 backend_exec, backend_options,
1775 DEVNULL);
1777 PG_CMD_OPEN;
1779 conv_lines = readfile(dictionary_file);
1780 for (line = conv_lines; *line != NULL; line++)
1782 PG_CMD_PUTS(*line);
1783 free(*line);
1786 free(conv_lines);
1788 PG_CMD_CLOSE;
1790 check_ok();
1794 * Set up privileges
1796 * We mark most system catalogs as world-readable. We don't currently have
1797 * to touch functions, languages, or databases, because their default
1798 * permissions are OK.
1800 * Some objects may require different permissions by default, so we
1801 * make sure we don't overwrite privilege sets that have already been
1802 * set (NOT NULL).
1804 static void
1805 setup_privileges(void)
1807 PG_CMD_DECL;
1808 char **line;
1809 char **priv_lines;
1810 static char *privileges_setup[] = {
1811 "UPDATE pg_class "
1812 " SET relacl = E'{\"=r/\\\\\"$POSTGRES_SUPERUSERNAME\\\\\"\"}' "
1813 " WHERE relkind IN ('r', 'v', 'S') AND relacl IS NULL;\n",
1814 "GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n",
1815 "GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n",
1816 NULL
1819 fputs(_("setting privileges on built-in objects ... "), stdout);
1820 fflush(stdout);
1822 snprintf(cmd, sizeof(cmd),
1823 "\"%s\" %s template1 >%s",
1824 backend_exec, backend_options,
1825 DEVNULL);
1827 PG_CMD_OPEN;
1829 priv_lines = replace_token(privileges_setup,
1830 "$POSTGRES_SUPERUSERNAME", username);
1831 for (line = priv_lines; *line != NULL; line++)
1832 PG_CMD_PUTS(*line);
1834 PG_CMD_CLOSE;
1836 check_ok();
1840 * extract the strange version of version required for information schema
1841 * (09.08.0007abc)
1843 static void
1844 set_info_version(void)
1846 char *letterversion;
1847 long major = 0,
1848 minor = 0,
1849 micro = 0;
1850 char *endptr;
1851 char *vstr = xstrdup(PG_VERSION);
1852 char *ptr;
1854 ptr = vstr + (strlen(vstr) - 1);
1855 while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1856 ptr--;
1857 letterversion = ptr + 1;
1858 major = strtol(vstr, &endptr, 10);
1859 if (*endptr)
1860 minor = strtol(endptr + 1, &endptr, 10);
1861 if (*endptr)
1862 micro = strtol(endptr + 1, &endptr, 10);
1863 snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1864 major, minor, micro, letterversion);
1868 * load info schema and populate from features file
1870 static void
1871 setup_schema(void)
1873 PG_CMD_DECL;
1874 char **line;
1875 char **lines;
1877 fputs(_("creating information schema ... "), stdout);
1878 fflush(stdout);
1880 lines = readfile(info_schema_file);
1883 * We use -j here to avoid backslashing stuff in information_schema.sql
1885 snprintf(cmd, sizeof(cmd),
1886 "\"%s\" %s -j template1 >%s",
1887 backend_exec, backend_options,
1888 DEVNULL);
1890 PG_CMD_OPEN;
1892 for (line = lines; *line != NULL; line++)
1894 PG_CMD_PUTS(*line);
1895 free(*line);
1898 free(lines);
1900 PG_CMD_CLOSE;
1902 snprintf(cmd, sizeof(cmd),
1903 "\"%s\" %s template1 >%s",
1904 backend_exec, backend_options,
1905 DEVNULL);
1907 PG_CMD_OPEN;
1909 PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "
1910 " SET character_value = '%s' "
1911 " WHERE implementation_info_name = 'DBMS VERSION';\n",
1912 infoversion);
1914 PG_CMD_PRINTF1("COPY information_schema.sql_features "
1915 " (feature_id, feature_name, sub_feature_id, "
1916 " sub_feature_name, is_supported, comments) "
1917 " FROM E'%s';\n",
1918 escape_quotes(features_file));
1920 PG_CMD_CLOSE;
1922 check_ok();
1926 * clean everything up in template1
1928 static void
1929 vacuum_db(void)
1931 PG_CMD_DECL;
1933 fputs(_("vacuuming database template1 ... "), stdout);
1934 fflush(stdout);
1936 snprintf(cmd, sizeof(cmd),
1937 "\"%s\" %s template1 >%s",
1938 backend_exec, backend_options,
1939 DEVNULL);
1941 PG_CMD_OPEN;
1943 PG_CMD_PUTS("ANALYZE;\nVACUUM FULL;\nVACUUM FREEZE;\n");
1945 PG_CMD_CLOSE;
1947 check_ok();
1951 * copy template1 to template0
1953 static void
1954 make_template0(void)
1956 PG_CMD_DECL;
1957 const char **line;
1958 static const char *template0_setup[] = {
1959 "CREATE DATABASE template0;\n",
1960 "UPDATE pg_database SET "
1961 " datistemplate = 't', "
1962 " datallowconn = 'f' "
1963 " WHERE datname = 'template0';\n",
1966 * We use the OID of template0 to determine lastsysoid
1968 "UPDATE pg_database SET datlastsysoid = "
1969 " (SELECT oid FROM pg_database "
1970 " WHERE datname = 'template0');\n",
1973 * Explicitly revoke public create-schema and create-temp-table
1974 * privileges in template1 and template0; else the latter would be on
1975 * by default
1977 "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n",
1978 "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n",
1981 * Finally vacuum to clean up dead rows in pg_database
1983 "VACUUM FULL pg_database;\n",
1984 NULL
1987 fputs(_("copying template1 to template0 ... "), stdout);
1988 fflush(stdout);
1990 snprintf(cmd, sizeof(cmd),
1991 "\"%s\" %s template1 >%s",
1992 backend_exec, backend_options,
1993 DEVNULL);
1995 PG_CMD_OPEN;
1997 for (line = template0_setup; *line; line++)
1998 PG_CMD_PUTS(*line);
2000 PG_CMD_CLOSE;
2002 check_ok();
2006 * copy template1 to postgres
2008 static void
2009 make_postgres(void)
2011 PG_CMD_DECL;
2012 const char **line;
2013 static const char *postgres_setup[] = {
2014 "CREATE DATABASE postgres;\n",
2015 NULL
2018 fputs(_("copying template1 to postgres ... "), stdout);
2019 fflush(stdout);
2021 snprintf(cmd, sizeof(cmd),
2022 "\"%s\" %s template1 >%s",
2023 backend_exec, backend_options,
2024 DEVNULL);
2026 PG_CMD_OPEN;
2028 for (line = postgres_setup; *line; line++)
2029 PG_CMD_PUTS(*line);
2031 PG_CMD_CLOSE;
2033 check_ok();
2038 * signal handler in case we are interrupted.
2040 * The Windows runtime docs at
2041 * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_signal.asp
2042 * specifically forbid a number of things being done from a signal handler,
2043 * including IO, memory allocation and system calls, and only allow jmpbuf
2044 * if you are handling SIGFPE.
2046 * I avoided doing the forbidden things by setting a flag instead of calling
2047 * exit_nicely() directly.
2049 * Also note the behaviour of Windows with SIGINT, which says this:
2050 * Note SIGINT is not supported for any Win32 application, including
2051 * Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs,
2052 * Win32 operating systems generate a new thread to specifically handle
2053 * that interrupt. This can cause a single-thread application such as UNIX,
2054 * to become multithreaded, resulting in unexpected behavior.
2056 * I have no idea how to handle this. (Strange they call UNIX an application!)
2057 * So this will need some testing on Windows.
2059 static void
2060 trapsig(int signum)
2062 /* handle systems that reset the handler, like Windows (grr) */
2063 pqsignal(signum, trapsig);
2064 caught_signal = true;
2068 * call exit_nicely() if we got a signal, or else output "ok".
2070 static void
2071 check_ok(void)
2073 if (caught_signal)
2075 printf(_("caught signal\n"));
2076 fflush(stdout);
2077 exit_nicely();
2079 else if (output_failed)
2081 printf(_("could not write to child process: %s\n"),
2082 strerror(output_errno));
2083 fflush(stdout);
2084 exit_nicely();
2086 else
2088 /* all seems well */
2089 printf(_("ok\n"));
2090 fflush(stdout);
2095 * Escape (by doubling) any single quotes or backslashes in given string
2097 * Note: this is used to process both postgresql.conf entries and SQL
2098 * string literals. Since postgresql.conf strings are defined to treat
2099 * backslashes as escapes, we have to double backslashes here. Hence,
2100 * when using this for a SQL string literal, use E'' syntax.
2102 * We do not need to worry about encoding considerations because all
2103 * valid backend encodings are ASCII-safe.
2105 static char *
2106 escape_quotes(const char *src)
2108 int len = strlen(src),
2111 char *result = pg_malloc(len * 2 + 1);
2113 for (i = 0, j = 0; i < len; i++)
2115 if (SQL_STR_DOUBLE(src[i], true))
2116 result[j++] = src[i];
2117 result[j++] = src[i];
2119 result[j] = '\0';
2120 return result;
2123 /* Hack to suppress a warning about %x from some versions of gcc */
2124 static inline size_t
2125 my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)
2127 return strftime(s, max, fmt, tm);
2131 * Determine likely date order from locale
2133 static int
2134 locale_date_order(const char *locale)
2136 struct tm testtime;
2137 char buf[128];
2138 char *posD;
2139 char *posM;
2140 char *posY;
2141 char *save;
2142 size_t res;
2143 int result;
2145 result = DATEORDER_MDY; /* default */
2147 save = setlocale(LC_TIME, NULL);
2148 if (!save)
2149 return result;
2150 save = xstrdup(save);
2152 setlocale(LC_TIME, locale);
2154 memset(&testtime, 0, sizeof(testtime));
2155 testtime.tm_mday = 22;
2156 testtime.tm_mon = 10; /* November, should come out as "11" */
2157 testtime.tm_year = 133; /* 2033 */
2159 res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2161 setlocale(LC_TIME, save);
2162 free(save);
2164 if (res == 0)
2165 return result;
2167 posM = strstr(buf, "11");
2168 posD = strstr(buf, "22");
2169 posY = strstr(buf, "33");
2171 if (!posM || !posD || !posY)
2172 return result;
2174 if (posY < posM && posM < posD)
2175 result = DATEORDER_YMD;
2176 else if (posD < posM)
2177 result = DATEORDER_DMY;
2178 else
2179 result = DATEORDER_MDY;
2181 return result;
2185 * check if given string is a valid locale specifier
2187 * this should match the backend check_locale() function
2189 static bool
2190 chklocale(const char *locale)
2192 bool ret;
2193 int category = LC_CTYPE;
2194 char *save;
2196 save = setlocale(category, NULL);
2197 if (!save)
2198 return false; /* should not happen; */
2200 save = xstrdup(save);
2202 ret = (setlocale(category, locale) != NULL);
2204 setlocale(category, save);
2205 free(save);
2207 /* should we exit here? */
2208 if (!ret)
2209 fprintf(stderr, _("%s: invalid locale name \"%s\"\n"), progname, locale);
2211 return ret;
2215 * set up the locale variables
2217 * assumes we have called setlocale(LC_ALL,"")
2219 static void
2220 setlocales(void)
2222 /* set empty lc_* values to locale config if set */
2224 if (strlen(locale) > 0)
2226 if (strlen(lc_ctype) == 0)
2227 lc_ctype = locale;
2228 if (strlen(lc_collate) == 0)
2229 lc_collate = locale;
2230 if (strlen(lc_numeric) == 0)
2231 lc_numeric = locale;
2232 if (strlen(lc_time) == 0)
2233 lc_time = locale;
2234 if (strlen(lc_monetary) == 0)
2235 lc_monetary = locale;
2236 if (strlen(lc_messages) == 0)
2237 lc_messages = locale;
2241 * override absent/invalid config settings from initdb's locale settings
2244 if (strlen(lc_ctype) == 0 || !chklocale(lc_ctype))
2245 lc_ctype = xstrdup(setlocale(LC_CTYPE, NULL));
2246 if (strlen(lc_collate) == 0 || !chklocale(lc_collate))
2247 lc_collate = xstrdup(setlocale(LC_COLLATE, NULL));
2248 if (strlen(lc_numeric) == 0 || !chklocale(lc_numeric))
2249 lc_numeric = xstrdup(setlocale(LC_NUMERIC, NULL));
2250 if (strlen(lc_time) == 0 || !chklocale(lc_time))
2251 lc_time = xstrdup(setlocale(LC_TIME, NULL));
2252 if (strlen(lc_monetary) == 0 || !chklocale(lc_monetary))
2253 lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL));
2254 if (strlen(lc_messages) == 0 || !chklocale(lc_messages))
2255 #if defined(LC_MESSAGES) && !defined(WIN32)
2257 /* when available get the current locale setting */
2258 lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL));
2260 #else
2262 /* when not available, get the CTYPE setting */
2263 lc_messages = xstrdup(setlocale(LC_CTYPE, NULL));
2265 #endif
2269 #ifdef WIN32
2270 typedef BOOL(WINAPI * __CreateRestrictedToken) (HANDLE, DWORD, DWORD, PSID_AND_ATTRIBUTES, DWORD, PLUID_AND_ATTRIBUTES, DWORD, PSID_AND_ATTRIBUTES, PHANDLE);
2272 #define DISABLE_MAX_PRIVILEGE 0x1
2275 * Create a restricted token and execute the specified process with it.
2277 * Returns 0 on failure, non-zero on success, same as CreateProcess().
2279 * On NT4, or any other system not containing the required functions, will
2280 * NOT execute anything.
2282 static int
2283 CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo)
2285 BOOL b;
2286 STARTUPINFO si;
2287 HANDLE origToken;
2288 HANDLE restrictedToken;
2289 SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
2290 SID_AND_ATTRIBUTES dropSids[2];
2291 __CreateRestrictedToken _CreateRestrictedToken = NULL;
2292 HANDLE Advapi32Handle;
2294 ZeroMemory(&si, sizeof(si));
2295 si.cb = sizeof(si);
2297 Advapi32Handle = LoadLibrary("ADVAPI32.DLL");
2298 if (Advapi32Handle != NULL)
2300 _CreateRestrictedToken = (__CreateRestrictedToken) GetProcAddress(Advapi32Handle, "CreateRestrictedToken");
2303 if (_CreateRestrictedToken == NULL)
2305 fprintf(stderr, "WARNING: cannot create restricted tokens on this platform\n");
2306 if (Advapi32Handle != NULL)
2307 FreeLibrary(Advapi32Handle);
2308 return 0;
2311 /* Open the current token to use as a base for the restricted one */
2312 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken))
2314 fprintf(stderr, "Failed to open process token: %lu\n", GetLastError());
2315 return 0;
2318 /* Allocate list of SIDs to remove */
2319 ZeroMemory(&dropSids, sizeof(dropSids));
2320 if (!AllocateAndInitializeSid(&NtAuthority, 2,
2321 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
2322 0, &dropSids[0].Sid) ||
2323 !AllocateAndInitializeSid(&NtAuthority, 2,
2324 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
2325 0, &dropSids[1].Sid))
2327 fprintf(stderr, "Failed to allocate SIDs: %lu\n", GetLastError());
2328 return 0;
2331 b = _CreateRestrictedToken(origToken,
2332 DISABLE_MAX_PRIVILEGE,
2333 sizeof(dropSids) / sizeof(dropSids[0]),
2334 dropSids,
2335 0, NULL,
2336 0, NULL,
2337 &restrictedToken);
2339 FreeSid(dropSids[1].Sid);
2340 FreeSid(dropSids[0].Sid);
2341 CloseHandle(origToken);
2342 FreeLibrary(Advapi32Handle);
2344 if (!b)
2346 fprintf(stderr, "Failed to create restricted token: %lu\n", GetLastError());
2347 return 0;
2350 if (!CreateProcessAsUser(restrictedToken,
2351 NULL,
2352 cmd,
2353 NULL,
2354 NULL,
2355 TRUE,
2356 CREATE_SUSPENDED,
2357 NULL,
2358 NULL,
2359 &si,
2360 processInfo))
2363 fprintf(stderr, "CreateProcessAsUser failed: %lu\n", GetLastError());
2364 return 0;
2367 #ifndef __CYGWIN__
2368 AddUserToDacl(processInfo->hProcess);
2369 #endif
2371 return ResumeThread(processInfo->hThread);
2373 #endif
2376 * print help text
2378 static void
2379 usage(const char *progname)
2381 printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2382 printf(_("Usage:\n"));
2383 printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2384 printf(_("\nOptions:\n"));
2385 printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2386 printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2387 printf(_(" --locale=LOCALE set default locale for new databases\n"));
2388 printf(_(" --lc-collate, --lc-ctype, --lc-messages=LOCALE\n"
2389 " --lc-monetary, --lc-numeric, --lc-time=LOCALE\n"
2390 " set default locale in the respective\n"
2391 " category for new databases (default\n"
2392 " taken from environment)\n"));
2393 printf(_(" --no-locale equivalent to --locale=C\n"));
2394 printf(_(" -T, --text-search-config=CFG\n"
2395 " default text search configuration\n"));
2396 printf(_(" -X, --xlogdir=XLOGDIR location for the transaction log directory\n"));
2397 printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2398 printf(_(" -U, --username=NAME database superuser name\n"));
2399 printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2400 printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2401 printf(_(" -?, --help show this help, then exit\n"));
2402 printf(_(" -V, --version output version information, then exit\n"));
2403 printf(_("\nLess commonly used options:\n"));
2404 printf(_(" -d, --debug generate lots of debugging output\n"));
2405 printf(_(" -s, --show show internal settings\n"));
2406 printf(_(" -L DIRECTORY where to find the input files\n"));
2407 printf(_(" -n, --noclean do not clean up after errors\n"));
2408 printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2409 "is used.\n"));
2410 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
2414 main(int argc, char *argv[])
2417 * options with no short version return a low integer, the rest return
2418 * their short version value
2420 static struct option long_options[] = {
2421 {"pgdata", required_argument, NULL, 'D'},
2422 {"encoding", required_argument, NULL, 'E'},
2423 {"locale", required_argument, NULL, 1},
2424 {"lc-collate", required_argument, NULL, 2},
2425 {"lc-ctype", required_argument, NULL, 3},
2426 {"lc-monetary", required_argument, NULL, 4},
2427 {"lc-numeric", required_argument, NULL, 5},
2428 {"lc-time", required_argument, NULL, 6},
2429 {"lc-messages", required_argument, NULL, 7},
2430 {"no-locale", no_argument, NULL, 8},
2431 {"text-search-config", required_argument, NULL, 'T'},
2432 {"auth", required_argument, NULL, 'A'},
2433 {"pwprompt", no_argument, NULL, 'W'},
2434 {"pwfile", required_argument, NULL, 9},
2435 {"username", required_argument, NULL, 'U'},
2436 {"help", no_argument, NULL, '?'},
2437 {"version", no_argument, NULL, 'V'},
2438 {"debug", no_argument, NULL, 'd'},
2439 {"show", no_argument, NULL, 's'},
2440 {"noclean", no_argument, NULL, 'n'},
2441 {"xlogdir", required_argument, NULL, 'X'},
2442 {NULL, 0, NULL, 0}
2445 int c,
2447 ret;
2448 int option_index;
2449 char *short_version;
2450 char *effective_user;
2451 char *pgdenv; /* PGDATA value gotten from and sent to
2452 * environment */
2453 char bin_dir[MAXPGPATH];
2454 char *pg_data_native;
2456 #ifdef WIN32
2457 char *restrict_env;
2458 #endif
2459 static const char *subdirs[] = {
2460 "global",
2461 "pg_xlog",
2462 "pg_xlog/archive_status",
2463 "pg_clog",
2464 "pg_subtrans",
2465 "pg_twophase",
2466 "pg_multixact/members",
2467 "pg_multixact/offsets",
2468 "base",
2469 "base/1",
2470 "pg_tblspc",
2471 "pg_stat_tmp"
2474 progname = get_progname(argv[0]);
2475 set_pglocale_pgservice(argv[0], "initdb");
2477 if (argc > 1)
2479 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2481 usage(progname);
2482 exit(0);
2484 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2486 puts("initdb (PostgreSQL) " PG_VERSION);
2487 exit(0);
2491 /* process command-line options */
2493 while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1)
2495 switch (c)
2497 case 'A':
2498 authmethod = xstrdup(optarg);
2499 break;
2500 case 'D':
2501 pg_data = xstrdup(optarg);
2502 break;
2503 case 'E':
2504 encoding = xstrdup(optarg);
2505 break;
2506 case 'W':
2507 pwprompt = true;
2508 break;
2509 case 'U':
2510 username = xstrdup(optarg);
2511 break;
2512 case 'd':
2513 debug = true;
2514 printf(_("Running in debug mode.\n"));
2515 break;
2516 case 'n':
2517 noclean = true;
2518 printf(_("Running in noclean mode. Mistakes will not be cleaned up.\n"));
2519 break;
2520 case 'L':
2521 share_path = xstrdup(optarg);
2522 break;
2523 case 1:
2524 locale = xstrdup(optarg);
2525 break;
2526 case 2:
2527 lc_collate = xstrdup(optarg);
2528 break;
2529 case 3:
2530 lc_ctype = xstrdup(optarg);
2531 break;
2532 case 4:
2533 lc_monetary = xstrdup(optarg);
2534 break;
2535 case 5:
2536 lc_numeric = xstrdup(optarg);
2537 break;
2538 case 6:
2539 lc_time = xstrdup(optarg);
2540 break;
2541 case 7:
2542 lc_messages = xstrdup(optarg);
2543 break;
2544 case 8:
2545 locale = "C";
2546 break;
2547 case 9:
2548 pwfilename = xstrdup(optarg);
2549 break;
2550 case 's':
2551 show_setting = true;
2552 break;
2553 case 'T':
2554 default_text_search_config = xstrdup(optarg);
2555 break;
2556 case 'X':
2557 xlog_dir = xstrdup(optarg);
2558 break;
2559 default:
2560 /* getopt_long already emitted a complaint */
2561 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2562 progname);
2563 exit(1);
2568 /* Non-option argument specifies data directory */
2569 if (optind < argc)
2571 pg_data = xstrdup(argv[optind]);
2572 optind++;
2575 if (optind < argc)
2577 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
2578 progname, argv[optind + 1]);
2579 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2580 progname);
2581 exit(1);
2584 if (pwprompt && pwfilename)
2586 fprintf(stderr, _("%s: password prompt and password file cannot be specified together\n"), progname);
2587 exit(1);
2590 if (authmethod == NULL || !strlen(authmethod))
2592 authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n"
2593 "You can change this by editing pg_hba.conf or using the -A option the\n"
2594 "next time you run initdb.\n");
2595 authmethod = "trust";
2598 if (strcmp(authmethod, "md5") &&
2599 strcmp(authmethod, "ident") &&
2600 strncmp(authmethod, "ident ", 6) && /* ident with space = param */
2601 strcmp(authmethod, "trust") &&
2602 #ifdef USE_PAM
2603 strcmp(authmethod, "pam") &&
2604 strncmp(authmethod, "pam ", 4) && /* pam with space = param */
2605 #endif
2606 strcmp(authmethod, "crypt") &&
2607 strcmp(authmethod, "password")
2611 * Kerberos methods not listed because they are not supported over
2612 * local connections and are rejected in hba.c
2615 fprintf(stderr, _("%s: unrecognized authentication method \"%s\"\n"),
2616 progname, authmethod);
2617 exit(1);
2620 if ((!strcmp(authmethod, "md5") ||
2621 !strcmp(authmethod, "crypt") ||
2622 !strcmp(authmethod, "password")) &&
2623 !(pwprompt || pwfilename))
2625 fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname, authmethod);
2626 exit(1);
2629 if (strlen(pg_data) == 0)
2631 pgdenv = getenv("PGDATA");
2632 if (pgdenv && strlen(pgdenv))
2634 /* PGDATA found */
2635 pg_data = xstrdup(pgdenv);
2637 else
2639 fprintf(stderr,
2640 _("%s: no data directory specified\n"
2641 "You must identify the directory where the data for this database system\n"
2642 "will reside. Do this with either the invocation option -D or the\n"
2643 "environment variable PGDATA.\n"),
2644 progname);
2645 exit(1);
2649 pg_data_native = pg_data;
2650 canonicalize_path(pg_data);
2652 #ifdef WIN32
2655 * Before we execute another program, make sure that we are running with a
2656 * restricted token. If not, re-execute ourselves with one.
2659 if ((restrict_env = getenv("PG_RESTRICT_EXEC")) == NULL
2660 || strcmp(restrict_env, "1") != 0)
2662 PROCESS_INFORMATION pi;
2663 char *cmdline;
2665 ZeroMemory(&pi, sizeof(pi));
2667 cmdline = xstrdup(GetCommandLine());
2669 putenv("PG_RESTRICT_EXEC=1");
2671 if (!CreateRestrictedProcess(cmdline, &pi))
2673 fprintf(stderr, "Failed to re-exec with restricted token: %lu.\n", GetLastError());
2675 else
2678 * Successfully re-execed. Now wait for child process to capture
2679 * exitcode.
2681 DWORD x;
2683 CloseHandle(pi.hThread);
2684 WaitForSingleObject(pi.hProcess, INFINITE);
2686 if (!GetExitCodeProcess(pi.hProcess, &x))
2688 fprintf(stderr, "Failed to get exit code from subprocess: %lu\n", GetLastError());
2689 exit(1);
2691 exit(x);
2694 #endif
2697 * we have to set PGDATA for postgres rather than pass it on the command
2698 * line to avoid dumb quoting problems on Windows, and we would especially
2699 * need quotes otherwise on Windows because paths there are most likely to
2700 * have embedded spaces.
2702 pgdenv = pg_malloc(8 + strlen(pg_data));
2703 sprintf(pgdenv, "PGDATA=%s", pg_data);
2704 putenv(pgdenv);
2706 if ((ret = find_other_exec(argv[0], "postgres", PG_BACKEND_VERSIONSTR,
2707 backend_exec)) < 0)
2709 char full_path[MAXPGPATH];
2711 if (find_my_exec(argv[0], full_path) < 0)
2712 strlcpy(full_path, progname, sizeof(full_path));
2714 if (ret == -1)
2715 fprintf(stderr,
2716 _("The program \"postgres\" is needed by %s "
2717 "but was not found in the\n"
2718 "same directory as \"%s\".\n"
2719 "Check your installation.\n"),
2720 progname, full_path);
2721 else
2722 fprintf(stderr,
2723 _("The program \"postgres\" was found by \"%s\"\n"
2724 "but was not the same version as %s.\n"
2725 "Check your installation.\n"),
2726 full_path, progname);
2727 exit(1);
2730 /* store binary directory */
2731 strcpy(bin_path, backend_exec);
2732 *last_dir_separator(bin_path) = '\0';
2733 canonicalize_path(bin_path);
2735 if (!share_path)
2737 share_path = pg_malloc(MAXPGPATH);
2738 get_share_path(backend_exec, share_path);
2740 else if (!is_absolute_path(share_path))
2742 fprintf(stderr, _("%s: input file location must be an absolute path\n"), progname);
2743 exit(1);
2746 canonicalize_path(share_path);
2748 if ((short_version = get_short_version()) == NULL)
2750 fprintf(stderr, _("%s: could not determine valid short version string\n"), progname);
2751 exit(1);
2754 effective_user = get_id();
2755 if (strlen(username) == 0)
2756 username = effective_user;
2758 set_input(&bki_file, "postgres.bki");
2759 set_input(&desc_file, "postgres.description");
2760 set_input(&shdesc_file, "postgres.shdescription");
2761 set_input(&hba_file, "pg_hba.conf.sample");
2762 set_input(&ident_file, "pg_ident.conf.sample");
2763 set_input(&conf_file, "postgresql.conf.sample");
2764 set_input(&conversion_file, "conversion_create.sql");
2765 set_input(&dictionary_file, "snowball_create.sql");
2766 set_input(&info_schema_file, "information_schema.sql");
2767 set_input(&features_file, "sql_features.txt");
2768 set_input(&system_views_file, "system_views.sql");
2770 set_info_version();
2772 if (show_setting || debug)
2774 fprintf(stderr,
2775 "VERSION=%s\n"
2776 "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2777 "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2778 "POSTGRES_DESCR=%s\nPOSTGRES_SHDESCR=%s\n"
2779 "POSTGRESQL_CONF_SAMPLE=%s\n"
2780 "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2781 PG_VERSION,
2782 pg_data, share_path, bin_path,
2783 username, bki_file,
2784 desc_file, shdesc_file,
2785 conf_file,
2786 hba_file, ident_file);
2787 if (show_setting)
2788 exit(0);
2791 check_input(bki_file);
2792 check_input(desc_file);
2793 check_input(shdesc_file);
2794 check_input(hba_file);
2795 check_input(ident_file);
2796 check_input(conf_file);
2797 check_input(conversion_file);
2798 check_input(dictionary_file);
2799 check_input(info_schema_file);
2800 check_input(features_file);
2801 check_input(system_views_file);
2803 setlocales();
2805 printf(_("The files belonging to this database system will be owned "
2806 "by user \"%s\".\n"
2807 "This user must also own the server process.\n\n"),
2808 effective_user);
2810 if (strcmp(lc_ctype, lc_collate) == 0 &&
2811 strcmp(lc_ctype, lc_time) == 0 &&
2812 strcmp(lc_ctype, lc_numeric) == 0 &&
2813 strcmp(lc_ctype, lc_monetary) == 0 &&
2814 strcmp(lc_ctype, lc_messages) == 0)
2815 printf(_("The database cluster will be initialized with locale %s.\n"), lc_ctype);
2816 else
2818 printf(_("The database cluster will be initialized with locales\n"
2819 " COLLATE: %s\n"
2820 " CTYPE: %s\n"
2821 " MESSAGES: %s\n"
2822 " MONETARY: %s\n"
2823 " NUMERIC: %s\n"
2824 " TIME: %s\n"),
2825 lc_collate,
2826 lc_ctype,
2827 lc_messages,
2828 lc_monetary,
2829 lc_numeric,
2830 lc_time);
2833 if (strlen(encoding) == 0)
2835 int ctype_enc;
2837 ctype_enc = pg_get_encoding_from_locale(lc_ctype);
2839 if (ctype_enc == PG_SQL_ASCII &&
2840 !(pg_strcasecmp(lc_ctype, "C") == 0 ||
2841 pg_strcasecmp(lc_ctype, "POSIX") == 0))
2843 /* Hmm, couldn't recognize the locale's codeset */
2844 fprintf(stderr, _("%s: could not find suitable encoding for locale %s\n"),
2845 progname, lc_ctype);
2846 fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
2847 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2848 progname);
2849 exit(1);
2851 else if (!pg_valid_server_encoding_id(ctype_enc))
2853 /* We recognized it, but it's not a legal server encoding */
2854 fprintf(stderr,
2855 _("%s: locale %s requires unsupported encoding %s\n"),
2856 progname, lc_ctype, pg_encoding_to_char(ctype_enc));
2857 fprintf(stderr,
2858 _("Encoding %s is not allowed as a server-side encoding.\n"
2859 "Rerun %s with a different locale selection.\n"),
2860 pg_encoding_to_char(ctype_enc), progname);
2861 exit(1);
2863 else
2865 encodingid = encodingid_to_string(ctype_enc);
2866 printf(_("The default database encoding has accordingly been set to %s.\n"),
2867 pg_encoding_to_char(ctype_enc));
2870 else
2872 int user_enc;
2873 int ctype_enc;
2875 encodingid = get_encoding_id(encoding);
2876 user_enc = atoi(encodingid);
2878 ctype_enc = pg_get_encoding_from_locale(lc_ctype);
2880 /* We allow selection of SQL_ASCII --- see notes in createdb() */
2881 if (!(ctype_enc == user_enc ||
2882 ctype_enc == PG_SQL_ASCII ||
2883 user_enc == PG_SQL_ASCII
2884 #ifdef WIN32
2887 * On win32, if the encoding chosen is UTF8, all locales are OK
2888 * (assuming the actual locale name passed the checks above). This is
2889 * because UTF8 is a pseudo-codepage, that we convert to UTF16 before
2890 * doing any operations on, and UTF16 supports all locales.
2892 || user_enc == PG_UTF8
2893 #endif
2896 fprintf(stderr, _("%s: encoding mismatch\n"), progname);
2897 fprintf(stderr,
2898 _("The encoding you selected (%s) and the encoding that the\n"
2899 "selected locale uses (%s) do not match. This would lead to\n"
2900 "misbehavior in various character string processing functions.\n"
2901 "Rerun %s and either do not specify an encoding explicitly,\n"
2902 "or choose a matching combination.\n"),
2903 pg_encoding_to_char(user_enc),
2904 pg_encoding_to_char(ctype_enc),
2905 progname);
2906 exit(1);
2910 if (strlen(default_text_search_config) == 0)
2912 default_text_search_config = find_matching_ts_config(lc_ctype);
2913 if (default_text_search_config == NULL)
2915 printf(_("%s: could not find suitable text search configuration for locale %s\n"),
2916 progname, lc_ctype);
2917 default_text_search_config = "simple";
2920 else
2922 const char *checkmatch = find_matching_ts_config(lc_ctype);
2924 if (checkmatch == NULL)
2926 printf(_("%s: warning: suitable text search configuration for locale %s is unknown\n"),
2927 progname, lc_ctype);
2929 else if (strcmp(checkmatch, default_text_search_config) != 0)
2931 printf(_("%s: warning: specified text search configuration \"%s\" might not match locale %s\n"),
2932 progname, default_text_search_config, lc_ctype);
2936 printf(_("The default text search configuration will be set to \"%s\".\n"),
2937 default_text_search_config);
2939 printf("\n");
2941 umask(077);
2944 * now we are starting to do real work, trap signals so we can clean up
2947 /* some of these are not valid on Windows */
2948 #ifdef SIGHUP
2949 pqsignal(SIGHUP, trapsig);
2950 #endif
2951 #ifdef SIGINT
2952 pqsignal(SIGINT, trapsig);
2953 #endif
2954 #ifdef SIGQUIT
2955 pqsignal(SIGQUIT, trapsig);
2956 #endif
2957 #ifdef SIGTERM
2958 pqsignal(SIGTERM, trapsig);
2959 #endif
2961 /* Ignore SIGPIPE when writing to backend, so we can clean up */
2962 #ifdef SIGPIPE
2963 pqsignal(SIGPIPE, SIG_IGN);
2964 #endif
2966 switch (check_data_dir(pg_data))
2968 case 0:
2969 /* PGDATA not there, must create it */
2970 printf(_("creating directory %s ... "),
2971 pg_data);
2972 fflush(stdout);
2974 if (!mkdatadir(NULL))
2975 exit_nicely();
2976 else
2977 check_ok();
2979 made_new_pgdata = true;
2980 break;
2982 case 1:
2983 /* Present but empty, fix permissions and use it */
2984 printf(_("fixing permissions on existing directory %s ... "),
2985 pg_data);
2986 fflush(stdout);
2988 if (chmod(pg_data, 0700) != 0)
2990 fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
2991 progname, pg_data, strerror(errno));
2992 exit_nicely();
2994 else
2995 check_ok();
2997 found_existing_pgdata = true;
2998 break;
3000 case 2:
3001 /* Present and not empty */
3002 fprintf(stderr,
3003 _("%s: directory \"%s\" exists but is not empty\n"),
3004 progname, pg_data);
3005 fprintf(stderr,
3006 _("If you want to create a new database system, either remove or empty\n"
3007 "the directory \"%s\" or run %s\n"
3008 "with an argument other than \"%s\".\n"),
3009 pg_data, progname, pg_data);
3010 exit(1); /* no further message needed */
3012 default:
3013 /* Trouble accessing directory */
3014 fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
3015 progname, pg_data, strerror(errno));
3016 exit_nicely();
3019 /* Create transaction log symlink, if required */
3020 if (strcmp(xlog_dir, "") != 0)
3022 char *linkloc;
3024 /* clean up xlog directory name, check it's absolute */
3025 canonicalize_path(xlog_dir);
3026 if (!is_absolute_path(xlog_dir))
3028 fprintf(stderr, _("%s: xlog directory location must be an absolute path\n"), progname);
3029 exit_nicely();
3032 /* check if the specified xlog directory is empty */
3033 switch (check_data_dir(xlog_dir))
3035 case 0:
3036 /* xlog directory not there, must create it */
3037 printf(_("creating directory %s ... "),
3038 xlog_dir);
3039 fflush(stdout);
3041 if (mkdir_p(xlog_dir, 0700) != 0)
3043 fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3044 progname, xlog_dir, strerror(errno));
3045 exit_nicely();
3047 else
3048 check_ok();
3050 made_new_xlogdir = true;
3051 break;
3052 case 1:
3053 /* Present but empty, fix permissions and use it */
3054 printf(_("fixing permissions on existing directory %s ... "),
3055 xlog_dir);
3056 fflush(stdout);
3058 if (chmod(xlog_dir, 0700) != 0)
3060 fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
3061 progname, xlog_dir, strerror(errno));
3062 exit_nicely();
3064 else
3065 check_ok();
3067 found_existing_xlogdir = true;
3068 break;
3069 case 2:
3070 /* Present and not empty */
3071 fprintf(stderr,
3072 _("%s: directory \"%s\" exists but is not empty\n"),
3073 progname, xlog_dir);
3074 fprintf(stderr,
3075 _("If you want to store the transaction log there, either\n"
3076 "remove or empty the directory \"%s\".\n"),
3077 xlog_dir);
3078 exit_nicely();
3080 default:
3081 /* Trouble accessing directory */
3082 fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
3083 progname, xlog_dir, strerror(errno));
3084 exit_nicely();
3087 /* form name of the place where the symlink must go */
3088 linkloc = (char *) pg_malloc(strlen(pg_data) + 8 + 1);
3089 sprintf(linkloc, "%s/pg_xlog", pg_data);
3091 #ifdef HAVE_SYMLINK
3092 if (symlink(xlog_dir, linkloc) != 0)
3094 fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
3095 progname, linkloc, strerror(errno));
3096 exit_nicely();
3098 #else
3099 fprintf(stderr, _("%s: symlinks are not supported on this platform"));
3100 exit_nicely();
3101 #endif
3104 /* Create required subdirectories */
3105 printf(_("creating subdirectories ... "));
3106 fflush(stdout);
3108 for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3110 if (!mkdatadir(subdirs[i]))
3111 exit_nicely();
3114 check_ok();
3116 /* Top level PG_VERSION is checked by bootstrapper, so make it first */
3117 set_short_version(short_version, NULL);
3119 /* Select suitable configuration settings */
3120 set_null_conf();
3121 test_config_settings();
3123 /* Now create all the text config files */
3124 setup_config();
3126 /* Bootstrap template1 */
3127 bootstrap_template1(short_version);
3130 * Make the per-database PG_VERSION for template1 only after init'ing it
3132 set_short_version(short_version, "base/1");
3134 /* Create the stuff we don't need to use bootstrap mode for */
3136 setup_auth();
3137 if (pwprompt || pwfilename)
3138 get_set_pwd();
3140 setup_depend();
3142 setup_sysviews();
3144 setup_description();
3146 setup_conversion();
3148 setup_dictionary();
3150 setup_privileges();
3152 setup_schema();
3154 vacuum_db();
3156 make_template0();
3158 make_postgres();
3160 if (authwarning != NULL)
3161 fprintf(stderr, "%s", authwarning);
3163 /* Get directory specification used to start this executable */
3164 strcpy(bin_dir, argv[0]);
3165 get_parent_directory(bin_dir);
3167 printf(_("\nSuccess. You can now start the database server using:\n\n"
3168 " %s%s%spostgres%s -D %s%s%s\n"
3169 "or\n"
3170 " %s%s%spg_ctl%s -D %s%s%s -l logfile start\n\n"),
3171 QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
3172 QUOTE_PATH, pg_data_native, QUOTE_PATH,
3173 QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
3174 QUOTE_PATH, pg_data_native, QUOTE_PATH);
3176 return 0;