Tighten the check in initdb and CREATE DATABASE that the chosen encoding
[PostgreSQL.git] / src / bin / initdb / initdb.c
blob5a929ba39f050aac7c5a1e6b6d04fa620636f036
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 check_locale_name(const char *locale);
192 static bool check_locale_encoding(const char *locale, int encoding);
193 static void setlocales(void);
194 static void usage(const char *progname);
196 #ifdef WIN32
197 static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo);
198 #endif
202 * macros for running pipes to postgres
204 #define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd
206 #define PG_CMD_OPEN \
207 do { \
208 cmdfd = popen_check(cmd, "w"); \
209 if (cmdfd == NULL) \
210 exit_nicely(); /* message already printed by popen_check */ \
211 } while (0)
213 #define PG_CMD_CLOSE \
214 do { \
215 if (pclose_check(cmdfd)) \
216 exit_nicely(); /* message already printed by pclose_check */ \
217 } while (0)
219 #define PG_CMD_PUTS(line) \
220 do { \
221 if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
222 output_failed = true, output_errno = errno; \
223 } while (0)
225 #define PG_CMD_PRINTF1(fmt, arg1) \
226 do { \
227 if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
228 output_failed = true, output_errno = errno; \
229 } while (0)
231 #define PG_CMD_PRINTF2(fmt, arg1, arg2) \
232 do { \
233 if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
234 output_failed = true, output_errno = errno; \
235 } while (0)
237 #ifndef WIN32
238 #define QUOTE_PATH ""
239 #define DIR_SEP "/"
240 #else
241 #define QUOTE_PATH "\""
242 #define DIR_SEP "\\"
243 #endif
246 * routines to check mem allocations and fail noisily.
248 * Note that we can't call exit_nicely() on a memory failure, as it calls
249 * rmtree() which needs memory allocation. So we just exit with a bang.
251 static void *
252 pg_malloc(size_t size)
254 void *result;
256 result = malloc(size);
257 if (!result)
259 fprintf(stderr, _("%s: out of memory\n"), progname);
260 exit(1);
262 return result;
265 static char *
266 xstrdup(const char *s)
268 char *result;
270 result = strdup(s);
271 if (!result)
273 fprintf(stderr, _("%s: out of memory\n"), progname);
274 exit(1);
276 return result;
280 * make a copy of the array of lines, with token replaced by replacement
281 * the first time it occurs on each line.
283 * This does most of what sed was used for in the shell script, but
284 * doesn't need any regexp stuff.
286 static char **
287 replace_token(char **lines, const char *token, const char *replacement)
289 int numlines = 1;
290 int i;
291 char **result;
292 int toklen,
293 replen,
294 diff;
296 for (i = 0; lines[i]; i++)
297 numlines++;
299 result = (char **) pg_malloc(numlines * sizeof(char *));
301 toklen = strlen(token);
302 replen = strlen(replacement);
303 diff = replen - toklen;
305 for (i = 0; i < numlines; i++)
307 char *where;
308 char *newline;
309 int pre;
311 /* just copy pointer if NULL or no change needed */
312 if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
314 result[i] = lines[i];
315 continue;
318 /* if we get here a change is needed - set up new line */
320 newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
322 pre = where - lines[i];
324 strncpy(newline, lines[i], pre);
326 strcpy(newline + pre, replacement);
328 strcpy(newline + pre + replen, lines[i] + pre + toklen);
330 result[i] = newline;
333 return result;
337 * make a copy of lines without any that contain the token
339 * a sort of poor man's grep -v
341 #ifndef HAVE_UNIX_SOCKETS
342 static char **
343 filter_lines_with_token(char **lines, const char *token)
345 int numlines = 1;
346 int i,
347 src,
348 dst;
349 char **result;
351 for (i = 0; lines[i]; i++)
352 numlines++;
354 result = (char **) pg_malloc(numlines * sizeof(char *));
356 for (src = 0, dst = 0; src < numlines; src++)
358 if (lines[src] == NULL || strstr(lines[src], token) == NULL)
359 result[dst++] = lines[src];
362 return result;
364 #endif
367 * get the lines from a text file
369 static char **
370 readfile(char *path)
372 FILE *infile;
373 int maxlength = 0,
374 linelen = 0;
375 int nlines = 0;
376 char **result;
377 char *buffer;
378 int c;
380 if ((infile = fopen(path, "r")) == NULL)
382 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
383 progname, path, strerror(errno));
384 exit_nicely();
387 /* pass over the file twice - the first time to size the result */
389 while ((c = fgetc(infile)) != EOF)
391 linelen++;
392 if (c == '\n')
394 nlines++;
395 if (linelen > maxlength)
396 maxlength = linelen;
397 linelen = 0;
401 /* handle last line without a terminating newline (yuck) */
403 if (linelen)
404 nlines++;
405 if (linelen > maxlength)
406 maxlength = linelen;
408 /* set up the result and the line buffer */
410 result = (char **) pg_malloc((nlines + 2) * sizeof(char *));
411 buffer = (char *) pg_malloc(maxlength + 2);
413 /* now reprocess the file and store the lines */
415 rewind(infile);
416 nlines = 0;
417 while (fgets(buffer, maxlength + 1, infile) != NULL)
419 result[nlines] = xstrdup(buffer);
420 nlines++;
423 fclose(infile);
424 free(buffer);
425 result[nlines] = NULL;
427 return result;
431 * write an array of lines to a file
433 * This is only used to write text files. Use fopen "w" not PG_BINARY_W
434 * so that the resulting configuration files are nicely editable on Windows.
436 static void
437 writefile(char *path, char **lines)
439 FILE *out_file;
440 char **line;
442 if ((out_file = fopen(path, "w")) == NULL)
444 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
445 progname, path, strerror(errno));
446 exit_nicely();
448 for (line = lines; *line != NULL; line++)
450 if (fputs(*line, out_file) < 0)
452 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
453 progname, path, strerror(errno));
454 exit_nicely();
456 free(*line);
458 if (fclose(out_file))
460 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
461 progname, path, strerror(errno));
462 exit_nicely();
467 * Open a subcommand with suitable error messaging
469 static FILE *
470 popen_check(const char *command, const char *mode)
472 FILE *cmdfd;
474 fflush(stdout);
475 fflush(stderr);
476 errno = 0;
477 cmdfd = popen(command, mode);
478 if (cmdfd == NULL)
479 fprintf(stderr, _("%s: could not execute command \"%s\": %s\n"),
480 progname, command, strerror(errno));
481 return cmdfd;
484 /* source stolen from FreeBSD /src/bin/mkdir/mkdir.c and adapted */
487 * this tries to build all the elements of a path to a directory a la mkdir -p
488 * we assume the path is in canonical form, i.e. uses / as the separator
489 * we also assume it isn't null.
491 * note that on failure, the path arg has been modified to show the particular
492 * directory level we had problems with.
494 static int
495 mkdir_p(char *path, mode_t omode)
497 struct stat sb;
498 mode_t numask,
499 oumask;
500 int first,
501 last,
502 retval;
503 char *p;
505 p = path;
506 oumask = 0;
507 retval = 0;
509 #ifdef WIN32
510 /* skip network and drive specifiers for win32 */
511 if (strlen(p) >= 2)
513 if (p[0] == '/' && p[1] == '/')
515 /* network drive */
516 p = strstr(p + 2, "/");
517 if (p == NULL)
518 return 1;
520 else if (p[1] == ':' &&
521 ((p[0] >= 'a' && p[0] <= 'z') ||
522 (p[0] >= 'A' && p[0] <= 'Z')))
524 /* local drive */
525 p += 2;
528 #endif
530 if (p[0] == '/') /* Skip leading '/'. */
531 ++p;
532 for (first = 1, last = 0; !last; ++p)
534 if (p[0] == '\0')
535 last = 1;
536 else if (p[0] != '/')
537 continue;
538 *p = '\0';
539 if (!last && p[1] == '\0')
540 last = 1;
541 if (first)
544 * POSIX 1003.2: For each dir operand that does not name an
545 * existing directory, effects equivalent to those caused by the
546 * following command shall occcur:
548 * mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
549 * dir
551 * We change the user's umask and then restore it, instead of
552 * doing chmod's.
554 oumask = umask(0);
555 numask = oumask & ~(S_IWUSR | S_IXUSR);
556 (void) umask(numask);
557 first = 0;
559 if (last)
560 (void) umask(oumask);
562 /* check for pre-existing directory; ok if it's a parent */
563 if (stat(path, &sb) == 0)
565 if (!S_ISDIR(sb.st_mode))
567 if (last)
568 errno = EEXIST;
569 else
570 errno = ENOTDIR;
571 retval = 1;
572 break;
575 else if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
577 retval = 1;
578 break;
580 if (!last)
581 *p = '/';
583 if (!first && !last)
584 (void) umask(oumask);
585 return retval;
589 * clean up any files we created on failure
590 * if we created the data directory remove it too
592 static void
593 exit_nicely(void)
595 if (!noclean)
597 if (made_new_pgdata)
599 fprintf(stderr, _("%s: removing data directory \"%s\"\n"),
600 progname, pg_data);
601 if (!rmtree(pg_data, true))
602 fprintf(stderr, _("%s: failed to remove data directory\n"),
603 progname);
605 else if (found_existing_pgdata)
607 fprintf(stderr,
608 _("%s: removing contents of data directory \"%s\"\n"),
609 progname, pg_data);
610 if (!rmtree(pg_data, false))
611 fprintf(stderr, _("%s: failed to remove contents of data directory\n"),
612 progname);
615 if (made_new_xlogdir)
617 fprintf(stderr, _("%s: removing transaction log directory \"%s\"\n"),
618 progname, xlog_dir);
619 if (!rmtree(xlog_dir, true))
620 fprintf(stderr, _("%s: failed to remove transaction log directory\n"),
621 progname);
623 else if (found_existing_xlogdir)
625 fprintf(stderr,
626 _("%s: removing contents of transaction log directory \"%s\"\n"),
627 progname, xlog_dir);
628 if (!rmtree(xlog_dir, false))
629 fprintf(stderr, _("%s: failed to remove contents of transaction log directory\n"),
630 progname);
632 /* otherwise died during startup, do nothing! */
634 else
636 if (made_new_pgdata || found_existing_pgdata)
637 fprintf(stderr,
638 _("%s: data directory \"%s\" not removed at user's request\n"),
639 progname, pg_data);
641 if (made_new_xlogdir || found_existing_xlogdir)
642 fprintf(stderr,
643 _("%s: transaction log directory \"%s\" not removed at user's request\n"),
644 progname, xlog_dir);
647 exit(1);
651 * find the current user
653 * on unix make sure it isn't really root
655 static char *
656 get_id(void)
658 #ifndef WIN32
660 struct passwd *pw;
662 pw = getpwuid(geteuid());
664 if (geteuid() == 0) /* 0 is root's uid */
666 fprintf(stderr,
667 _("%s: cannot be run as root\n"
668 "Please log in (using, e.g., \"su\") as the "
669 "(unprivileged) user that will\n"
670 "own the server process.\n"),
671 progname);
672 exit(1);
674 #else /* the windows code */
676 struct passwd_win32
678 int pw_uid;
679 char pw_name[128];
680 } pass_win32;
681 struct passwd_win32 *pw = &pass_win32;
682 DWORD pwname_size = sizeof(pass_win32.pw_name) - 1;
684 pw->pw_uid = 1;
685 GetUserName(pw->pw_name, &pwname_size);
686 #endif
688 return xstrdup(pw->pw_name);
691 static char *
692 encodingid_to_string(int enc)
694 char result[20];
696 sprintf(result, "%d", enc);
697 return xstrdup(result);
701 * get the encoding id for a given encoding name
703 static char *
704 get_encoding_id(char *encoding_name)
706 int enc;
708 if (encoding_name && *encoding_name)
710 if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
711 return encodingid_to_string(enc);
713 fprintf(stderr, _("%s: \"%s\" is not a valid server encoding name\n"),
714 progname, encoding_name ? encoding_name : "(null)");
715 exit(1);
719 * Support for determining the best default text search configuration.
720 * We key this off the first part of LC_CTYPE (ie, the language name).
722 struct tsearch_config_match
724 const char *tsconfname;
725 const char *langname;
728 static const struct tsearch_config_match tsearch_config_languages[] =
730 {"danish", "da"},
731 {"danish", "Danish"},
732 {"dutch", "nl"},
733 {"dutch", "Dutch"},
734 {"english", "C"},
735 {"english", "POSIX"},
736 {"english", "en"},
737 {"english", "English"},
738 {"finnish", "fi"},
739 {"finnish", "Finnish"},
740 {"french", "fr"},
741 {"french", "French"},
742 {"german", "de"},
743 {"german", "German"},
744 {"hungarian", "hu"},
745 {"hungarian", "Hungarian"},
746 {"italian", "it"},
747 {"italian", "Italian"},
748 {"norwegian", "no"},
749 {"norwegian", "Norwegian"},
750 {"portuguese", "pt"},
751 {"portuguese", "Portuguese"},
752 {"romanian", "ro"},
753 {"russian", "ru"},
754 {"russian", "Russian"},
755 {"spanish", "es"},
756 {"spanish", "Spanish"},
757 {"swedish", "sv"},
758 {"swedish", "Swedish"},
759 {"turkish", "tr"},
760 {"turkish", "Turkish"},
761 {NULL, NULL} /* end marker */
765 * Look for a text search configuration matching lc_ctype, and return its
766 * name; return NULL if no match.
768 static const char *
769 find_matching_ts_config(const char *lc_type)
771 int i;
772 char *langname,
773 *ptr;
776 * Convert lc_ctype to a language name by stripping everything after an
777 * underscore. Just for paranoia, we also stop at '.' or '@'.
779 if (lc_type == NULL)
780 langname = xstrdup("");
781 else
783 ptr = langname = xstrdup(lc_type);
784 while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@')
785 ptr++;
786 *ptr = '\0';
789 for (i = 0; tsearch_config_languages[i].tsconfname; i++)
791 if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
793 free(langname);
794 return tsearch_config_languages[i].tsconfname;
798 free(langname);
799 return NULL;
804 * get short version of VERSION
806 static char *
807 get_short_version(void)
809 bool gotdot = false;
810 int end;
811 char *vr;
813 vr = xstrdup(PG_VERSION);
815 for (end = 0; vr[end] != '\0'; end++)
817 if (vr[end] == '.')
819 if (end == 0)
820 return NULL;
821 else if (gotdot)
822 break;
823 else
824 gotdot = true;
826 else if (vr[end] < '0' || vr[end] > '9')
828 /* gone past digits and dots */
829 break;
832 if (end == 0 || vr[end - 1] == '.' || !gotdot)
833 return NULL;
835 vr[end] = '\0';
836 return vr;
840 * make sure the directory either doesn't exist or is empty
842 * Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
843 * or -1 if trouble accessing directory
845 static int
846 check_data_dir(char *dir)
848 DIR *chkdir;
849 struct dirent *file;
850 int result = 1;
852 errno = 0;
854 chkdir = opendir(dir);
856 if (!chkdir)
857 return (errno == ENOENT) ? 0 : -1;
859 while ((file = readdir(chkdir)) != NULL)
861 if (strcmp(".", file->d_name) == 0 ||
862 strcmp("..", file->d_name) == 0)
864 /* skip this and parent directory */
865 continue;
867 else
869 result = 2; /* not empty */
870 break;
874 #ifdef WIN32
877 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
878 * released version
880 if (GetLastError() == ERROR_NO_MORE_FILES)
881 errno = 0;
882 #endif
884 closedir(chkdir);
886 if (errno != 0)
887 result = -1; /* some kind of I/O error? */
889 return result;
893 * make the data directory (or one of its subdirectories if subdir is not NULL)
895 static bool
896 mkdatadir(const char *subdir)
898 char *path;
900 path = pg_malloc(strlen(pg_data) + 2 +
901 (subdir == NULL ? 0 : strlen(subdir)));
903 if (subdir != NULL)
904 sprintf(path, "%s/%s", pg_data, subdir);
905 else
906 strcpy(path, pg_data);
908 if (mkdir_p(path, 0700) == 0)
909 return true;
911 fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
912 progname, path, strerror(errno));
914 return false;
919 * set name of given input file variable under data directory
921 static void
922 set_input(char **dest, char *filename)
924 *dest = pg_malloc(strlen(share_path) + strlen(filename) + 2);
925 sprintf(*dest, "%s/%s", share_path, filename);
929 * check that given input file exists
931 static void
932 check_input(char *path)
934 struct stat statbuf;
936 if (stat(path, &statbuf) != 0)
938 if (errno == ENOENT)
940 fprintf(stderr,
941 _("%s: file \"%s\" does not exist\n"), progname, path);
942 fprintf(stderr,
943 _("This might mean you have a corrupted installation or identified\n"
944 "the wrong directory with the invocation option -L.\n"));
946 else
948 fprintf(stderr,
949 _("%s: could not access file \"%s\": %s\n"), progname, path,
950 strerror(errno));
951 fprintf(stderr,
952 _("This might mean you have a corrupted installation or identified\n"
953 "the wrong directory with the invocation option -L.\n"));
955 exit(1);
957 if (!S_ISREG(statbuf.st_mode))
959 fprintf(stderr,
960 _("%s: file \"%s\" is not a regular file\n"), progname, path);
961 fprintf(stderr,
962 _("This might mean you have a corrupted installation or identified\n"
963 "the wrong directory with the invocation option -L.\n"));
964 exit(1);
969 * write out the PG_VERSION file in the data dir, or its subdirectory
970 * if extrapath is not NULL
972 static void
973 set_short_version(char *short_version, char *extrapath)
975 FILE *version_file;
976 char *path;
978 if (extrapath == NULL)
980 path = pg_malloc(strlen(pg_data) + 12);
981 sprintf(path, "%s/PG_VERSION", pg_data);
983 else
985 path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13);
986 sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath);
988 version_file = fopen(path, PG_BINARY_W);
989 if (version_file == NULL)
991 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
992 progname, path, strerror(errno));
993 exit_nicely();
995 if (fprintf(version_file, "%s\n", short_version) < 0 ||
996 fclose(version_file))
998 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
999 progname, path, strerror(errno));
1000 exit_nicely();
1002 free(path);
1006 * set up an empty config file so we can check config settings by launching
1007 * a test backend
1009 static void
1010 set_null_conf(void)
1012 FILE *conf_file;
1013 char *path;
1015 path = pg_malloc(strlen(pg_data) + 17);
1016 sprintf(path, "%s/postgresql.conf", pg_data);
1017 conf_file = fopen(path, PG_BINARY_W);
1018 if (conf_file == NULL)
1020 fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
1021 progname, path, strerror(errno));
1022 exit_nicely();
1024 if (fclose(conf_file))
1026 fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
1027 progname, path, strerror(errno));
1028 exit_nicely();
1030 free(path);
1034 * Determine platform-specific config settings
1036 * Use reasonable values if kernel will let us, else scale back. Probe
1037 * for max_connections first since it is subject to more constraints than
1038 * shared_buffers.
1040 static void
1041 test_config_settings(void)
1044 * These macros define the minimum shared_buffers we want for a given
1045 * max_connections value, and the max_fsm_pages setting to be used for a
1046 * given shared_buffers value. The arrays show the settings to try.
1049 #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
1050 #define FSM_FOR_BUFS(nbuffers) ((nbuffers) > 1000 ? 50 * (nbuffers) : 20000)
1052 static const int trial_conns[] = {
1053 100, 50, 40, 30, 20, 10
1055 static const int trial_bufs[] = {
1056 4096, 3584, 3072, 2560, 2048, 1536,
1057 1000, 900, 800, 700, 600, 500,
1058 400, 300, 200, 100, 50
1061 char cmd[MAXPGPATH];
1062 const int connslen = sizeof(trial_conns) / sizeof(int);
1063 const int bufslen = sizeof(trial_bufs) / sizeof(int);
1064 int i,
1065 status,
1066 test_conns,
1067 test_buffs,
1068 test_max_fsm,
1069 ok_buffers = 0;
1072 printf(_("selecting default max_connections ... "));
1073 fflush(stdout);
1075 for (i = 0; i < connslen; i++)
1077 test_conns = trial_conns[i];
1078 test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
1079 test_max_fsm = FSM_FOR_BUFS(test_buffs);
1081 snprintf(cmd, sizeof(cmd),
1082 SYSTEMQUOTE "\"%s\" --boot -x0 %s "
1083 "-c max_connections=%d "
1084 "-c shared_buffers=%d "
1085 "-c max_fsm_pages=%d "
1086 "< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
1087 backend_exec, boot_options,
1088 test_conns, test_buffs, test_max_fsm,
1089 DEVNULL, DEVNULL);
1090 status = system(cmd);
1091 if (status == 0)
1093 ok_buffers = test_buffs;
1094 break;
1097 if (i >= connslen)
1098 i = connslen - 1;
1099 n_connections = trial_conns[i];
1101 printf("%d\n", n_connections);
1103 printf(_("selecting default shared_buffers/max_fsm_pages ... "));
1104 fflush(stdout);
1106 for (i = 0; i < bufslen; i++)
1108 /* Use same amount of memory, independent of BLCKSZ */
1109 test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
1110 if (test_buffs <= ok_buffers)
1112 test_buffs = ok_buffers;
1113 break;
1115 test_max_fsm = FSM_FOR_BUFS(test_buffs);
1117 snprintf(cmd, sizeof(cmd),
1118 SYSTEMQUOTE "\"%s\" --boot -x0 %s "
1119 "-c max_connections=%d "
1120 "-c shared_buffers=%d "
1121 "-c max_fsm_pages=%d "
1122 "< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE,
1123 backend_exec, boot_options,
1124 n_connections, test_buffs, test_max_fsm,
1125 DEVNULL, DEVNULL);
1126 status = system(cmd);
1127 if (status == 0)
1128 break;
1130 n_buffers = test_buffs;
1131 n_fsm_pages = FSM_FOR_BUFS(n_buffers);
1133 if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1134 printf("%dMB/%d\n", (n_buffers * (BLCKSZ / 1024)) / 1024, n_fsm_pages);
1135 else
1136 printf("%dkB/%d\n", n_buffers * (BLCKSZ / 1024), n_fsm_pages);
1140 * set up all the config files
1142 static void
1143 setup_config(void)
1145 char **conflines;
1146 char repltok[100];
1147 char path[MAXPGPATH];
1149 fputs(_("creating configuration files ... "), stdout);
1150 fflush(stdout);
1152 /* postgresql.conf */
1154 conflines = readfile(conf_file);
1156 snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
1157 conflines = replace_token(conflines, "#max_connections = 100", repltok);
1159 if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1160 snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
1161 (n_buffers * (BLCKSZ / 1024)) / 1024);
1162 else
1163 snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
1164 n_buffers * (BLCKSZ / 1024));
1165 conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
1167 snprintf(repltok, sizeof(repltok), "max_fsm_pages = %d", n_fsm_pages);
1168 conflines = replace_token(conflines, "#max_fsm_pages = 204800", repltok);
1170 #if DEF_PGPORT != 5432
1171 snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
1172 conflines = replace_token(conflines, "#port = 5432", repltok);
1173 #endif
1175 snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
1176 escape_quotes(lc_messages));
1177 conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
1179 snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
1180 escape_quotes(lc_monetary));
1181 conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
1183 snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
1184 escape_quotes(lc_numeric));
1185 conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
1187 snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
1188 escape_quotes(lc_time));
1189 conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
1191 switch (locale_date_order(lc_time))
1193 case DATEORDER_YMD:
1194 strcpy(repltok, "datestyle = 'iso, ymd'");
1195 break;
1196 case DATEORDER_DMY:
1197 strcpy(repltok, "datestyle = 'iso, dmy'");
1198 break;
1199 case DATEORDER_MDY:
1200 default:
1201 strcpy(repltok, "datestyle = 'iso, mdy'");
1202 break;
1204 conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
1206 snprintf(repltok, sizeof(repltok),
1207 "default_text_search_config = 'pg_catalog.%s'",
1208 escape_quotes(default_text_search_config));
1209 conflines = replace_token(conflines,
1210 "#default_text_search_config = 'pg_catalog.simple'",
1211 repltok);
1213 snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1215 writefile(path, conflines);
1216 chmod(path, 0600);
1218 free(conflines);
1221 /* pg_hba.conf */
1223 conflines = readfile(hba_file);
1225 #ifndef HAVE_UNIX_SOCKETS
1226 conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
1227 #else
1228 conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
1229 #endif
1231 #ifdef HAVE_IPV6
1234 * Probe to see if there is really any platform support for IPv6, and
1235 * comment out the relevant pg_hba line if not. This avoids runtime
1236 * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1237 * useful on Windows, where executables built on a machine with IPv6 may
1238 * have to run on a machine without.
1241 struct addrinfo *gai_result;
1242 struct addrinfo hints;
1243 int err = 0;
1245 #ifdef WIN32
1246 /* need to call WSAStartup before calling getaddrinfo */
1247 WSADATA wsaData;
1249 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1250 #endif
1252 /* for best results, this code should match parse_hba() */
1253 hints.ai_flags = AI_NUMERICHOST;
1254 hints.ai_family = PF_UNSPEC;
1255 hints.ai_socktype = 0;
1256 hints.ai_protocol = 0;
1257 hints.ai_addrlen = 0;
1258 hints.ai_canonname = NULL;
1259 hints.ai_addr = NULL;
1260 hints.ai_next = NULL;
1262 if (err != 0 ||
1263 getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1264 conflines = replace_token(conflines,
1265 "host all all ::1",
1266 "#host all all ::1");
1268 #else /* !HAVE_IPV6 */
1269 /* If we didn't compile IPV6 support at all, always comment it out */
1270 conflines = replace_token(conflines,
1271 "host all all ::1",
1272 "#host all all ::1");
1273 #endif /* HAVE_IPV6 */
1275 /* Replace default authentication methods */
1276 conflines = replace_token(conflines,
1277 "@authmethod@",
1278 authmethod);
1280 conflines = replace_token(conflines,
1281 "@authcomment@",
1282 strcmp(authmethod, "trust") ? "" : AUTHTRUST_WARNING);
1284 snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1286 writefile(path, conflines);
1287 chmod(path, 0600);
1289 free(conflines);
1291 /* pg_ident.conf */
1293 conflines = readfile(ident_file);
1295 snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1297 writefile(path, conflines);
1298 chmod(path, 0600);
1300 free(conflines);
1302 check_ok();
1307 * run the BKI script in bootstrap mode to create template1
1309 static void
1310 bootstrap_template1(char *short_version)
1312 PG_CMD_DECL;
1313 char **line;
1314 char *talkargs = "";
1315 char **bki_lines;
1316 char headerline[MAXPGPATH];
1317 char buf[64];
1319 printf(_("creating template1 database in %s/base/1 ... "), pg_data);
1320 fflush(stdout);
1322 if (debug)
1323 talkargs = "-d 5";
1325 bki_lines = readfile(bki_file);
1327 /* Check that bki file appears to be of the right version */
1329 snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1330 short_version);
1332 if (strcmp(headerline, *bki_lines) != 0)
1334 fprintf(stderr,
1335 _("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
1336 "Check your installation or specify the correct path "
1337 "using the option -L.\n"),
1338 progname, bki_file, PG_VERSION);
1339 exit_nicely();
1342 /* Substitute for various symbols used in the BKI file */
1344 sprintf(buf, "%d", NAMEDATALEN);
1345 bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1347 bki_lines = replace_token(bki_lines, "FLOAT4PASSBYVAL",
1348 FLOAT4PASSBYVAL ? "true" : "false");
1350 bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1351 FLOAT8PASSBYVAL ? "true" : "false");
1353 bki_lines = replace_token(bki_lines, "POSTGRES", username);
1355 bki_lines = replace_token(bki_lines, "ENCODING", encodingid);
1357 bki_lines = replace_token(bki_lines, "LC_COLLATE", lc_collate);
1359 bki_lines = replace_token(bki_lines, "LC_CTYPE", lc_ctype);
1362 * Pass correct LC_xxx environment to bootstrap.
1364 * The shell script arranged to restore the LC settings afterwards, but
1365 * there doesn't seem to be any compelling reason to do that.
1367 snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate);
1368 putenv(xstrdup(cmd));
1370 snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
1371 putenv(xstrdup(cmd));
1373 unsetenv("LC_ALL");
1375 /* Also ensure backend isn't confused by this environment var: */
1376 unsetenv("PGCLIENTENCODING");
1378 snprintf(cmd, sizeof(cmd),
1379 "\"%s\" --boot -x1 %s %s",
1380 backend_exec, boot_options, talkargs);
1382 PG_CMD_OPEN;
1384 for (line = bki_lines; *line != NULL; line++)
1386 PG_CMD_PUTS(*line);
1387 free(*line);
1390 PG_CMD_CLOSE;
1392 free(bki_lines);
1394 check_ok();
1398 * set up the shadow password table
1400 static void
1401 setup_auth(void)
1403 PG_CMD_DECL;
1404 const char **line;
1405 static const char *pg_authid_setup[] = {
1407 * Create triggers to ensure manual updates to shared catalogs will be
1408 * reflected into their "flat file" copies.
1410 "CREATE TRIGGER pg_sync_pg_database "
1411 " AFTER INSERT OR UPDATE OR DELETE ON pg_database "
1412 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1413 "CREATE TRIGGER pg_sync_pg_authid "
1414 " AFTER INSERT OR UPDATE OR DELETE ON pg_authid "
1415 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1416 "CREATE TRIGGER pg_sync_pg_auth_members "
1417 " AFTER INSERT OR UPDATE OR DELETE ON pg_auth_members "
1418 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1421 * The authid table shouldn't be readable except through views, to
1422 * ensure passwords are not publicly visible.
1424 "REVOKE ALL on pg_authid FROM public;\n",
1425 NULL
1428 fputs(_("initializing pg_authid ... "), stdout);
1429 fflush(stdout);
1431 snprintf(cmd, sizeof(cmd),
1432 "\"%s\" %s template1 >%s",
1433 backend_exec, backend_options,
1434 DEVNULL);
1436 PG_CMD_OPEN;
1438 for (line = pg_authid_setup; *line != NULL; line++)
1439 PG_CMD_PUTS(*line);
1441 PG_CMD_CLOSE;
1443 check_ok();
1447 * get the superuser password if required, and call postgres to set it
1449 static void
1450 get_set_pwd(void)
1452 PG_CMD_DECL;
1454 char *pwd1,
1455 *pwd2;
1456 char pwdpath[MAXPGPATH];
1457 struct stat statbuf;
1459 if (pwprompt)
1462 * Read password from terminal
1464 pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
1465 pwd2 = simple_prompt("Enter it again: ", 100, false);
1466 if (strcmp(pwd1, pwd2) != 0)
1468 fprintf(stderr, _("Passwords didn't match.\n"));
1469 exit_nicely();
1471 free(pwd2);
1473 else
1476 * Read password from file
1478 * Ideally this should insist that the file not be world-readable.
1479 * However, this option is mainly intended for use on Windows where
1480 * file permissions may not exist at all, so we'll skip the paranoia
1481 * for now.
1483 FILE *pwf = fopen(pwfilename, "r");
1484 char pwdbuf[MAXPGPATH];
1485 int i;
1487 if (!pwf)
1489 fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1490 progname, pwfilename, strerror(errno));
1491 exit_nicely();
1493 if (!fgets(pwdbuf, sizeof(pwdbuf), pwf))
1495 fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
1496 progname, pwfilename, strerror(errno));
1497 exit_nicely();
1499 fclose(pwf);
1501 i = strlen(pwdbuf);
1502 while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
1503 pwdbuf[--i] = '\0';
1505 pwd1 = xstrdup(pwdbuf);
1508 printf(_("setting password ... "));
1509 fflush(stdout);
1511 snprintf(cmd, sizeof(cmd),
1512 "\"%s\" %s template1 >%s",
1513 backend_exec, backend_options,
1514 DEVNULL);
1516 PG_CMD_OPEN;
1518 PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n",
1519 username, escape_quotes(pwd1));
1521 /* MM: pwd1 is no longer needed, freeing it */
1522 free(pwd1);
1524 PG_CMD_CLOSE;
1526 check_ok();
1528 snprintf(pwdpath, sizeof(pwdpath), "%s/global/pg_auth", pg_data);
1529 if (stat(pwdpath, &statbuf) != 0 || !S_ISREG(statbuf.st_mode))
1531 fprintf(stderr,
1532 _("%s: The password file was not generated. "
1533 "Please report this problem.\n"),
1534 progname);
1535 exit_nicely();
1540 * set up pg_depend
1542 static void
1543 setup_depend(void)
1545 PG_CMD_DECL;
1546 const char **line;
1547 static const char *pg_depend_setup[] = {
1549 * Make PIN entries in pg_depend for all objects made so far in the
1550 * tables that the dependency code handles. This is overkill (the
1551 * system doesn't really depend on having every last weird datatype,
1552 * for instance) but generating only the minimum required set of
1553 * dependencies seems hard.
1555 * Note that we deliberately do not pin the system views, which
1556 * haven't been created yet. Also, no conversions, databases, or
1557 * tablespaces are pinned.
1559 * First delete any already-made entries; PINs override all else, and
1560 * must be the only entries for their objects.
1562 "DELETE FROM pg_depend;\n",
1563 "VACUUM pg_depend;\n",
1564 "DELETE FROM pg_shdepend;\n",
1565 "VACUUM pg_shdepend;\n",
1567 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1568 " FROM pg_class;\n",
1569 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1570 " FROM pg_proc;\n",
1571 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1572 " FROM pg_type;\n",
1573 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1574 " FROM pg_cast;\n",
1575 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1576 " FROM pg_constraint;\n",
1577 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1578 " FROM pg_attrdef;\n",
1579 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1580 " FROM pg_language;\n",
1581 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1582 " FROM pg_operator;\n",
1583 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1584 " FROM pg_opclass;\n",
1585 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1586 " FROM pg_opfamily;\n",
1587 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1588 " FROM pg_amop;\n",
1589 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1590 " FROM pg_amproc;\n",
1591 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1592 " FROM pg_rewrite;\n",
1593 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1594 " FROM pg_trigger;\n",
1597 * restriction here to avoid pinning the public namespace
1599 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1600 " FROM pg_namespace "
1601 " WHERE nspname LIKE 'pg%';\n",
1603 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1604 " FROM pg_ts_parser;\n",
1605 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1606 " FROM pg_ts_dict;\n",
1607 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1608 " FROM pg_ts_template;\n",
1609 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1610 " FROM pg_ts_config;\n",
1611 "INSERT INTO pg_shdepend SELECT 0, 0, 0, tableoid, oid, 'p' "
1612 " FROM pg_authid;\n",
1613 NULL
1616 fputs(_("initializing dependencies ... "), stdout);
1617 fflush(stdout);
1619 snprintf(cmd, sizeof(cmd),
1620 "\"%s\" %s template1 >%s",
1621 backend_exec, backend_options,
1622 DEVNULL);
1624 PG_CMD_OPEN;
1626 for (line = pg_depend_setup; *line != NULL; line++)
1627 PG_CMD_PUTS(*line);
1629 PG_CMD_CLOSE;
1631 check_ok();
1635 * set up system views
1637 static void
1638 setup_sysviews(void)
1640 PG_CMD_DECL;
1641 char **line;
1642 char **sysviews_setup;
1644 fputs(_("creating system views ... "), stdout);
1645 fflush(stdout);
1647 sysviews_setup = readfile(system_views_file);
1650 * We use -j here to avoid backslashing stuff in system_views.sql
1652 snprintf(cmd, sizeof(cmd),
1653 "\"%s\" %s -j template1 >%s",
1654 backend_exec, backend_options,
1655 DEVNULL);
1657 PG_CMD_OPEN;
1659 for (line = sysviews_setup; *line != NULL; line++)
1661 PG_CMD_PUTS(*line);
1662 free(*line);
1665 PG_CMD_CLOSE;
1667 free(sysviews_setup);
1669 check_ok();
1673 * load description data
1675 static void
1676 setup_description(void)
1678 PG_CMD_DECL;
1680 fputs(_("loading system objects' descriptions ... "), stdout);
1681 fflush(stdout);
1683 snprintf(cmd, sizeof(cmd),
1684 "\"%s\" %s template1 >%s",
1685 backend_exec, backend_options,
1686 DEVNULL);
1688 PG_CMD_OPEN;
1690 PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_description ( "
1691 " objoid oid, "
1692 " classname name, "
1693 " objsubid int4, "
1694 " description text) WITHOUT OIDS;\n");
1696 PG_CMD_PRINTF1("COPY tmp_pg_description FROM E'%s';\n",
1697 escape_quotes(desc_file));
1699 PG_CMD_PUTS("INSERT INTO pg_description "
1700 " SELECT t.objoid, c.oid, t.objsubid, t.description "
1701 " FROM tmp_pg_description t, pg_class c "
1702 " WHERE c.relname = t.classname;\n");
1704 PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_shdescription ( "
1705 " objoid oid, "
1706 " classname name, "
1707 " description text) WITHOUT OIDS;\n");
1709 PG_CMD_PRINTF1("COPY tmp_pg_shdescription FROM E'%s';\n",
1710 escape_quotes(shdesc_file));
1712 PG_CMD_PUTS("INSERT INTO pg_shdescription "
1713 " SELECT t.objoid, c.oid, t.description "
1714 " FROM tmp_pg_shdescription t, pg_class c "
1715 " WHERE c.relname = t.classname;\n");
1717 PG_CMD_CLOSE;
1719 check_ok();
1723 * load conversion functions
1725 static void
1726 setup_conversion(void)
1728 PG_CMD_DECL;
1729 char **line;
1730 char **conv_lines;
1732 fputs(_("creating conversions ... "), stdout);
1733 fflush(stdout);
1735 snprintf(cmd, sizeof(cmd),
1736 "\"%s\" %s template1 >%s",
1737 backend_exec, backend_options,
1738 DEVNULL);
1740 PG_CMD_OPEN;
1742 conv_lines = readfile(conversion_file);
1743 for (line = conv_lines; *line != NULL; line++)
1745 if (strstr(*line, "DROP CONVERSION") != *line)
1746 PG_CMD_PUTS(*line);
1747 free(*line);
1750 free(conv_lines);
1752 PG_CMD_CLOSE;
1754 check_ok();
1758 * load extra dictionaries (Snowball stemmers)
1760 static void
1761 setup_dictionary(void)
1763 PG_CMD_DECL;
1764 char **line;
1765 char **conv_lines;
1767 fputs(_("creating dictionaries ... "), stdout);
1768 fflush(stdout);
1771 * We use -j here to avoid backslashing stuff
1773 snprintf(cmd, sizeof(cmd),
1774 "\"%s\" %s -j template1 >%s",
1775 backend_exec, backend_options,
1776 DEVNULL);
1778 PG_CMD_OPEN;
1780 conv_lines = readfile(dictionary_file);
1781 for (line = conv_lines; *line != NULL; line++)
1783 PG_CMD_PUTS(*line);
1784 free(*line);
1787 free(conv_lines);
1789 PG_CMD_CLOSE;
1791 check_ok();
1795 * Set up privileges
1797 * We mark most system catalogs as world-readable. We don't currently have
1798 * to touch functions, languages, or databases, because their default
1799 * permissions are OK.
1801 * Some objects may require different permissions by default, so we
1802 * make sure we don't overwrite privilege sets that have already been
1803 * set (NOT NULL).
1805 static void
1806 setup_privileges(void)
1808 PG_CMD_DECL;
1809 char **line;
1810 char **priv_lines;
1811 static char *privileges_setup[] = {
1812 "UPDATE pg_class "
1813 " SET relacl = E'{\"=r/\\\\\"$POSTGRES_SUPERUSERNAME\\\\\"\"}' "
1814 " WHERE relkind IN ('r', 'v', 'S') AND relacl IS NULL;\n",
1815 "GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n",
1816 "GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n",
1817 NULL
1820 fputs(_("setting privileges on built-in objects ... "), stdout);
1821 fflush(stdout);
1823 snprintf(cmd, sizeof(cmd),
1824 "\"%s\" %s template1 >%s",
1825 backend_exec, backend_options,
1826 DEVNULL);
1828 PG_CMD_OPEN;
1830 priv_lines = replace_token(privileges_setup,
1831 "$POSTGRES_SUPERUSERNAME", username);
1832 for (line = priv_lines; *line != NULL; line++)
1833 PG_CMD_PUTS(*line);
1835 PG_CMD_CLOSE;
1837 check_ok();
1841 * extract the strange version of version required for information schema
1842 * (09.08.0007abc)
1844 static void
1845 set_info_version(void)
1847 char *letterversion;
1848 long major = 0,
1849 minor = 0,
1850 micro = 0;
1851 char *endptr;
1852 char *vstr = xstrdup(PG_VERSION);
1853 char *ptr;
1855 ptr = vstr + (strlen(vstr) - 1);
1856 while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1857 ptr--;
1858 letterversion = ptr + 1;
1859 major = strtol(vstr, &endptr, 10);
1860 if (*endptr)
1861 minor = strtol(endptr + 1, &endptr, 10);
1862 if (*endptr)
1863 micro = strtol(endptr + 1, &endptr, 10);
1864 snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1865 major, minor, micro, letterversion);
1869 * load info schema and populate from features file
1871 static void
1872 setup_schema(void)
1874 PG_CMD_DECL;
1875 char **line;
1876 char **lines;
1878 fputs(_("creating information schema ... "), stdout);
1879 fflush(stdout);
1881 lines = readfile(info_schema_file);
1884 * We use -j here to avoid backslashing stuff in information_schema.sql
1886 snprintf(cmd, sizeof(cmd),
1887 "\"%s\" %s -j template1 >%s",
1888 backend_exec, backend_options,
1889 DEVNULL);
1891 PG_CMD_OPEN;
1893 for (line = lines; *line != NULL; line++)
1895 PG_CMD_PUTS(*line);
1896 free(*line);
1899 free(lines);
1901 PG_CMD_CLOSE;
1903 snprintf(cmd, sizeof(cmd),
1904 "\"%s\" %s template1 >%s",
1905 backend_exec, backend_options,
1906 DEVNULL);
1908 PG_CMD_OPEN;
1910 PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "
1911 " SET character_value = '%s' "
1912 " WHERE implementation_info_name = 'DBMS VERSION';\n",
1913 infoversion);
1915 PG_CMD_PRINTF1("COPY information_schema.sql_features "
1916 " (feature_id, feature_name, sub_feature_id, "
1917 " sub_feature_name, is_supported, comments) "
1918 " FROM E'%s';\n",
1919 escape_quotes(features_file));
1921 PG_CMD_CLOSE;
1923 check_ok();
1927 * clean everything up in template1
1929 static void
1930 vacuum_db(void)
1932 PG_CMD_DECL;
1934 fputs(_("vacuuming database template1 ... "), stdout);
1935 fflush(stdout);
1937 snprintf(cmd, sizeof(cmd),
1938 "\"%s\" %s template1 >%s",
1939 backend_exec, backend_options,
1940 DEVNULL);
1942 PG_CMD_OPEN;
1944 PG_CMD_PUTS("ANALYZE;\nVACUUM FULL;\nVACUUM FREEZE;\n");
1946 PG_CMD_CLOSE;
1948 check_ok();
1952 * copy template1 to template0
1954 static void
1955 make_template0(void)
1957 PG_CMD_DECL;
1958 const char **line;
1959 static const char *template0_setup[] = {
1960 "CREATE DATABASE template0;\n",
1961 "UPDATE pg_database SET "
1962 " datistemplate = 't', "
1963 " datallowconn = 'f' "
1964 " WHERE datname = 'template0';\n",
1967 * We use the OID of template0 to determine lastsysoid
1969 "UPDATE pg_database SET datlastsysoid = "
1970 " (SELECT oid FROM pg_database "
1971 " WHERE datname = 'template0');\n",
1974 * Explicitly revoke public create-schema and create-temp-table
1975 * privileges in template1 and template0; else the latter would be on
1976 * by default
1978 "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n",
1979 "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n",
1982 * Finally vacuum to clean up dead rows in pg_database
1984 "VACUUM FULL pg_database;\n",
1985 NULL
1988 fputs(_("copying template1 to template0 ... "), stdout);
1989 fflush(stdout);
1991 snprintf(cmd, sizeof(cmd),
1992 "\"%s\" %s template1 >%s",
1993 backend_exec, backend_options,
1994 DEVNULL);
1996 PG_CMD_OPEN;
1998 for (line = template0_setup; *line; line++)
1999 PG_CMD_PUTS(*line);
2001 PG_CMD_CLOSE;
2003 check_ok();
2007 * copy template1 to postgres
2009 static void
2010 make_postgres(void)
2012 PG_CMD_DECL;
2013 const char **line;
2014 static const char *postgres_setup[] = {
2015 "CREATE DATABASE postgres;\n",
2016 NULL
2019 fputs(_("copying template1 to postgres ... "), stdout);
2020 fflush(stdout);
2022 snprintf(cmd, sizeof(cmd),
2023 "\"%s\" %s template1 >%s",
2024 backend_exec, backend_options,
2025 DEVNULL);
2027 PG_CMD_OPEN;
2029 for (line = postgres_setup; *line; line++)
2030 PG_CMD_PUTS(*line);
2032 PG_CMD_CLOSE;
2034 check_ok();
2039 * signal handler in case we are interrupted.
2041 * The Windows runtime docs at
2042 * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_signal.asp
2043 * specifically forbid a number of things being done from a signal handler,
2044 * including IO, memory allocation and system calls, and only allow jmpbuf
2045 * if you are handling SIGFPE.
2047 * I avoided doing the forbidden things by setting a flag instead of calling
2048 * exit_nicely() directly.
2050 * Also note the behaviour of Windows with SIGINT, which says this:
2051 * Note SIGINT is not supported for any Win32 application, including
2052 * Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs,
2053 * Win32 operating systems generate a new thread to specifically handle
2054 * that interrupt. This can cause a single-thread application such as UNIX,
2055 * to become multithreaded, resulting in unexpected behavior.
2057 * I have no idea how to handle this. (Strange they call UNIX an application!)
2058 * So this will need some testing on Windows.
2060 static void
2061 trapsig(int signum)
2063 /* handle systems that reset the handler, like Windows (grr) */
2064 pqsignal(signum, trapsig);
2065 caught_signal = true;
2069 * call exit_nicely() if we got a signal, or else output "ok".
2071 static void
2072 check_ok(void)
2074 if (caught_signal)
2076 printf(_("caught signal\n"));
2077 fflush(stdout);
2078 exit_nicely();
2080 else if (output_failed)
2082 printf(_("could not write to child process: %s\n"),
2083 strerror(output_errno));
2084 fflush(stdout);
2085 exit_nicely();
2087 else
2089 /* all seems well */
2090 printf(_("ok\n"));
2091 fflush(stdout);
2096 * Escape (by doubling) any single quotes or backslashes in given string
2098 * Note: this is used to process both postgresql.conf entries and SQL
2099 * string literals. Since postgresql.conf strings are defined to treat
2100 * backslashes as escapes, we have to double backslashes here. Hence,
2101 * when using this for a SQL string literal, use E'' syntax.
2103 * We do not need to worry about encoding considerations because all
2104 * valid backend encodings are ASCII-safe.
2106 static char *
2107 escape_quotes(const char *src)
2109 int len = strlen(src),
2112 char *result = pg_malloc(len * 2 + 1);
2114 for (i = 0, j = 0; i < len; i++)
2116 if (SQL_STR_DOUBLE(src[i], true))
2117 result[j++] = src[i];
2118 result[j++] = src[i];
2120 result[j] = '\0';
2121 return result;
2124 /* Hack to suppress a warning about %x from some versions of gcc */
2125 static inline size_t
2126 my_strftime(char *s, size_t max, const char *fmt, const struct tm * tm)
2128 return strftime(s, max, fmt, tm);
2132 * Determine likely date order from locale
2134 static int
2135 locale_date_order(const char *locale)
2137 struct tm testtime;
2138 char buf[128];
2139 char *posD;
2140 char *posM;
2141 char *posY;
2142 char *save;
2143 size_t res;
2144 int result;
2146 result = DATEORDER_MDY; /* default */
2148 save = setlocale(LC_TIME, NULL);
2149 if (!save)
2150 return result;
2151 save = xstrdup(save);
2153 setlocale(LC_TIME, locale);
2155 memset(&testtime, 0, sizeof(testtime));
2156 testtime.tm_mday = 22;
2157 testtime.tm_mon = 10; /* November, should come out as "11" */
2158 testtime.tm_year = 133; /* 2033 */
2160 res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2162 setlocale(LC_TIME, save);
2163 free(save);
2165 if (res == 0)
2166 return result;
2168 posM = strstr(buf, "11");
2169 posD = strstr(buf, "22");
2170 posY = strstr(buf, "33");
2172 if (!posM || !posD || !posY)
2173 return result;
2175 if (posY < posM && posM < posD)
2176 result = DATEORDER_YMD;
2177 else if (posD < posM)
2178 result = DATEORDER_DMY;
2179 else
2180 result = DATEORDER_MDY;
2182 return result;
2186 * check if given string is a valid locale specifier
2188 * this should match the backend check_locale() function
2190 static bool
2191 check_locale_name(const char *locale)
2193 bool ret;
2194 int category = LC_CTYPE;
2195 char *save;
2197 save = setlocale(category, NULL);
2198 if (!save)
2199 return false; /* should not happen; */
2201 save = xstrdup(save);
2203 ret = (setlocale(category, locale) != NULL);
2205 setlocale(category, save);
2206 free(save);
2208 /* should we exit here? */
2209 if (!ret)
2210 fprintf(stderr, _("%s: invalid locale name \"%s\"\n"), progname, locale);
2212 return ret;
2216 * check if the chosen encoding matches the encoding required by the locale
2218 * this should match the similar check in the backend createdb() function
2220 static bool
2221 check_locale_encoding(const char *locale, int user_enc)
2223 int locale_enc;
2225 locale_enc = pg_get_encoding_from_locale(locale);
2227 /* We allow selection of SQL_ASCII --- see notes in createdb() */
2228 if (!(locale_enc == user_enc ||
2229 locale_enc == PG_SQL_ASCII ||
2230 user_enc == PG_SQL_ASCII
2231 #ifdef WIN32
2234 * On win32, if the encoding chosen is UTF8, all locales are OK
2235 * (assuming the actual locale name passed the checks above). This is
2236 * because UTF8 is a pseudo-codepage, that we convert to UTF16 before
2237 * doing any operations on, and UTF16 supports all locales.
2239 || user_enc == PG_UTF8
2240 #endif
2243 fprintf(stderr, _("%s: encoding mismatch\n"), progname);
2244 fprintf(stderr,
2245 _("The encoding you selected (%s) and the encoding that the\n"
2246 "selected locale uses (%s) do not match. This would lead to\n"
2247 "misbehavior in various character string processing functions.\n"
2248 "Rerun %s and either do not specify an encoding explicitly,\n"
2249 "or choose a matching combination.\n"),
2250 pg_encoding_to_char(user_enc),
2251 pg_encoding_to_char(locale_enc),
2252 progname);
2253 return false;
2255 return true;
2260 * set up the locale variables
2262 * assumes we have called setlocale(LC_ALL,"")
2264 static void
2265 setlocales(void)
2267 /* set empty lc_* values to locale config if set */
2269 if (strlen(locale) > 0)
2271 if (strlen(lc_ctype) == 0)
2272 lc_ctype = locale;
2273 if (strlen(lc_collate) == 0)
2274 lc_collate = locale;
2275 if (strlen(lc_numeric) == 0)
2276 lc_numeric = locale;
2277 if (strlen(lc_time) == 0)
2278 lc_time = locale;
2279 if (strlen(lc_monetary) == 0)
2280 lc_monetary = locale;
2281 if (strlen(lc_messages) == 0)
2282 lc_messages = locale;
2286 * override absent/invalid config settings from initdb's locale settings
2289 if (strlen(lc_ctype) == 0 || !check_locale_name(lc_ctype))
2290 lc_ctype = xstrdup(setlocale(LC_CTYPE, NULL));
2291 if (strlen(lc_collate) == 0 || !check_locale_name(lc_collate))
2292 lc_collate = xstrdup(setlocale(LC_COLLATE, NULL));
2293 if (strlen(lc_numeric) == 0 || !check_locale_name(lc_numeric))
2294 lc_numeric = xstrdup(setlocale(LC_NUMERIC, NULL));
2295 if (strlen(lc_time) == 0 || !check_locale_name(lc_time))
2296 lc_time = xstrdup(setlocale(LC_TIME, NULL));
2297 if (strlen(lc_monetary) == 0 || !check_locale_name(lc_monetary))
2298 lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL));
2299 if (strlen(lc_messages) == 0 || !check_locale_name(lc_messages))
2300 #if defined(LC_MESSAGES) && !defined(WIN32)
2302 /* when available get the current locale setting */
2303 lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL));
2305 #else
2307 /* when not available, get the CTYPE setting */
2308 lc_messages = xstrdup(setlocale(LC_CTYPE, NULL));
2310 #endif
2314 #ifdef WIN32
2315 typedef BOOL(WINAPI * __CreateRestrictedToken) (HANDLE, DWORD, DWORD, PSID_AND_ATTRIBUTES, DWORD, PLUID_AND_ATTRIBUTES, DWORD, PSID_AND_ATTRIBUTES, PHANDLE);
2317 #define DISABLE_MAX_PRIVILEGE 0x1
2320 * Create a restricted token and execute the specified process with it.
2322 * Returns 0 on failure, non-zero on success, same as CreateProcess().
2324 * On NT4, or any other system not containing the required functions, will
2325 * NOT execute anything.
2327 static int
2328 CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION * processInfo)
2330 BOOL b;
2331 STARTUPINFO si;
2332 HANDLE origToken;
2333 HANDLE restrictedToken;
2334 SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
2335 SID_AND_ATTRIBUTES dropSids[2];
2336 __CreateRestrictedToken _CreateRestrictedToken = NULL;
2337 HANDLE Advapi32Handle;
2339 ZeroMemory(&si, sizeof(si));
2340 si.cb = sizeof(si);
2342 Advapi32Handle = LoadLibrary("ADVAPI32.DLL");
2343 if (Advapi32Handle != NULL)
2345 _CreateRestrictedToken = (__CreateRestrictedToken) GetProcAddress(Advapi32Handle, "CreateRestrictedToken");
2348 if (_CreateRestrictedToken == NULL)
2350 fprintf(stderr, "WARNING: cannot create restricted tokens on this platform\n");
2351 if (Advapi32Handle != NULL)
2352 FreeLibrary(Advapi32Handle);
2353 return 0;
2356 /* Open the current token to use as a base for the restricted one */
2357 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken))
2359 fprintf(stderr, "Failed to open process token: %lu\n", GetLastError());
2360 return 0;
2363 /* Allocate list of SIDs to remove */
2364 ZeroMemory(&dropSids, sizeof(dropSids));
2365 if (!AllocateAndInitializeSid(&NtAuthority, 2,
2366 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
2367 0, &dropSids[0].Sid) ||
2368 !AllocateAndInitializeSid(&NtAuthority, 2,
2369 SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
2370 0, &dropSids[1].Sid))
2372 fprintf(stderr, "Failed to allocate SIDs: %lu\n", GetLastError());
2373 return 0;
2376 b = _CreateRestrictedToken(origToken,
2377 DISABLE_MAX_PRIVILEGE,
2378 sizeof(dropSids) / sizeof(dropSids[0]),
2379 dropSids,
2380 0, NULL,
2381 0, NULL,
2382 &restrictedToken);
2384 FreeSid(dropSids[1].Sid);
2385 FreeSid(dropSids[0].Sid);
2386 CloseHandle(origToken);
2387 FreeLibrary(Advapi32Handle);
2389 if (!b)
2391 fprintf(stderr, "Failed to create restricted token: %lu\n", GetLastError());
2392 return 0;
2395 if (!CreateProcessAsUser(restrictedToken,
2396 NULL,
2397 cmd,
2398 NULL,
2399 NULL,
2400 TRUE,
2401 CREATE_SUSPENDED,
2402 NULL,
2403 NULL,
2404 &si,
2405 processInfo))
2408 fprintf(stderr, "CreateProcessAsUser failed: %lu\n", GetLastError());
2409 return 0;
2412 #ifndef __CYGWIN__
2413 AddUserToDacl(processInfo->hProcess);
2414 #endif
2416 return ResumeThread(processInfo->hThread);
2418 #endif
2421 * print help text
2423 static void
2424 usage(const char *progname)
2426 printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2427 printf(_("Usage:\n"));
2428 printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2429 printf(_("\nOptions:\n"));
2430 printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2431 printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2432 printf(_(" --locale=LOCALE set default locale for new databases\n"));
2433 printf(_(" --lc-collate, --lc-ctype, --lc-messages=LOCALE\n"
2434 " --lc-monetary, --lc-numeric, --lc-time=LOCALE\n"
2435 " set default locale in the respective\n"
2436 " category for new databases (default\n"
2437 " taken from environment)\n"));
2438 printf(_(" --no-locale equivalent to --locale=C\n"));
2439 printf(_(" -T, --text-search-config=CFG\n"
2440 " default text search configuration\n"));
2441 printf(_(" -X, --xlogdir=XLOGDIR location for the transaction log directory\n"));
2442 printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2443 printf(_(" -U, --username=NAME database superuser name\n"));
2444 printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2445 printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2446 printf(_(" -?, --help show this help, then exit\n"));
2447 printf(_(" -V, --version output version information, then exit\n"));
2448 printf(_("\nLess commonly used options:\n"));
2449 printf(_(" -d, --debug generate lots of debugging output\n"));
2450 printf(_(" -s, --show show internal settings\n"));
2451 printf(_(" -L DIRECTORY where to find the input files\n"));
2452 printf(_(" -n, --noclean do not clean up after errors\n"));
2453 printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2454 "is used.\n"));
2455 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
2459 main(int argc, char *argv[])
2462 * options with no short version return a low integer, the rest return
2463 * their short version value
2465 static struct option long_options[] = {
2466 {"pgdata", required_argument, NULL, 'D'},
2467 {"encoding", required_argument, NULL, 'E'},
2468 {"locale", required_argument, NULL, 1},
2469 {"lc-collate", required_argument, NULL, 2},
2470 {"lc-ctype", required_argument, NULL, 3},
2471 {"lc-monetary", required_argument, NULL, 4},
2472 {"lc-numeric", required_argument, NULL, 5},
2473 {"lc-time", required_argument, NULL, 6},
2474 {"lc-messages", required_argument, NULL, 7},
2475 {"no-locale", no_argument, NULL, 8},
2476 {"text-search-config", required_argument, NULL, 'T'},
2477 {"auth", required_argument, NULL, 'A'},
2478 {"pwprompt", no_argument, NULL, 'W'},
2479 {"pwfile", required_argument, NULL, 9},
2480 {"username", required_argument, NULL, 'U'},
2481 {"help", no_argument, NULL, '?'},
2482 {"version", no_argument, NULL, 'V'},
2483 {"debug", no_argument, NULL, 'd'},
2484 {"show", no_argument, NULL, 's'},
2485 {"noclean", no_argument, NULL, 'n'},
2486 {"xlogdir", required_argument, NULL, 'X'},
2487 {NULL, 0, NULL, 0}
2490 int c,
2492 ret;
2493 int option_index;
2494 char *short_version;
2495 char *effective_user;
2496 char *pgdenv; /* PGDATA value gotten from and sent to
2497 * environment */
2498 char bin_dir[MAXPGPATH];
2499 char *pg_data_native;
2500 int user_enc;
2502 #ifdef WIN32
2503 char *restrict_env;
2504 #endif
2505 static const char *subdirs[] = {
2506 "global",
2507 "pg_xlog",
2508 "pg_xlog/archive_status",
2509 "pg_clog",
2510 "pg_subtrans",
2511 "pg_twophase",
2512 "pg_multixact/members",
2513 "pg_multixact/offsets",
2514 "base",
2515 "base/1",
2516 "pg_tblspc",
2517 "pg_stat_tmp"
2520 progname = get_progname(argv[0]);
2521 set_pglocale_pgservice(argv[0], "initdb");
2523 if (argc > 1)
2525 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2527 usage(progname);
2528 exit(0);
2530 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2532 puts("initdb (PostgreSQL) " PG_VERSION);
2533 exit(0);
2537 /* process command-line options */
2539 while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:sT:X:", long_options, &option_index)) != -1)
2541 switch (c)
2543 case 'A':
2544 authmethod = xstrdup(optarg);
2545 break;
2546 case 'D':
2547 pg_data = xstrdup(optarg);
2548 break;
2549 case 'E':
2550 encoding = xstrdup(optarg);
2551 break;
2552 case 'W':
2553 pwprompt = true;
2554 break;
2555 case 'U':
2556 username = xstrdup(optarg);
2557 break;
2558 case 'd':
2559 debug = true;
2560 printf(_("Running in debug mode.\n"));
2561 break;
2562 case 'n':
2563 noclean = true;
2564 printf(_("Running in noclean mode. Mistakes will not be cleaned up.\n"));
2565 break;
2566 case 'L':
2567 share_path = xstrdup(optarg);
2568 break;
2569 case 1:
2570 locale = xstrdup(optarg);
2571 break;
2572 case 2:
2573 lc_collate = xstrdup(optarg);
2574 break;
2575 case 3:
2576 lc_ctype = xstrdup(optarg);
2577 break;
2578 case 4:
2579 lc_monetary = xstrdup(optarg);
2580 break;
2581 case 5:
2582 lc_numeric = xstrdup(optarg);
2583 break;
2584 case 6:
2585 lc_time = xstrdup(optarg);
2586 break;
2587 case 7:
2588 lc_messages = xstrdup(optarg);
2589 break;
2590 case 8:
2591 locale = "C";
2592 break;
2593 case 9:
2594 pwfilename = xstrdup(optarg);
2595 break;
2596 case 's':
2597 show_setting = true;
2598 break;
2599 case 'T':
2600 default_text_search_config = xstrdup(optarg);
2601 break;
2602 case 'X':
2603 xlog_dir = xstrdup(optarg);
2604 break;
2605 default:
2606 /* getopt_long already emitted a complaint */
2607 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2608 progname);
2609 exit(1);
2614 /* Non-option argument specifies data directory */
2615 if (optind < argc)
2617 pg_data = xstrdup(argv[optind]);
2618 optind++;
2621 if (optind < argc)
2623 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
2624 progname, argv[optind + 1]);
2625 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2626 progname);
2627 exit(1);
2630 if (pwprompt && pwfilename)
2632 fprintf(stderr, _("%s: password prompt and password file cannot be specified together\n"), progname);
2633 exit(1);
2636 if (authmethod == NULL || !strlen(authmethod))
2638 authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n"
2639 "You can change this by editing pg_hba.conf or using the -A option the\n"
2640 "next time you run initdb.\n");
2641 authmethod = "trust";
2644 if (strcmp(authmethod, "md5") &&
2645 strcmp(authmethod, "ident") &&
2646 strncmp(authmethod, "ident ", 6) && /* ident with space = param */
2647 strcmp(authmethod, "trust") &&
2648 #ifdef USE_PAM
2649 strcmp(authmethod, "pam") &&
2650 strncmp(authmethod, "pam ", 4) && /* pam with space = param */
2651 #endif
2652 strcmp(authmethod, "crypt") &&
2653 strcmp(authmethod, "password")
2657 * Kerberos methods not listed because they are not supported over
2658 * local connections and are rejected in hba.c
2661 fprintf(stderr, _("%s: unrecognized authentication method \"%s\"\n"),
2662 progname, authmethod);
2663 exit(1);
2666 if ((!strcmp(authmethod, "md5") ||
2667 !strcmp(authmethod, "crypt") ||
2668 !strcmp(authmethod, "password")) &&
2669 !(pwprompt || pwfilename))
2671 fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname, authmethod);
2672 exit(1);
2675 if (strlen(pg_data) == 0)
2677 pgdenv = getenv("PGDATA");
2678 if (pgdenv && strlen(pgdenv))
2680 /* PGDATA found */
2681 pg_data = xstrdup(pgdenv);
2683 else
2685 fprintf(stderr,
2686 _("%s: no data directory specified\n"
2687 "You must identify the directory where the data for this database system\n"
2688 "will reside. Do this with either the invocation option -D or the\n"
2689 "environment variable PGDATA.\n"),
2690 progname);
2691 exit(1);
2695 pg_data_native = pg_data;
2696 canonicalize_path(pg_data);
2698 #ifdef WIN32
2701 * Before we execute another program, make sure that we are running with a
2702 * restricted token. If not, re-execute ourselves with one.
2705 if ((restrict_env = getenv("PG_RESTRICT_EXEC")) == NULL
2706 || strcmp(restrict_env, "1") != 0)
2708 PROCESS_INFORMATION pi;
2709 char *cmdline;
2711 ZeroMemory(&pi, sizeof(pi));
2713 cmdline = xstrdup(GetCommandLine());
2715 putenv("PG_RESTRICT_EXEC=1");
2717 if (!CreateRestrictedProcess(cmdline, &pi))
2719 fprintf(stderr, "Failed to re-exec with restricted token: %lu.\n", GetLastError());
2721 else
2724 * Successfully re-execed. Now wait for child process to capture
2725 * exitcode.
2727 DWORD x;
2729 CloseHandle(pi.hThread);
2730 WaitForSingleObject(pi.hProcess, INFINITE);
2732 if (!GetExitCodeProcess(pi.hProcess, &x))
2734 fprintf(stderr, "Failed to get exit code from subprocess: %lu\n", GetLastError());
2735 exit(1);
2737 exit(x);
2740 #endif
2743 * we have to set PGDATA for postgres rather than pass it on the command
2744 * line to avoid dumb quoting problems on Windows, and we would especially
2745 * need quotes otherwise on Windows because paths there are most likely to
2746 * have embedded spaces.
2748 pgdenv = pg_malloc(8 + strlen(pg_data));
2749 sprintf(pgdenv, "PGDATA=%s", pg_data);
2750 putenv(pgdenv);
2752 if ((ret = find_other_exec(argv[0], "postgres", PG_BACKEND_VERSIONSTR,
2753 backend_exec)) < 0)
2755 char full_path[MAXPGPATH];
2757 if (find_my_exec(argv[0], full_path) < 0)
2758 strlcpy(full_path, progname, sizeof(full_path));
2760 if (ret == -1)
2761 fprintf(stderr,
2762 _("The program \"postgres\" is needed by %s "
2763 "but was not found in the\n"
2764 "same directory as \"%s\".\n"
2765 "Check your installation.\n"),
2766 progname, full_path);
2767 else
2768 fprintf(stderr,
2769 _("The program \"postgres\" was found by \"%s\"\n"
2770 "but was not the same version as %s.\n"
2771 "Check your installation.\n"),
2772 full_path, progname);
2773 exit(1);
2776 /* store binary directory */
2777 strcpy(bin_path, backend_exec);
2778 *last_dir_separator(bin_path) = '\0';
2779 canonicalize_path(bin_path);
2781 if (!share_path)
2783 share_path = pg_malloc(MAXPGPATH);
2784 get_share_path(backend_exec, share_path);
2786 else if (!is_absolute_path(share_path))
2788 fprintf(stderr, _("%s: input file location must be an absolute path\n"), progname);
2789 exit(1);
2792 canonicalize_path(share_path);
2794 if ((short_version = get_short_version()) == NULL)
2796 fprintf(stderr, _("%s: could not determine valid short version string\n"), progname);
2797 exit(1);
2800 effective_user = get_id();
2801 if (strlen(username) == 0)
2802 username = effective_user;
2804 set_input(&bki_file, "postgres.bki");
2805 set_input(&desc_file, "postgres.description");
2806 set_input(&shdesc_file, "postgres.shdescription");
2807 set_input(&hba_file, "pg_hba.conf.sample");
2808 set_input(&ident_file, "pg_ident.conf.sample");
2809 set_input(&conf_file, "postgresql.conf.sample");
2810 set_input(&conversion_file, "conversion_create.sql");
2811 set_input(&dictionary_file, "snowball_create.sql");
2812 set_input(&info_schema_file, "information_schema.sql");
2813 set_input(&features_file, "sql_features.txt");
2814 set_input(&system_views_file, "system_views.sql");
2816 set_info_version();
2818 if (show_setting || debug)
2820 fprintf(stderr,
2821 "VERSION=%s\n"
2822 "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2823 "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2824 "POSTGRES_DESCR=%s\nPOSTGRES_SHDESCR=%s\n"
2825 "POSTGRESQL_CONF_SAMPLE=%s\n"
2826 "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2827 PG_VERSION,
2828 pg_data, share_path, bin_path,
2829 username, bki_file,
2830 desc_file, shdesc_file,
2831 conf_file,
2832 hba_file, ident_file);
2833 if (show_setting)
2834 exit(0);
2837 check_input(bki_file);
2838 check_input(desc_file);
2839 check_input(shdesc_file);
2840 check_input(hba_file);
2841 check_input(ident_file);
2842 check_input(conf_file);
2843 check_input(conversion_file);
2844 check_input(dictionary_file);
2845 check_input(info_schema_file);
2846 check_input(features_file);
2847 check_input(system_views_file);
2849 setlocales();
2851 printf(_("The files belonging to this database system will be owned "
2852 "by user \"%s\".\n"
2853 "This user must also own the server process.\n\n"),
2854 effective_user);
2856 if (strcmp(lc_ctype, lc_collate) == 0 &&
2857 strcmp(lc_ctype, lc_time) == 0 &&
2858 strcmp(lc_ctype, lc_numeric) == 0 &&
2859 strcmp(lc_ctype, lc_monetary) == 0 &&
2860 strcmp(lc_ctype, lc_messages) == 0)
2861 printf(_("The database cluster will be initialized with locale %s.\n"), lc_ctype);
2862 else
2864 printf(_("The database cluster will be initialized with locales\n"
2865 " COLLATE: %s\n"
2866 " CTYPE: %s\n"
2867 " MESSAGES: %s\n"
2868 " MONETARY: %s\n"
2869 " NUMERIC: %s\n"
2870 " TIME: %s\n"),
2871 lc_collate,
2872 lc_ctype,
2873 lc_messages,
2874 lc_monetary,
2875 lc_numeric,
2876 lc_time);
2879 if (strlen(encoding) == 0)
2881 int ctype_enc;
2883 ctype_enc = pg_get_encoding_from_locale(lc_ctype);
2885 if (ctype_enc == PG_SQL_ASCII &&
2886 !(pg_strcasecmp(lc_ctype, "C") == 0 ||
2887 pg_strcasecmp(lc_ctype, "POSIX") == 0))
2889 /* Hmm, couldn't recognize the locale's codeset */
2890 fprintf(stderr, _("%s: could not find suitable encoding for locale %s\n"),
2891 progname, lc_ctype);
2892 fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
2893 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2894 progname);
2895 exit(1);
2897 else if (!pg_valid_server_encoding_id(ctype_enc))
2899 /* We recognized it, but it's not a legal server encoding */
2900 fprintf(stderr,
2901 _("%s: locale %s requires unsupported encoding %s\n"),
2902 progname, lc_ctype, pg_encoding_to_char(ctype_enc));
2903 fprintf(stderr,
2904 _("Encoding %s is not allowed as a server-side encoding.\n"
2905 "Rerun %s with a different locale selection.\n"),
2906 pg_encoding_to_char(ctype_enc), progname);
2907 exit(1);
2909 else
2911 encodingid = encodingid_to_string(ctype_enc);
2912 printf(_("The default database encoding has accordingly been set to %s.\n"),
2913 pg_encoding_to_char(ctype_enc));
2916 else
2917 encodingid = get_encoding_id(encoding);
2919 user_enc = atoi(encodingid);
2920 if (!check_locale_encoding(lc_ctype, user_enc) ||
2921 !check_locale_encoding(lc_collate, user_enc))
2922 exit(1); /* check_locale_encoding printed the error */
2924 if (strlen(default_text_search_config) == 0)
2926 default_text_search_config = find_matching_ts_config(lc_ctype);
2927 if (default_text_search_config == NULL)
2929 printf(_("%s: could not find suitable text search configuration for locale %s\n"),
2930 progname, lc_ctype);
2931 default_text_search_config = "simple";
2934 else
2936 const char *checkmatch = find_matching_ts_config(lc_ctype);
2938 if (checkmatch == NULL)
2940 printf(_("%s: warning: suitable text search configuration for locale %s is unknown\n"),
2941 progname, lc_ctype);
2943 else if (strcmp(checkmatch, default_text_search_config) != 0)
2945 printf(_("%s: warning: specified text search configuration \"%s\" might not match locale %s\n"),
2946 progname, default_text_search_config, lc_ctype);
2950 printf(_("The default text search configuration will be set to \"%s\".\n"),
2951 default_text_search_config);
2953 printf("\n");
2955 umask(077);
2958 * now we are starting to do real work, trap signals so we can clean up
2961 /* some of these are not valid on Windows */
2962 #ifdef SIGHUP
2963 pqsignal(SIGHUP, trapsig);
2964 #endif
2965 #ifdef SIGINT
2966 pqsignal(SIGINT, trapsig);
2967 #endif
2968 #ifdef SIGQUIT
2969 pqsignal(SIGQUIT, trapsig);
2970 #endif
2971 #ifdef SIGTERM
2972 pqsignal(SIGTERM, trapsig);
2973 #endif
2975 /* Ignore SIGPIPE when writing to backend, so we can clean up */
2976 #ifdef SIGPIPE
2977 pqsignal(SIGPIPE, SIG_IGN);
2978 #endif
2980 switch (check_data_dir(pg_data))
2982 case 0:
2983 /* PGDATA not there, must create it */
2984 printf(_("creating directory %s ... "),
2985 pg_data);
2986 fflush(stdout);
2988 if (!mkdatadir(NULL))
2989 exit_nicely();
2990 else
2991 check_ok();
2993 made_new_pgdata = true;
2994 break;
2996 case 1:
2997 /* Present but empty, fix permissions and use it */
2998 printf(_("fixing permissions on existing directory %s ... "),
2999 pg_data);
3000 fflush(stdout);
3002 if (chmod(pg_data, 0700) != 0)
3004 fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
3005 progname, pg_data, strerror(errno));
3006 exit_nicely();
3008 else
3009 check_ok();
3011 found_existing_pgdata = true;
3012 break;
3014 case 2:
3015 /* Present and not empty */
3016 fprintf(stderr,
3017 _("%s: directory \"%s\" exists but is not empty\n"),
3018 progname, pg_data);
3019 fprintf(stderr,
3020 _("If you want to create a new database system, either remove or empty\n"
3021 "the directory \"%s\" or run %s\n"
3022 "with an argument other than \"%s\".\n"),
3023 pg_data, progname, pg_data);
3024 exit(1); /* no further message needed */
3026 default:
3027 /* Trouble accessing directory */
3028 fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
3029 progname, pg_data, strerror(errno));
3030 exit_nicely();
3033 /* Create transaction log symlink, if required */
3034 if (strcmp(xlog_dir, "") != 0)
3036 char *linkloc;
3038 /* clean up xlog directory name, check it's absolute */
3039 canonicalize_path(xlog_dir);
3040 if (!is_absolute_path(xlog_dir))
3042 fprintf(stderr, _("%s: xlog directory location must be an absolute path\n"), progname);
3043 exit_nicely();
3046 /* check if the specified xlog directory is empty */
3047 switch (check_data_dir(xlog_dir))
3049 case 0:
3050 /* xlog directory not there, must create it */
3051 printf(_("creating directory %s ... "),
3052 xlog_dir);
3053 fflush(stdout);
3055 if (mkdir_p(xlog_dir, 0700) != 0)
3057 fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
3058 progname, xlog_dir, strerror(errno));
3059 exit_nicely();
3061 else
3062 check_ok();
3064 made_new_xlogdir = true;
3065 break;
3066 case 1:
3067 /* Present but empty, fix permissions and use it */
3068 printf(_("fixing permissions on existing directory %s ... "),
3069 xlog_dir);
3070 fflush(stdout);
3072 if (chmod(xlog_dir, 0700) != 0)
3074 fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
3075 progname, xlog_dir, strerror(errno));
3076 exit_nicely();
3078 else
3079 check_ok();
3081 found_existing_xlogdir = true;
3082 break;
3083 case 2:
3084 /* Present and not empty */
3085 fprintf(stderr,
3086 _("%s: directory \"%s\" exists but is not empty\n"),
3087 progname, xlog_dir);
3088 fprintf(stderr,
3089 _("If you want to store the transaction log there, either\n"
3090 "remove or empty the directory \"%s\".\n"),
3091 xlog_dir);
3092 exit_nicely();
3094 default:
3095 /* Trouble accessing directory */
3096 fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
3097 progname, xlog_dir, strerror(errno));
3098 exit_nicely();
3101 /* form name of the place where the symlink must go */
3102 linkloc = (char *) pg_malloc(strlen(pg_data) + 8 + 1);
3103 sprintf(linkloc, "%s/pg_xlog", pg_data);
3105 #ifdef HAVE_SYMLINK
3106 if (symlink(xlog_dir, linkloc) != 0)
3108 fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
3109 progname, linkloc, strerror(errno));
3110 exit_nicely();
3112 #else
3113 fprintf(stderr, _("%s: symlinks are not supported on this platform"));
3114 exit_nicely();
3115 #endif
3118 /* Create required subdirectories */
3119 printf(_("creating subdirectories ... "));
3120 fflush(stdout);
3122 for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++)
3124 if (!mkdatadir(subdirs[i]))
3125 exit_nicely();
3128 check_ok();
3130 /* Top level PG_VERSION is checked by bootstrapper, so make it first */
3131 set_short_version(short_version, NULL);
3133 /* Select suitable configuration settings */
3134 set_null_conf();
3135 test_config_settings();
3137 /* Now create all the text config files */
3138 setup_config();
3140 /* Bootstrap template1 */
3141 bootstrap_template1(short_version);
3144 * Make the per-database PG_VERSION for template1 only after init'ing it
3146 set_short_version(short_version, "base/1");
3148 /* Create the stuff we don't need to use bootstrap mode for */
3150 setup_auth();
3151 if (pwprompt || pwfilename)
3152 get_set_pwd();
3154 setup_depend();
3156 setup_sysviews();
3158 setup_description();
3160 setup_conversion();
3162 setup_dictionary();
3164 setup_privileges();
3166 setup_schema();
3168 vacuum_db();
3170 make_template0();
3172 make_postgres();
3174 if (authwarning != NULL)
3175 fprintf(stderr, "%s", authwarning);
3177 /* Get directory specification used to start this executable */
3178 strcpy(bin_dir, argv[0]);
3179 get_parent_directory(bin_dir);
3181 printf(_("\nSuccess. You can now start the database server using:\n\n"
3182 " %s%s%spostgres%s -D %s%s%s\n"
3183 "or\n"
3184 " %s%s%spg_ctl%s -D %s%s%s -l logfile start\n\n"),
3185 QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
3186 QUOTE_PATH, pg_data_native, QUOTE_PATH,
3187 QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH,
3188 QUOTE_PATH, pg_data_native, QUOTE_PATH);
3190 return 0;