PostgreSQL Source Code  git master
initdb.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * initdb --- initialize a PostgreSQL installation
4  *
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.
8  *
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".
14  *
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.
19  *
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.
23  *
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.
30  *
31  *
32  * Note:
33  * The program has some memory leakage - it isn't worth cleaning it up.
34  *
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
38  *
39  * This code is released under the terms of the PostgreSQL License.
40  *
41  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
42  * Portions Copyright (c) 1994, Regents of the University of California
43  *
44  * src/bin/initdb/initdb.c
45  *
46  *-------------------------------------------------------------------------
47  */
48 
49 #include "postgres_fe.h"
50 
51 #include <dirent.h>
52 #include <fcntl.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <signal.h>
56 #include <time.h>
57 
58 #ifdef HAVE_SHM_OPEN
59 #include "sys/mman.h"
60 #endif
61 
62 #include "access/xlog_internal.h"
63 #include "catalog/pg_authid_d.h"
64 #include "catalog/pg_class_d.h" /* pgrminclude ignore */
65 #include "catalog/pg_collation_d.h"
66 #include "catalog/pg_database_d.h" /* pgrminclude ignore */
67 #include "common/file_perm.h"
68 #include "common/file_utils.h"
69 #include "common/logging.h"
70 #include "common/pg_prng.h"
72 #include "common/string.h"
73 #include "common/username.h"
74 #include "fe_utils/string_utils.h"
75 #include "getaddrinfo.h"
76 #include "getopt_long.h"
77 #include "mb/pg_wchar.h"
78 #include "miscadmin.h"
79 
80 
81 /* Ideally this would be in a .h file, but it hardly seems worth the trouble */
82 extern const char *select_default_timezone(const char *share_path);
83 
84 static const char *const auth_methods_host[] = {
85  "trust", "reject", "scram-sha-256", "md5", "password", "ident", "radius",
86 #ifdef ENABLE_GSS
87  "gss",
88 #endif
89 #ifdef ENABLE_SSPI
90  "sspi",
91 #endif
92 #ifdef USE_PAM
93  "pam", "pam ",
94 #endif
95 #ifdef USE_BSD_AUTH
96  "bsd",
97 #endif
98 #ifdef USE_LDAP
99  "ldap",
100 #endif
101 #ifdef USE_SSL
102  "cert",
103 #endif
104  NULL
105 };
106 static const char *const auth_methods_local[] = {
107  "trust", "reject", "scram-sha-256", "md5", "password", "peer", "radius",
108 #ifdef USE_PAM
109  "pam", "pam ",
110 #endif
111 #ifdef USE_BSD_AUTH
112  "bsd",
113 #endif
114 #ifdef USE_LDAP
115  "ldap",
116 #endif
117  NULL
118 };
119 
120 /*
121  * these values are passed in by makefile defines
122  */
123 static char *share_path = NULL;
124 
125 /* values to be obtained from arguments */
126 static char *pg_data = NULL;
127 static char *encoding = NULL;
128 static char *locale = NULL;
129 static char *lc_collate = NULL;
130 static char *lc_ctype = NULL;
131 static char *lc_monetary = NULL;
132 static char *lc_numeric = NULL;
133 static char *lc_time = NULL;
134 static char *lc_messages = NULL;
135 static char locale_provider = COLLPROVIDER_LIBC;
136 static char *icu_locale = NULL;
137 static const char *default_text_search_config = NULL;
138 static char *username = NULL;
139 static bool pwprompt = false;
140 static char *pwfilename = NULL;
141 static char *superuser_password = NULL;
142 static const char *authmethodhost = NULL;
143 static const char *authmethodlocal = NULL;
144 static bool debug = false;
145 static bool noclean = false;
146 static bool noinstructions = false;
147 static bool do_sync = true;
148 static bool sync_only = false;
149 static bool show_setting = false;
150 static bool data_checksums = false;
151 static char *xlog_dir = NULL;
152 static char *str_wal_segment_size_mb = NULL;
154 
155 
156 /* internal vars */
157 static const char *progname;
158 static int encodingid;
159 static char *bki_file;
160 static char *hba_file;
161 static char *ident_file;
162 static char *conf_file;
163 static char *dictionary_file;
164 static char *info_schema_file;
165 static char *features_file;
168 static char *system_views_file;
169 static bool success = false;
170 static bool made_new_pgdata = false;
171 static bool found_existing_pgdata = false;
172 static bool made_new_xlogdir = false;
173 static bool found_existing_xlogdir = false;
174 static char infoversion[100];
175 static bool caught_signal = false;
176 static bool output_failed = false;
177 static int output_errno = 0;
178 static char *pgdata_native;
179 
180 /* defaults */
181 static int n_connections = 10;
182 static int n_buffers = 50;
183 static const char *dynamic_shared_memory_type = NULL;
184 static const char *default_timezone = NULL;
185 
186 /*
187  * Warning messages for authentication methods
188  */
189 #define AUTHTRUST_WARNING \
190 "# CAUTION: Configuring the system for local \"trust\" authentication\n" \
191 "# allows any local user to connect as any PostgreSQL user, including\n" \
192 "# the database superuser. If you do not trust all your local users,\n" \
193 "# use another authentication method.\n"
194 static bool authwarning = false;
195 
196 /*
197  * Centralized knowledge of switches to pass to backend
198  *
199  * Note: we run the backend with -F (fsync disabled) and then do a single
200  * pass of fsync'ing at the end. This is faster than fsync'ing each step.
201  *
202  * Note: in the shell-script version, we also passed PGDATA as a -D switch,
203  * but here it is more convenient to pass it as an environment variable
204  * (no quoting to worry about).
205  */
206 static const char *boot_options = "-F -c log_checkpoints=false";
207 static const char *backend_options = "--single -F -O -j -c search_path=pg_catalog -c exit_on_error=true -c log_checkpoints=false";
208 
209 /* Additional switches to pass to backend (either boot or standalone) */
210 static char *extra_options = "";
211 
212 static const char *const subdirs[] = {
213  "global",
214  "pg_wal/archive_status",
215  "pg_commit_ts",
216  "pg_dynshmem",
217  "pg_notify",
218  "pg_serial",
219  "pg_snapshots",
220  "pg_subtrans",
221  "pg_twophase",
222  "pg_multixact",
223  "pg_multixact/members",
224  "pg_multixact/offsets",
225  "base",
226  "base/1",
227  "pg_replslot",
228  "pg_tblspc",
229  "pg_stat",
230  "pg_stat_tmp",
231  "pg_xact",
232  "pg_logical",
233  "pg_logical/snapshots",
234  "pg_logical/mappings"
235 };
236 
237 
238 /* path to 'initdb' binary directory */
239 static char bin_path[MAXPGPATH];
240 static char backend_exec[MAXPGPATH];
241 
242 static char **replace_token(char **lines,
243  const char *token, const char *replacement);
244 
245 #ifndef HAVE_UNIX_SOCKETS
246 static char **filter_lines_with_token(char **lines, const char *token);
247 #endif
248 static char **readfile(const char *path);
249 static void writefile(char *path, char **lines);
250 static FILE *popen_check(const char *command, const char *mode);
251 static char *get_id(void);
252 static int get_encoding_id(const char *encoding_name);
253 static void set_input(char **dest, const char *filename);
254 static void check_input(char *path);
255 static void write_version_file(const char *extrapath);
256 static void set_null_conf(void);
257 static void test_config_settings(void);
258 static void setup_config(void);
259 static void bootstrap_template1(void);
260 static void setup_auth(FILE *cmdfd);
261 static void get_su_pwd(void);
262 static void setup_depend(FILE *cmdfd);
263 static void setup_run_file(FILE *cmdfd, const char *filename);
264 static void setup_description(FILE *cmdfd);
265 static void setup_collation(FILE *cmdfd);
266 static void setup_privileges(FILE *cmdfd);
267 static void set_info_version(void);
268 static void setup_schema(FILE *cmdfd);
269 static void load_plpgsql(FILE *cmdfd);
270 static void vacuum_db(FILE *cmdfd);
271 static void make_template0(FILE *cmdfd);
272 static void make_postgres(FILE *cmdfd);
273 static void trapsig(int signum);
274 static void check_ok(void);
275 static char *escape_quotes(const char *src);
276 static char *escape_quotes_bki(const char *src);
277 static int locale_date_order(const char *locale);
278 static void check_locale_name(int category, const char *locale,
279  char **canonname);
280 static bool check_locale_encoding(const char *locale, int encoding);
281 static void setlocales(void);
282 static void usage(const char *progname);
283 void setup_pgdata(void);
284 void setup_bin_paths(const char *argv0);
285 void setup_data_file_paths(void);
286 void setup_locale_encoding(void);
287 void setup_signals(void);
288 void setup_text_search(void);
289 void create_data_directory(void);
290 void create_xlog_or_symlink(void);
291 void warn_on_mount_point(int error);
292 void initialize_data_directory(void);
293 
294 /*
295  * macros for running pipes to postgres
296  */
297 #define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd
298 
299 #define PG_CMD_OPEN \
300 do { \
301  cmdfd = popen_check(cmd, "w"); \
302  if (cmdfd == NULL) \
303  exit(1); /* message already printed by popen_check */ \
304 } while (0)
305 
306 #define PG_CMD_CLOSE \
307 do { \
308  if (pclose_check(cmdfd)) \
309  exit(1); /* message already printed by pclose_check */ \
310 } while (0)
311 
312 #define PG_CMD_PUTS(line) \
313 do { \
314  if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
315  output_failed = true, output_errno = errno; \
316 } while (0)
317 
318 #define PG_CMD_PRINTF(fmt, ...) \
319 do { \
320  if (fprintf(cmdfd, fmt, __VA_ARGS__) < 0 || fflush(cmdfd) < 0) \
321  output_failed = true, output_errno = errno; \
322 } while (0)
323 
324 /*
325  * Escape single quotes and backslashes, suitably for insertions into
326  * configuration files or SQL E'' strings.
327  */
328 static char *
329 escape_quotes(const char *src)
330 {
331  char *result = escape_single_quotes_ascii(src);
332 
333  if (!result)
334  pg_fatal("out of memory");
335  return result;
336 }
337 
338 /*
339  * Escape a field value to be inserted into the BKI data.
340  * Run the value through escape_quotes (which will be inverted
341  * by the backend's DeescapeQuotedString() function), then wrap
342  * the value in single quotes, even if that isn't strictly necessary.
343  */
344 static char *
345 escape_quotes_bki(const char *src)
346 {
347  char *result;
348  char *data = escape_quotes(src);
349  char *resultp;
350  char *datap;
351 
352  result = (char *) pg_malloc(strlen(data) + 3);
353  resultp = result;
354  *resultp++ = '\'';
355  for (datap = data; *datap; datap++)
356  *resultp++ = *datap;
357  *resultp++ = '\'';
358  *resultp = '\0';
359 
360  free(data);
361  return result;
362 }
363 
364 /*
365  * make a copy of the array of lines, with token replaced by replacement
366  * the first time it occurs on each line.
367  *
368  * This does most of what sed was used for in the shell script, but
369  * doesn't need any regexp stuff.
370  */
371 static char **
372 replace_token(char **lines, const char *token, const char *replacement)
373 {
374  int numlines = 1;
375  int i;
376  char **result;
377  int toklen,
378  replen,
379  diff;
380 
381  for (i = 0; lines[i]; i++)
382  numlines++;
383 
384  result = (char **) pg_malloc(numlines * sizeof(char *));
385 
386  toklen = strlen(token);
387  replen = strlen(replacement);
388  diff = replen - toklen;
389 
390  for (i = 0; i < numlines; i++)
391  {
392  char *where;
393  char *newline;
394  int pre;
395 
396  /* just copy pointer if NULL or no change needed */
397  if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
398  {
399  result[i] = lines[i];
400  continue;
401  }
402 
403  /* if we get here a change is needed - set up new line */
404 
405  newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
406 
407  pre = where - lines[i];
408 
409  memcpy(newline, lines[i], pre);
410 
411  memcpy(newline + pre, replacement, replen);
412 
413  strcpy(newline + pre + replen, lines[i] + pre + toklen);
414 
415  result[i] = newline;
416  }
417 
418  return result;
419 }
420 
421 /*
422  * make a copy of lines without any that contain the token
423  *
424  * a sort of poor man's grep -v
425  */
426 #ifndef HAVE_UNIX_SOCKETS
427 static char **
428 filter_lines_with_token(char **lines, const char *token)
429 {
430  int numlines = 1;
431  int i,
432  src,
433  dst;
434  char **result;
435 
436  for (i = 0; lines[i]; i++)
437  numlines++;
438 
439  result = (char **) pg_malloc(numlines * sizeof(char *));
440 
441  for (src = 0, dst = 0; src < numlines; src++)
442  {
443  if (lines[src] == NULL || strstr(lines[src], token) == NULL)
444  result[dst++] = lines[src];
445  }
446 
447  return result;
448 }
449 #endif
450 
451 /*
452  * get the lines from a text file
453  */
454 static char **
455 readfile(const char *path)
456 {
457  char **result;
458  FILE *infile;
459  StringInfoData line;
460  int maxlines;
461  int n;
462 
463  if ((infile = fopen(path, "r")) == NULL)
464  pg_fatal("could not open file \"%s\" for reading: %m", path);
465 
466  initStringInfo(&line);
467 
468  maxlines = 1024;
469  result = (char **) pg_malloc(maxlines * sizeof(char *));
470 
471  n = 0;
472  while (pg_get_line_buf(infile, &line))
473  {
474  /* make sure there will be room for a trailing NULL pointer */
475  if (n >= maxlines - 1)
476  {
477  maxlines *= 2;
478  result = (char **) pg_realloc(result, maxlines * sizeof(char *));
479  }
480 
481  result[n++] = pg_strdup(line.data);
482  }
483  result[n] = NULL;
484 
485  pfree(line.data);
486 
487  fclose(infile);
488 
489  return result;
490 }
491 
492 /*
493  * write an array of lines to a file
494  *
495  * This is only used to write text files. Use fopen "w" not PG_BINARY_W
496  * so that the resulting configuration files are nicely editable on Windows.
497  */
498 static void
499 writefile(char *path, char **lines)
500 {
501  FILE *out_file;
502  char **line;
503 
504  if ((out_file = fopen(path, "w")) == NULL)
505  pg_fatal("could not open file \"%s\" for writing: %m", path);
506  for (line = lines; *line != NULL; line++)
507  {
508  if (fputs(*line, out_file) < 0)
509  pg_fatal("could not write file \"%s\": %m", path);
510  free(*line);
511  }
512  if (fclose(out_file))
513  pg_fatal("could not close file \"%s\": %m", path);
514 }
515 
516 /*
517  * Open a subcommand with suitable error messaging
518  */
519 static FILE *
520 popen_check(const char *command, const char *mode)
521 {
522  FILE *cmdfd;
523 
524  fflush(stdout);
525  fflush(stderr);
526  errno = 0;
527  cmdfd = popen(command, mode);
528  if (cmdfd == NULL)
529  pg_log_error("could not execute command \"%s\": %m", command);
530  return cmdfd;
531 }
532 
533 /*
534  * clean up any files we created on failure
535  * if we created the data directory remove it too
536  */
537 static void
539 {
540  if (success)
541  return;
542 
543  if (!noclean)
544  {
545  if (made_new_pgdata)
546  {
547  pg_log_info("removing data directory \"%s\"", pg_data);
548  if (!rmtree(pg_data, true))
549  pg_log_error("failed to remove data directory");
550  }
551  else if (found_existing_pgdata)
552  {
553  pg_log_info("removing contents of data directory \"%s\"",
554  pg_data);
555  if (!rmtree(pg_data, false))
556  pg_log_error("failed to remove contents of data directory");
557  }
558 
559  if (made_new_xlogdir)
560  {
561  pg_log_info("removing WAL directory \"%s\"", xlog_dir);
562  if (!rmtree(xlog_dir, true))
563  pg_log_error("failed to remove WAL directory");
564  }
565  else if (found_existing_xlogdir)
566  {
567  pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
568  if (!rmtree(xlog_dir, false))
569  pg_log_error("failed to remove contents of WAL directory");
570  }
571  /* otherwise died during startup, do nothing! */
572  }
573  else
574  {
576  pg_log_info("data directory \"%s\" not removed at user's request",
577  pg_data);
578 
580  pg_log_info("WAL directory \"%s\" not removed at user's request",
581  xlog_dir);
582  }
583 }
584 
585 /*
586  * find the current user
587  *
588  * on unix make sure it isn't root
589  */
590 static char *
591 get_id(void)
592 {
593  const char *username;
594 
595 #ifndef WIN32
596  if (geteuid() == 0) /* 0 is root's uid */
597  {
598  pg_log_error("cannot be run as root");
599  pg_log_error_hint("Please log in (using, e.g., \"su\") as the (unprivileged) user that will own the server process.");
600  exit(1);
601  }
602 #endif
603 
605 
606  return pg_strdup(username);
607 }
608 
609 static char *
611 {
612  char result[20];
613 
614  sprintf(result, "%d", enc);
615  return pg_strdup(result);
616 }
617 
618 /*
619  * get the encoding id for a given encoding name
620  */
621 static int
622 get_encoding_id(const char *encoding_name)
623 {
624  int enc;
625 
626  if (encoding_name && *encoding_name)
627  {
628  if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
629  return enc;
630  }
631  pg_fatal("\"%s\" is not a valid server encoding name",
632  encoding_name ? encoding_name : "(null)");
633 }
634 
635 /*
636  * Support for determining the best default text search configuration.
637  * We key this off the first part of LC_CTYPE (ie, the language name).
638  */
640 {
641  const char *tsconfname;
642  const char *langname;
643 };
644 
645 static const struct tsearch_config_match tsearch_config_languages[] =
646 {
647  {"arabic", "ar"},
648  {"arabic", "Arabic"},
649  {"armenian", "hy"},
650  {"armenian", "Armenian"},
651  {"basque", "eu"},
652  {"basque", "Basque"},
653  {"catalan", "ca"},
654  {"catalan", "Catalan"},
655  {"danish", "da"},
656  {"danish", "Danish"},
657  {"dutch", "nl"},
658  {"dutch", "Dutch"},
659  {"english", "C"},
660  {"english", "POSIX"},
661  {"english", "en"},
662  {"english", "English"},
663  {"finnish", "fi"},
664  {"finnish", "Finnish"},
665  {"french", "fr"},
666  {"french", "French"},
667  {"german", "de"},
668  {"german", "German"},
669  {"greek", "el"},
670  {"greek", "Greek"},
671  {"hindi", "hi"},
672  {"hindi", "Hindi"},
673  {"hungarian", "hu"},
674  {"hungarian", "Hungarian"},
675  {"indonesian", "id"},
676  {"indonesian", "Indonesian"},
677  {"irish", "ga"},
678  {"irish", "Irish"},
679  {"italian", "it"},
680  {"italian", "Italian"},
681  {"lithuanian", "lt"},
682  {"lithuanian", "Lithuanian"},
683  {"nepali", "ne"},
684  {"nepali", "Nepali"},
685  {"norwegian", "no"},
686  {"norwegian", "Norwegian"},
687  {"portuguese", "pt"},
688  {"portuguese", "Portuguese"},
689  {"romanian", "ro"},
690  {"russian", "ru"},
691  {"russian", "Russian"},
692  {"serbian", "sr"},
693  {"serbian", "Serbian"},
694  {"spanish", "es"},
695  {"spanish", "Spanish"},
696  {"swedish", "sv"},
697  {"swedish", "Swedish"},
698  {"tamil", "ta"},
699  {"tamil", "Tamil"},
700  {"turkish", "tr"},
701  {"turkish", "Turkish"},
702  {"yiddish", "yi"},
703  {"yiddish", "Yiddish"},
704  {NULL, NULL} /* end marker */
705 };
706 
707 /*
708  * Look for a text search configuration matching lc_ctype, and return its
709  * name; return NULL if no match.
710  */
711 static const char *
712 find_matching_ts_config(const char *lc_type)
713 {
714  int i;
715  char *langname,
716  *ptr;
717 
718  /*
719  * Convert lc_ctype to a language name by stripping everything after an
720  * underscore (usual case) or a hyphen (Windows "locale name"; see
721  * comments at IsoLocaleName()).
722  *
723  * XXX Should ' ' be a stop character? This would select "norwegian" for
724  * the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
725  * should also accept the "nn" and "nb" Unix locales.
726  *
727  * Just for paranoia, we also stop at '.' or '@'.
728  */
729  if (lc_type == NULL)
730  langname = pg_strdup("");
731  else
732  {
733  ptr = langname = pg_strdup(lc_type);
734  while (*ptr &&
735  *ptr != '_' && *ptr != '-' && *ptr != '.' && *ptr != '@')
736  ptr++;
737  *ptr = '\0';
738  }
739 
740  for (i = 0; tsearch_config_languages[i].tsconfname; i++)
741  {
743  {
744  free(langname);
746  }
747  }
748 
749  free(langname);
750  return NULL;
751 }
752 
753 
754 /*
755  * set name of given input file variable under data directory
756  */
757 static void
758 set_input(char **dest, const char *filename)
759 {
760  *dest = psprintf("%s/%s", share_path, filename);
761 }
762 
763 /*
764  * check that given input file exists
765  */
766 static void
767 check_input(char *path)
768 {
769  struct stat statbuf;
770 
771  if (stat(path, &statbuf) != 0)
772  {
773  if (errno == ENOENT)
774  {
775  pg_log_error("file \"%s\" does not exist", path);
776  pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
777  }
778  else
779  {
780  pg_log_error("could not access file \"%s\": %m", path);
781  pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
782  }
783  exit(1);
784  }
785  if (!S_ISREG(statbuf.st_mode))
786  {
787  pg_log_error("file \"%s\" is not a regular file", path);
788  pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
789  exit(1);
790  }
791 }
792 
793 /*
794  * write out the PG_VERSION file in the data dir, or its subdirectory
795  * if extrapath is not NULL
796  */
797 static void
798 write_version_file(const char *extrapath)
799 {
800  FILE *version_file;
801  char *path;
802 
803  if (extrapath == NULL)
804  path = psprintf("%s/PG_VERSION", pg_data);
805  else
806  path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
807 
808  if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
809  pg_fatal("could not open file \"%s\" for writing: %m", path);
810  if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
811  fclose(version_file))
812  pg_fatal("could not write file \"%s\": %m", path);
813  free(path);
814 }
815 
816 /*
817  * set up an empty config file so we can check config settings by launching
818  * a test backend
819  */
820 static void
822 {
823  FILE *conf_file;
824  char *path;
825 
826  path = psprintf("%s/postgresql.conf", pg_data);
827  conf_file = fopen(path, PG_BINARY_W);
828  if (conf_file == NULL)
829  pg_fatal("could not open file \"%s\" for writing: %m", path);
830  if (fclose(conf_file))
831  pg_fatal("could not write file \"%s\": %m", path);
832  free(path);
833 }
834 
835 /*
836  * Determine which dynamic shared memory implementation should be used on
837  * this platform. POSIX shared memory is preferable because the default
838  * allocation limits are much higher than the limits for System V on most
839  * systems that support both, but the fact that a platform has shm_open
840  * doesn't guarantee that that call will succeed when attempted. So, we
841  * attempt to reproduce what the postmaster will do when allocating a POSIX
842  * segment in dsm_impl.c; if it doesn't work, we assume it won't work for
843  * the postmaster either, and configure the cluster for System V shared
844  * memory instead.
845  *
846  * We avoid choosing Solaris's implementation of shm_open() by default. It
847  * can sleep and fail spuriously under contention.
848  */
849 static const char *
851 {
852 #if defined(HAVE_SHM_OPEN) && !defined(__sun__)
853  int ntries = 10;
854  pg_prng_state prng_state;
855 
856  /* Initialize prng; this function is its only user in this program. */
857  pg_prng_seed(&prng_state, (uint64) (getpid() ^ time(NULL)));
858 
859  while (ntries > 0)
860  {
861  uint32 handle;
862  char name[64];
863  int fd;
864 
865  handle = pg_prng_uint32(&prng_state);
866  snprintf(name, 64, "/PostgreSQL.%u", handle);
867  if ((fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0600)) != -1)
868  {
869  close(fd);
870  shm_unlink(name);
871  return "posix";
872  }
873  if (errno != EEXIST)
874  break;
875  --ntries;
876  }
877 #endif
878 
879 #ifdef WIN32
880  return "windows";
881 #else
882  return "sysv";
883 #endif
884 }
885 
886 /*
887  * Determine platform-specific config settings
888  *
889  * Use reasonable values if kernel will let us, else scale back.
890  */
891 static void
893 {
894  /*
895  * This macro defines the minimum shared_buffers we want for a given
896  * max_connections value. The arrays show the settings to try.
897  */
898 #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
899 
900  static const int trial_conns[] = {
901  100, 50, 40, 30, 20
902  };
903  static const int trial_bufs[] = {
904  16384, 8192, 4096, 3584, 3072, 2560, 2048, 1536,
905  1000, 900, 800, 700, 600, 500,
906  400, 300, 200, 100, 50
907  };
908 
909  char cmd[MAXPGPATH];
910  const int connslen = sizeof(trial_conns) / sizeof(int);
911  const int bufslen = sizeof(trial_bufs) / sizeof(int);
912  int i,
913  status,
914  test_conns,
915  test_buffs,
916  ok_buffers = 0;
917 
918  /*
919  * Need to determine working DSM implementation first so that subsequent
920  * tests don't fail because DSM setting doesn't work.
921  */
922  printf(_("selecting dynamic shared memory implementation ... "));
923  fflush(stdout);
926 
927  /*
928  * Probe for max_connections before shared_buffers, since it is subject to
929  * more constraints than shared_buffers.
930  */
931  printf(_("selecting default max_connections ... "));
932  fflush(stdout);
933 
934  for (i = 0; i < connslen; i++)
935  {
936  test_conns = trial_conns[i];
937  test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
938 
939  snprintf(cmd, sizeof(cmd),
940  "\"%s\" --check %s %s "
941  "-c max_connections=%d "
942  "-c shared_buffers=%d "
943  "-c dynamic_shared_memory_type=%s "
944  "< \"%s\" > \"%s\" 2>&1",
946  test_conns, test_buffs,
948  DEVNULL, DEVNULL);
949  status = system(cmd);
950  if (status == 0)
951  {
952  ok_buffers = test_buffs;
953  break;
954  }
955  }
956  if (i >= connslen)
957  i = connslen - 1;
958  n_connections = trial_conns[i];
959 
960  printf("%d\n", n_connections);
961 
962  printf(_("selecting default shared_buffers ... "));
963  fflush(stdout);
964 
965  for (i = 0; i < bufslen; i++)
966  {
967  /* Use same amount of memory, independent of BLCKSZ */
968  test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
969  if (test_buffs <= ok_buffers)
970  {
971  test_buffs = ok_buffers;
972  break;
973  }
974 
975  snprintf(cmd, sizeof(cmd),
976  "\"%s\" --check %s %s "
977  "-c max_connections=%d "
978  "-c shared_buffers=%d "
979  "-c dynamic_shared_memory_type=%s "
980  "< \"%s\" > \"%s\" 2>&1",
982  n_connections, test_buffs,
984  DEVNULL, DEVNULL);
985  status = system(cmd);
986  if (status == 0)
987  break;
988  }
989  n_buffers = test_buffs;
990 
991  if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
992  printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
993  else
994  printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
995 
996  printf(_("selecting default time zone ... "));
997  fflush(stdout);
999  printf("%s\n", default_timezone ? default_timezone : "GMT");
1000 }
1001 
1002 /*
1003  * Calculate the default wal_size with a "pretty" unit.
1004  */
1005 static char *
1006 pretty_wal_size(int segment_count)
1007 {
1008  int sz = wal_segment_size_mb * segment_count;
1009  char *result = pg_malloc(14);
1010 
1011  if ((sz % 1024) == 0)
1012  snprintf(result, 14, "%dGB", sz / 1024);
1013  else
1014  snprintf(result, 14, "%dMB", sz);
1015 
1016  return result;
1017 }
1018 
1019 /*
1020  * set up all the config files
1021  */
1022 static void
1024 {
1025  char **conflines;
1026  char repltok[MAXPGPATH];
1027  char path[MAXPGPATH];
1028  char *autoconflines[3];
1029 
1030  fputs(_("creating configuration files ... "), stdout);
1031  fflush(stdout);
1032 
1033  /* postgresql.conf */
1034 
1035  conflines = readfile(conf_file);
1036 
1037  snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
1038  conflines = replace_token(conflines, "#max_connections = 100", repltok);
1039 
1040  if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1041  snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
1042  (n_buffers * (BLCKSZ / 1024)) / 1024);
1043  else
1044  snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
1045  n_buffers * (BLCKSZ / 1024));
1046  conflines = replace_token(conflines, "#shared_buffers = 128MB", repltok);
1047 
1048 #ifdef HAVE_UNIX_SOCKETS
1049  snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'",
1051 #else
1052  snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''");
1053 #endif
1054  conflines = replace_token(conflines, "#unix_socket_directories = '/tmp'",
1055  repltok);
1056 
1057 #if DEF_PGPORT != 5432
1058  snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
1059  conflines = replace_token(conflines, "#port = 5432", repltok);
1060 #endif
1061 
1062  /* set default max_wal_size and min_wal_size */
1063  snprintf(repltok, sizeof(repltok), "min_wal_size = %s",
1065  conflines = replace_token(conflines, "#min_wal_size = 80MB", repltok);
1066 
1067  snprintf(repltok, sizeof(repltok), "max_wal_size = %s",
1069  conflines = replace_token(conflines, "#max_wal_size = 1GB", repltok);
1070 
1071  snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
1073  conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
1074 
1075  snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
1077  conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
1078 
1079  snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
1081  conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
1082 
1083  snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
1085  conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
1086 
1087  switch (locale_date_order(lc_time))
1088  {
1089  case DATEORDER_YMD:
1090  strcpy(repltok, "datestyle = 'iso, ymd'");
1091  break;
1092  case DATEORDER_DMY:
1093  strcpy(repltok, "datestyle = 'iso, dmy'");
1094  break;
1095  case DATEORDER_MDY:
1096  default:
1097  strcpy(repltok, "datestyle = 'iso, mdy'");
1098  break;
1099  }
1100  conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
1101 
1102  snprintf(repltok, sizeof(repltok),
1103  "default_text_search_config = 'pg_catalog.%s'",
1105  conflines = replace_token(conflines,
1106  "#default_text_search_config = 'pg_catalog.simple'",
1107  repltok);
1108 
1109  if (default_timezone)
1110  {
1111  snprintf(repltok, sizeof(repltok), "timezone = '%s'",
1113  conflines = replace_token(conflines, "#timezone = 'GMT'", repltok);
1114  snprintf(repltok, sizeof(repltok), "log_timezone = '%s'",
1116  conflines = replace_token(conflines, "#log_timezone = 'GMT'", repltok);
1117  }
1118 
1119  snprintf(repltok, sizeof(repltok), "dynamic_shared_memory_type = %s",
1121  conflines = replace_token(conflines, "#dynamic_shared_memory_type = posix",
1122  repltok);
1123 
1124 #if DEFAULT_BACKEND_FLUSH_AFTER > 0
1125  snprintf(repltok, sizeof(repltok), "#backend_flush_after = %dkB",
1126  DEFAULT_BACKEND_FLUSH_AFTER * (BLCKSZ / 1024));
1127  conflines = replace_token(conflines, "#backend_flush_after = 0",
1128  repltok);
1129 #endif
1130 
1131 #if DEFAULT_BGWRITER_FLUSH_AFTER > 0
1132  snprintf(repltok, sizeof(repltok), "#bgwriter_flush_after = %dkB",
1133  DEFAULT_BGWRITER_FLUSH_AFTER * (BLCKSZ / 1024));
1134  conflines = replace_token(conflines, "#bgwriter_flush_after = 0",
1135  repltok);
1136 #endif
1137 
1138 #if DEFAULT_CHECKPOINT_FLUSH_AFTER > 0
1139  snprintf(repltok, sizeof(repltok), "#checkpoint_flush_after = %dkB",
1140  DEFAULT_CHECKPOINT_FLUSH_AFTER * (BLCKSZ / 1024));
1141  conflines = replace_token(conflines, "#checkpoint_flush_after = 0",
1142  repltok);
1143 #endif
1144 
1145 #ifndef USE_PREFETCH
1146  conflines = replace_token(conflines,
1147  "#effective_io_concurrency = 1",
1148  "#effective_io_concurrency = 0");
1149 #endif
1150 
1151 #ifdef WIN32
1152  conflines = replace_token(conflines,
1153  "#update_process_title = on",
1154  "#update_process_title = off");
1155 #endif
1156 
1157  /*
1158  * Change password_encryption setting to md5 if md5 was chosen as an
1159  * authentication method, unless scram-sha-256 was also chosen.
1160  */
1161  if ((strcmp(authmethodlocal, "md5") == 0 &&
1162  strcmp(authmethodhost, "scram-sha-256") != 0) ||
1163  (strcmp(authmethodhost, "md5") == 0 &&
1164  strcmp(authmethodlocal, "scram-sha-256") != 0))
1165  {
1166  conflines = replace_token(conflines,
1167  "#password_encryption = scram-sha-256",
1168  "password_encryption = md5");
1169  }
1170 
1171  /*
1172  * If group access has been enabled for the cluster then it makes sense to
1173  * ensure that the log files also allow group access. Otherwise a backup
1174  * from a user in the group would fail if the log files were not
1175  * relocated.
1176  */
1178  {
1179  conflines = replace_token(conflines,
1180  "#log_file_mode = 0600",
1181  "log_file_mode = 0640");
1182  }
1183 
1184  snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1185 
1186  writefile(path, conflines);
1187  if (chmod(path, pg_file_create_mode) != 0)
1188  pg_fatal("could not change permissions of \"%s\": %m", path);
1189 
1190  /*
1191  * create the automatic configuration file to store the configuration
1192  * parameters set by ALTER SYSTEM command. The parameters present in this
1193  * file will override the value of parameters that exists before parse of
1194  * this file.
1195  */
1196  autoconflines[0] = pg_strdup("# Do not edit this file manually!\n");
1197  autoconflines[1] = pg_strdup("# It will be overwritten by the ALTER SYSTEM command.\n");
1198  autoconflines[2] = NULL;
1199 
1200  sprintf(path, "%s/postgresql.auto.conf", pg_data);
1201 
1202  writefile(path, autoconflines);
1203  if (chmod(path, pg_file_create_mode) != 0)
1204  pg_fatal("could not change permissions of \"%s\": %m", path);
1205 
1206  free(conflines);
1207 
1208 
1209  /* pg_hba.conf */
1210 
1211  conflines = readfile(hba_file);
1212 
1213 #ifndef HAVE_UNIX_SOCKETS
1214  conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
1215 #else
1216  conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
1217 #endif
1218 
1219 #ifdef HAVE_IPV6
1220 
1221  /*
1222  * Probe to see if there is really any platform support for IPv6, and
1223  * comment out the relevant pg_hba line if not. This avoids runtime
1224  * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1225  * useful on Windows, where executables built on a machine with IPv6 may
1226  * have to run on a machine without.
1227  */
1228  {
1229  struct addrinfo *gai_result;
1230  struct addrinfo hints;
1231  int err = 0;
1232 
1233 #ifdef WIN32
1234  /* need to call WSAStartup before calling getaddrinfo */
1235  WSADATA wsaData;
1236 
1237  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1238 #endif
1239 
1240  /* for best results, this code should match parse_hba_line() */
1241  hints.ai_flags = AI_NUMERICHOST;
1242  hints.ai_family = AF_UNSPEC;
1243  hints.ai_socktype = 0;
1244  hints.ai_protocol = 0;
1245  hints.ai_addrlen = 0;
1246  hints.ai_canonname = NULL;
1247  hints.ai_addr = NULL;
1248  hints.ai_next = NULL;
1249 
1250  if (err != 0 ||
1251  getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1252  {
1253  conflines = replace_token(conflines,
1254  "host all all ::1",
1255  "#host all all ::1");
1256  conflines = replace_token(conflines,
1257  "host replication all ::1",
1258  "#host replication all ::1");
1259  }
1260  }
1261 #else /* !HAVE_IPV6 */
1262  /* If we didn't compile IPV6 support at all, always comment it out */
1263  conflines = replace_token(conflines,
1264  "host all all ::1",
1265  "#host all all ::1");
1266  conflines = replace_token(conflines,
1267  "host replication all ::1",
1268  "#host replication all ::1");
1269 #endif /* HAVE_IPV6 */
1270 
1271  /* Replace default authentication methods */
1272  conflines = replace_token(conflines,
1273  "@authmethodhost@",
1274  authmethodhost);
1275  conflines = replace_token(conflines,
1276  "@authmethodlocal@",
1277  authmethodlocal);
1278 
1279  conflines = replace_token(conflines,
1280  "@authcomment@",
1281  (strcmp(authmethodlocal, "trust") == 0 || strcmp(authmethodhost, "trust") == 0) ? AUTHTRUST_WARNING : "");
1282 
1283  snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1284 
1285  writefile(path, conflines);
1286  if (chmod(path, pg_file_create_mode) != 0)
1287  pg_fatal("could not change permissions of \"%s\": %m", path);
1288 
1289  free(conflines);
1290 
1291  /* pg_ident.conf */
1292 
1293  conflines = readfile(ident_file);
1294 
1295  snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1296 
1297  writefile(path, conflines);
1298  if (chmod(path, pg_file_create_mode) != 0)
1299  pg_fatal("could not change permissions of \"%s\": %m", path);
1300 
1301  free(conflines);
1302 
1303  check_ok();
1304 }
1305 
1306 
1307 /*
1308  * run the BKI script in bootstrap mode to create template1
1309  */
1310 static void
1312 {
1313  PG_CMD_DECL;
1314  char **line;
1315  char **bki_lines;
1316  char headerline[MAXPGPATH];
1317  char buf[64];
1318 
1319  printf(_("running bootstrap script ... "));
1320  fflush(stdout);
1321 
1322  bki_lines = readfile(bki_file);
1323 
1324  /* Check that bki file appears to be of the right version */
1325 
1326  snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1327  PG_MAJORVERSION);
1328 
1329  if (strcmp(headerline, *bki_lines) != 0)
1330  {
1331  pg_log_error("input file \"%s\" does not belong to PostgreSQL %s",
1332  bki_file, PG_VERSION);
1333  pg_log_error_hint("Specify the correct path using the option -L.");
1334  exit(1);
1335  }
1336 
1337  /* Substitute for various symbols used in the BKI file */
1338 
1339  sprintf(buf, "%d", NAMEDATALEN);
1340  bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1341 
1342  sprintf(buf, "%d", (int) sizeof(Pointer));
1343  bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
1344 
1345  bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
1346  (sizeof(Pointer) == 4) ? "i" : "d");
1347 
1348  bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1349  FLOAT8PASSBYVAL ? "true" : "false");
1350 
1351  bki_lines = replace_token(bki_lines, "POSTGRES",
1353 
1354  bki_lines = replace_token(bki_lines, "ENCODING",
1356 
1357  bki_lines = replace_token(bki_lines, "LC_COLLATE",
1359 
1360  bki_lines = replace_token(bki_lines, "LC_CTYPE",
1362 
1363  bki_lines = replace_token(bki_lines, "ICU_LOCALE",
1364  locale_provider == COLLPROVIDER_ICU ? escape_quotes_bki(icu_locale) : "_null_");
1365 
1366  sprintf(buf, "%c", locale_provider);
1367  bki_lines = replace_token(bki_lines, "LOCALE_PROVIDER", buf);
1368 
1369  /* Also ensure backend isn't confused by this environment var: */
1370  unsetenv("PGCLIENTENCODING");
1371 
1372  snprintf(cmd, sizeof(cmd),
1373  "\"%s\" --boot -X %d %s %s %s %s",
1374  backend_exec,
1375  wal_segment_size_mb * (1024 * 1024),
1376  data_checksums ? "-k" : "",
1378  debug ? "-d 5" : "");
1379 
1380 
1381  PG_CMD_OPEN;
1382 
1383  for (line = bki_lines; *line != NULL; line++)
1384  {
1385  PG_CMD_PUTS(*line);
1386  free(*line);
1387  }
1388 
1389  PG_CMD_CLOSE;
1390 
1391  free(bki_lines);
1392 
1393  check_ok();
1394 }
1395 
1396 /*
1397  * set up the shadow password table
1398  */
1399 static void
1400 setup_auth(FILE *cmdfd)
1401 {
1402  const char *const *line;
1403  static const char *const pg_authid_setup[] = {
1404  /*
1405  * The authid table shouldn't be readable except through views, to
1406  * ensure passwords are not publicly visible.
1407  */
1408  "REVOKE ALL ON pg_authid FROM public;\n\n",
1409  NULL
1410  };
1411 
1412  for (line = pg_authid_setup; *line != NULL; line++)
1413  PG_CMD_PUTS(*line);
1414 
1415  if (superuser_password)
1416  PG_CMD_PRINTF("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1418 }
1419 
1420 /*
1421  * get the superuser password if required
1422  */
1423 static void
1425 {
1426  char *pwd1;
1427 
1428  if (pwprompt)
1429  {
1430  /*
1431  * Read password from terminal
1432  */
1433  char *pwd2;
1434 
1435  printf("\n");
1436  fflush(stdout);
1437  pwd1 = simple_prompt("Enter new superuser password: ", false);
1438  pwd2 = simple_prompt("Enter it again: ", false);
1439  if (strcmp(pwd1, pwd2) != 0)
1440  {
1441  fprintf(stderr, _("Passwords didn't match.\n"));
1442  exit(1);
1443  }
1444  free(pwd2);
1445  }
1446  else
1447  {
1448  /*
1449  * Read password from file
1450  *
1451  * Ideally this should insist that the file not be world-readable.
1452  * However, this option is mainly intended for use on Windows where
1453  * file permissions may not exist at all, so we'll skip the paranoia
1454  * for now.
1455  */
1456  FILE *pwf = fopen(pwfilename, "r");
1457 
1458  if (!pwf)
1459  pg_fatal("could not open file \"%s\" for reading: %m",
1460  pwfilename);
1461  pwd1 = pg_get_line(pwf, NULL);
1462  if (!pwd1)
1463  {
1464  if (ferror(pwf))
1465  pg_fatal("could not read password from file \"%s\": %m",
1466  pwfilename);
1467  else
1468  pg_fatal("password file \"%s\" is empty",
1469  pwfilename);
1470  }
1471  fclose(pwf);
1472 
1473  (void) pg_strip_crlf(pwd1);
1474  }
1475 
1476  superuser_password = pwd1;
1477 }
1478 
1479 /*
1480  * set up pg_depend
1481  */
1482 static void
1483 setup_depend(FILE *cmdfd)
1484 {
1485  const char *const *line;
1486  static const char *const pg_depend_setup[] = {
1487  /*
1488  * Advance the OID counter so that subsequently-created objects aren't
1489  * pinned.
1490  */
1491  "SELECT pg_stop_making_pinned_objects();\n\n",
1492  NULL
1493  };
1494 
1495  for (line = pg_depend_setup; *line != NULL; line++)
1496  PG_CMD_PUTS(*line);
1497 }
1498 
1499 /*
1500  * Run external file
1501  */
1502 static void
1503 setup_run_file(FILE *cmdfd, const char *filename)
1504 {
1505  char **lines;
1506 
1507  lines = readfile(filename);
1508 
1509  for (char **line = lines; *line != NULL; line++)
1510  {
1511  PG_CMD_PUTS(*line);
1512  free(*line);
1513  }
1514 
1515  PG_CMD_PUTS("\n\n");
1516 
1517  free(lines);
1518 }
1519 
1520 /*
1521  * fill in extra description data
1522  */
1523 static void
1524 setup_description(FILE *cmdfd)
1525 {
1526  /* Create default descriptions for operator implementation functions */
1527  PG_CMD_PUTS("WITH funcdescs AS ( "
1528  "SELECT p.oid as p_oid, o.oid as o_oid, oprname "
1529  "FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
1530  "INSERT INTO pg_description "
1531  " SELECT p_oid, 'pg_proc'::regclass, 0, "
1532  " 'implementation of ' || oprname || ' operator' "
1533  " FROM funcdescs "
1534  " WHERE NOT EXISTS (SELECT 1 FROM pg_description "
1535  " WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass) "
1536  " AND NOT EXISTS (SELECT 1 FROM pg_description "
1537  " WHERE objoid = o_oid AND classoid = 'pg_operator'::regclass"
1538  " AND description LIKE 'deprecated%');\n\n");
1539 }
1540 
1541 /*
1542  * populate pg_collation
1543  */
1544 static void
1545 setup_collation(FILE *cmdfd)
1546 {
1547  /*
1548  * Add an SQL-standard name. We don't want to pin this, so it doesn't go
1549  * in pg_collation.h. But add it before reading system collations, so
1550  * that it wins if libc defines a locale named ucs_basic.
1551  */
1552  PG_CMD_PRINTF("INSERT INTO pg_collation (oid, collname, collnamespace, collowner, collprovider, collisdeterministic, collencoding, collcollate, collctype)"
1553  "VALUES (pg_nextoid('pg_catalog.pg_collation', 'oid', 'pg_catalog.pg_collation_oid_index'), 'ucs_basic', 'pg_catalog'::regnamespace, %u, '%c', true, %d, 'C', 'C');\n\n",
1554  BOOTSTRAP_SUPERUSERID, COLLPROVIDER_LIBC, PG_UTF8);
1555 
1556  /* Now import all collations we can find in the operating system */
1557  PG_CMD_PUTS("SELECT pg_import_system_collations('pg_catalog');\n\n");
1558 }
1559 
1560 /*
1561  * Set up privileges
1562  *
1563  * We mark most system catalogs as world-readable. We don't currently have
1564  * to touch functions, languages, or databases, because their default
1565  * permissions are OK.
1566  *
1567  * Some objects may require different permissions by default, so we
1568  * make sure we don't overwrite privilege sets that have already been
1569  * set (NOT NULL).
1570  *
1571  * Also populate pg_init_privs to save what the privileges are at init
1572  * time. This is used by pg_dump to allow users to change privileges
1573  * on catalog objects and to have those privilege changes preserved
1574  * across dump/reload and pg_upgrade.
1575  *
1576  * Note that pg_init_privs is only for per-database objects and therefore
1577  * we don't include databases or tablespaces.
1578  */
1579 static void
1580 setup_privileges(FILE *cmdfd)
1581 {
1582  char **line;
1583  char **priv_lines;
1584  static char *privileges_setup[] = {
1585  "UPDATE pg_class "
1586  " SET relacl = (SELECT array_agg(a.acl) FROM "
1587  " (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl "
1588  " UNION SELECT unnest(pg_catalog.acldefault("
1589  " CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) " THEN 's' "
1590  " ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
1591  " ) as a) "
1592  " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1593  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1594  CppAsString2(RELKIND_SEQUENCE) ")"
1595  " AND relacl IS NULL;\n\n",
1596  "GRANT USAGE ON SCHEMA pg_catalog, public TO PUBLIC;\n\n",
1597  "REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n",
1598  "INSERT INTO pg_init_privs "
1599  " (objoid, classoid, objsubid, initprivs, privtype)"
1600  " SELECT"
1601  " oid,"
1602  " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1603  " 0,"
1604  " relacl,"
1605  " 'i'"
1606  " FROM"
1607  " pg_class"
1608  " WHERE"
1609  " relacl IS NOT NULL"
1610  " AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1611  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1612  CppAsString2(RELKIND_SEQUENCE) ");\n\n",
1613  "INSERT INTO pg_init_privs "
1614  " (objoid, classoid, objsubid, initprivs, privtype)"
1615  " SELECT"
1616  " pg_class.oid,"
1617  " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1618  " pg_attribute.attnum,"
1619  " pg_attribute.attacl,"
1620  " 'i'"
1621  " FROM"
1622  " pg_class"
1623  " JOIN pg_attribute ON (pg_class.oid = pg_attribute.attrelid)"
1624  " WHERE"
1625  " pg_attribute.attacl IS NOT NULL"
1626  " AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1627  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1628  CppAsString2(RELKIND_SEQUENCE) ");\n\n",
1629  "INSERT INTO pg_init_privs "
1630  " (objoid, classoid, objsubid, initprivs, privtype)"
1631  " SELECT"
1632  " oid,"
1633  " (SELECT oid FROM pg_class WHERE relname = 'pg_proc'),"
1634  " 0,"
1635  " proacl,"
1636  " 'i'"
1637  " FROM"
1638  " pg_proc"
1639  " WHERE"
1640  " proacl IS NOT NULL;\n\n",
1641  "INSERT INTO pg_init_privs "
1642  " (objoid, classoid, objsubid, initprivs, privtype)"
1643  " SELECT"
1644  " oid,"
1645  " (SELECT oid FROM pg_class WHERE relname = 'pg_type'),"
1646  " 0,"
1647  " typacl,"
1648  " 'i'"
1649  " FROM"
1650  " pg_type"
1651  " WHERE"
1652  " typacl IS NOT NULL;\n\n",
1653  "INSERT INTO pg_init_privs "
1654  " (objoid, classoid, objsubid, initprivs, privtype)"
1655  " SELECT"
1656  " oid,"
1657  " (SELECT oid FROM pg_class WHERE relname = 'pg_language'),"
1658  " 0,"
1659  " lanacl,"
1660  " 'i'"
1661  " FROM"
1662  " pg_language"
1663  " WHERE"
1664  " lanacl IS NOT NULL;\n\n",
1665  "INSERT INTO pg_init_privs "
1666  " (objoid, classoid, objsubid, initprivs, privtype)"
1667  " SELECT"
1668  " oid,"
1669  " (SELECT oid FROM pg_class WHERE "
1670  " relname = 'pg_largeobject_metadata'),"
1671  " 0,"
1672  " lomacl,"
1673  " 'i'"
1674  " FROM"
1675  " pg_largeobject_metadata"
1676  " WHERE"
1677  " lomacl IS NOT NULL;\n\n",
1678  "INSERT INTO pg_init_privs "
1679  " (objoid, classoid, objsubid, initprivs, privtype)"
1680  " SELECT"
1681  " oid,"
1682  " (SELECT oid FROM pg_class WHERE relname = 'pg_namespace'),"
1683  " 0,"
1684  " nspacl,"
1685  " 'i'"
1686  " FROM"
1687  " pg_namespace"
1688  " WHERE"
1689  " nspacl IS NOT NULL;\n\n",
1690  "INSERT INTO pg_init_privs "
1691  " (objoid, classoid, objsubid, initprivs, privtype)"
1692  " SELECT"
1693  " oid,"
1694  " (SELECT oid FROM pg_class WHERE "
1695  " relname = 'pg_foreign_data_wrapper'),"
1696  " 0,"
1697  " fdwacl,"
1698  " 'i'"
1699  " FROM"
1700  " pg_foreign_data_wrapper"
1701  " WHERE"
1702  " fdwacl IS NOT NULL;\n\n",
1703  "INSERT INTO pg_init_privs "
1704  " (objoid, classoid, objsubid, initprivs, privtype)"
1705  " SELECT"
1706  " oid,"
1707  " (SELECT oid FROM pg_class "
1708  " WHERE relname = 'pg_foreign_server'),"
1709  " 0,"
1710  " srvacl,"
1711  " 'i'"
1712  " FROM"
1713  " pg_foreign_server"
1714  " WHERE"
1715  " srvacl IS NOT NULL;\n\n",
1716  NULL
1717  };
1718 
1719  priv_lines = replace_token(privileges_setup, "$POSTGRES_SUPERUSERNAME",
1721  for (line = priv_lines; *line != NULL; line++)
1722  PG_CMD_PUTS(*line);
1723 }
1724 
1725 /*
1726  * extract the strange version of version required for information schema
1727  * (09.08.0007abc)
1728  */
1729 static void
1731 {
1732  char *letterversion;
1733  long major = 0,
1734  minor = 0,
1735  micro = 0;
1736  char *endptr;
1737  char *vstr = pg_strdup(PG_VERSION);
1738  char *ptr;
1739 
1740  ptr = vstr + (strlen(vstr) - 1);
1741  while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1742  ptr--;
1743  letterversion = ptr + 1;
1744  major = strtol(vstr, &endptr, 10);
1745  if (*endptr)
1746  minor = strtol(endptr + 1, &endptr, 10);
1747  if (*endptr)
1748  micro = strtol(endptr + 1, &endptr, 10);
1749  snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1750  major, minor, micro, letterversion);
1751 }
1752 
1753 /*
1754  * load info schema and populate from features file
1755  */
1756 static void
1757 setup_schema(FILE *cmdfd)
1758 {
1760 
1761  PG_CMD_PRINTF("UPDATE information_schema.sql_implementation_info "
1762  " SET character_value = '%s' "
1763  " WHERE implementation_info_name = 'DBMS VERSION';\n\n",
1764  infoversion);
1765 
1766  PG_CMD_PRINTF("COPY information_schema.sql_features "
1767  " (feature_id, feature_name, sub_feature_id, "
1768  " sub_feature_name, is_supported, comments) "
1769  " FROM E'%s';\n\n",
1771 }
1772 
1773 /*
1774  * load PL/pgSQL server-side language
1775  */
1776 static void
1777 load_plpgsql(FILE *cmdfd)
1778 {
1779  PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n\n");
1780 }
1781 
1782 /*
1783  * clean everything up in template1
1784  */
1785 static void
1786 vacuum_db(FILE *cmdfd)
1787 {
1788  /* Run analyze before VACUUM so the statistics are frozen. */
1789  PG_CMD_PUTS("ANALYZE;\n\nVACUUM FREEZE;\n\n");
1790 }
1791 
1792 /*
1793  * copy template1 to template0
1794  */
1795 static void
1796 make_template0(FILE *cmdfd)
1797 {
1798  const char *const *line;
1799 
1800  /*
1801  * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
1802  * enough to drop and recreate a conflicting database with the same name,
1803  * but if the same OID were used for one system-created database in the
1804  * old cluster and a different system-created database in the new cluster,
1805  * it would fail. To avoid that, assign a fixed OID to template0 rather
1806  * than letting the server choose one.
1807  *
1808  * (Note that, while the user could have dropped and recreated these
1809  * objects in the old cluster, the problem scenario only exists if the OID
1810  * that is in use in the old cluster is also used in the new cluster - and
1811  * the new cluster should be the result of a fresh initdb.)
1812  *
1813  * We use "STRATEGY = file_copy" here because checkpoints during initdb
1814  * are cheap. "STRATEGY = wal_log" would generate more WAL, which would be
1815  * a little bit slower and make the new cluster a little bit bigger.
1816  */
1817  static const char *const template0_setup[] = {
1818  "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false"
1819  " OID = " CppAsString2(Template0DbOid)
1820  " STRATEGY = file_copy;\n\n",
1821 
1822  /*
1823  * template0 shouldn't have any collation-dependent objects, so unset
1824  * the collation version. This disables collation version checks when
1825  * making a new database from it.
1826  */
1827  "UPDATE pg_database SET datcollversion = NULL WHERE datname = 'template0';\n\n",
1828 
1829  /*
1830  * While we are here, do set the collation version on template1.
1831  */
1832  "UPDATE pg_database SET datcollversion = pg_database_collation_actual_version(oid) WHERE datname = 'template1';\n\n",
1833 
1834  /*
1835  * Explicitly revoke public create-schema and create-temp-table
1836  * privileges in template1 and template0; else the latter would be on
1837  * by default
1838  */
1839  "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n",
1840  "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n",
1841 
1842  "COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n",
1843 
1844  /*
1845  * Finally vacuum to clean up dead rows in pg_database
1846  */
1847  "VACUUM pg_database;\n\n",
1848  NULL
1849  };
1850 
1851  for (line = template0_setup; *line; line++)
1852  PG_CMD_PUTS(*line);
1853 }
1854 
1855 /*
1856  * copy template1 to postgres
1857  */
1858 static void
1859 make_postgres(FILE *cmdfd)
1860 {
1861  const char *const *line;
1862 
1863  /*
1864  * Just as we did for template0, and for the same reasons, assign a fixed
1865  * OID to postgres and select the file_copy strategy.
1866  */
1867  static const char *const postgres_setup[] = {
1868  "CREATE DATABASE postgres OID = " CppAsString2(PostgresDbOid)
1869  " STRATEGY = file_copy;\n\n",
1870  "COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
1871  NULL
1872  };
1873 
1874  for (line = postgres_setup; *line; line++)
1875  PG_CMD_PUTS(*line);
1876 }
1877 
1878 /*
1879  * signal handler in case we are interrupted.
1880  *
1881  * The Windows runtime docs at
1882  * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/signal
1883  * specifically forbid a number of things being done from a signal handler,
1884  * including IO, memory allocation and system calls, and only allow jmpbuf
1885  * if you are handling SIGFPE.
1886  *
1887  * I avoided doing the forbidden things by setting a flag instead of calling
1888  * exit() directly.
1889  *
1890  * Also note the behaviour of Windows with SIGINT, which says this:
1891  * SIGINT is not supported for any Win32 application. When a CTRL+C interrupt
1892  * occurs, Win32 operating systems generate a new thread to specifically
1893  * handle that interrupt. This can cause a single-thread application, such as
1894  * one in UNIX, to become multithreaded and cause unexpected behavior.
1895  *
1896  * I have no idea how to handle this. (Strange they call UNIX an application!)
1897  * So this will need some testing on Windows.
1898  */
1899 static void
1900 trapsig(int signum)
1901 {
1902  /* handle systems that reset the handler, like Windows (grr) */
1903  pqsignal(signum, trapsig);
1904  caught_signal = true;
1905 }
1906 
1907 /*
1908  * call exit() if we got a signal, or else output "ok".
1909  */
1910 static void
1912 {
1913  if (caught_signal)
1914  {
1915  printf(_("caught signal\n"));
1916  fflush(stdout);
1917  exit(1);
1918  }
1919  else if (output_failed)
1920  {
1921  printf(_("could not write to child process: %s\n"),
1923  fflush(stdout);
1924  exit(1);
1925  }
1926  else
1927  {
1928  /* all seems well */
1929  printf(_("ok\n"));
1930  fflush(stdout);
1931  }
1932 }
1933 
1934 /* Hack to suppress a warning about %x from some versions of gcc */
1935 static inline size_t
1936 my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
1937 {
1938  return strftime(s, max, fmt, tm);
1939 }
1940 
1941 /*
1942  * Determine likely date order from locale
1943  */
1944 static int
1946 {
1947  struct tm testtime;
1948  char buf[128];
1949  char *posD;
1950  char *posM;
1951  char *posY;
1952  char *save;
1953  size_t res;
1954  int result;
1955 
1956  result = DATEORDER_MDY; /* default */
1957 
1958  save = setlocale(LC_TIME, NULL);
1959  if (!save)
1960  return result;
1961  save = pg_strdup(save);
1962 
1963  setlocale(LC_TIME, locale);
1964 
1965  memset(&testtime, 0, sizeof(testtime));
1966  testtime.tm_mday = 22;
1967  testtime.tm_mon = 10; /* November, should come out as "11" */
1968  testtime.tm_year = 133; /* 2033 */
1969 
1970  res = my_strftime(buf, sizeof(buf), "%x", &testtime);
1971 
1972  setlocale(LC_TIME, save);
1973  free(save);
1974 
1975  if (res == 0)
1976  return result;
1977 
1978  posM = strstr(buf, "11");
1979  posD = strstr(buf, "22");
1980  posY = strstr(buf, "33");
1981 
1982  if (!posM || !posD || !posY)
1983  return result;
1984 
1985  if (posY < posM && posM < posD)
1986  result = DATEORDER_YMD;
1987  else if (posD < posM)
1988  result = DATEORDER_DMY;
1989  else
1990  result = DATEORDER_MDY;
1991 
1992  return result;
1993 }
1994 
1995 /*
1996  * Verify that locale name is valid for the locale category.
1997  *
1998  * If successful, and canonname isn't NULL, a malloc'd copy of the locale's
1999  * canonical name is stored there. This is especially useful for figuring out
2000  * what locale name "" means (ie, the environment value). (Actually,
2001  * it seems that on most implementations that's the only thing it's good for;
2002  * we could wish that setlocale gave back a canonically spelled version of
2003  * the locale name, but typically it doesn't.)
2004  *
2005  * this should match the backend's check_locale() function
2006  */
2007 static void
2008 check_locale_name(int category, const char *locale, char **canonname)
2009 {
2010  char *save;
2011  char *res;
2012 
2013  if (canonname)
2014  *canonname = NULL; /* in case of failure */
2015 
2016  save = setlocale(category, NULL);
2017  if (!save)
2018  pg_fatal("setlocale() failed");
2019 
2020  /* save may be pointing at a modifiable scratch variable, so copy it. */
2021  save = pg_strdup(save);
2022 
2023  /* for setlocale() call */
2024  if (!locale)
2025  locale = "";
2026 
2027  /* set the locale with setlocale, to see if it accepts it. */
2028  res = setlocale(category, locale);
2029 
2030  /* save canonical name if requested. */
2031  if (res && canonname)
2032  *canonname = pg_strdup(res);
2033 
2034  /* restore old value. */
2035  if (!setlocale(category, save))
2036  pg_fatal("failed to restore old locale \"%s\"", save);
2037  free(save);
2038 
2039  /* complain if locale wasn't valid */
2040  if (res == NULL)
2041  {
2042  if (*locale)
2043  pg_fatal("invalid locale name \"%s\"", locale);
2044  else
2045  {
2046  /*
2047  * If no relevant switch was given on command line, locale is an
2048  * empty string, which is not too helpful to report. Presumably
2049  * setlocale() found something it did not like in the environment.
2050  * Ideally we'd report the bad environment variable, but since
2051  * setlocale's behavior is implementation-specific, it's hard to
2052  * be sure what it didn't like. Print a safe generic message.
2053  */
2054  pg_fatal("invalid locale settings; check LANG and LC_* environment variables");
2055  }
2056  }
2057 }
2058 
2059 /*
2060  * check if the chosen encoding matches the encoding required by the locale
2061  *
2062  * this should match the similar check in the backend createdb() function
2063  */
2064 static bool
2065 check_locale_encoding(const char *locale, int user_enc)
2066 {
2067  int locale_enc;
2068 
2069  locale_enc = pg_get_encoding_from_locale(locale, true);
2070 
2071  /* See notes in createdb() to understand these tests */
2072  if (!(locale_enc == user_enc ||
2073  locale_enc == PG_SQL_ASCII ||
2074  locale_enc == -1 ||
2075 #ifdef WIN32
2076  user_enc == PG_UTF8 ||
2077 #endif
2078  user_enc == PG_SQL_ASCII))
2079  {
2080  pg_log_error("encoding mismatch");
2081  pg_log_error_detail("The encoding you selected (%s) and the encoding that the "
2082  "selected locale uses (%s) do not match. This would lead to "
2083  "misbehavior in various character string processing functions.",
2084  pg_encoding_to_char(user_enc),
2085  pg_encoding_to_char(locale_enc));
2086  pg_log_error_hint("Rerun %s and either do not specify an encoding explicitly, "
2087  "or choose a matching combination.",
2088  progname);
2089  return false;
2090  }
2091  return true;
2092 }
2093 
2094 /*
2095  * set up the locale variables
2096  *
2097  * assumes we have called setlocale(LC_ALL, "") -- see set_pglocale_pgservice
2098  */
2099 static void
2101 {
2102  char *canonname;
2103 
2104  /* set empty lc_* values to locale config if set */
2105 
2106  if (locale)
2107  {
2108  if (!lc_ctype)
2109  lc_ctype = locale;
2110  if (!lc_collate)
2111  lc_collate = locale;
2112  if (!lc_numeric)
2113  lc_numeric = locale;
2114  if (!lc_time)
2115  lc_time = locale;
2116  if (!lc_monetary)
2117  lc_monetary = locale;
2118  if (!lc_messages)
2119  lc_messages = locale;
2120  }
2121 
2122  /*
2123  * canonicalize locale names, and obtain any missing values from our
2124  * current environment
2125  */
2126  check_locale_name(LC_CTYPE, lc_ctype, &canonname);
2127  lc_ctype = canonname;
2128  check_locale_name(LC_COLLATE, lc_collate, &canonname);
2129  lc_collate = canonname;
2130  check_locale_name(LC_NUMERIC, lc_numeric, &canonname);
2131  lc_numeric = canonname;
2132  check_locale_name(LC_TIME, lc_time, &canonname);
2133  lc_time = canonname;
2134  check_locale_name(LC_MONETARY, lc_monetary, &canonname);
2135  lc_monetary = canonname;
2136 #if defined(LC_MESSAGES) && !defined(WIN32)
2137  check_locale_name(LC_MESSAGES, lc_messages, &canonname);
2138  lc_messages = canonname;
2139 #else
2140  /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2141  check_locale_name(LC_CTYPE, lc_messages, &canonname);
2142  lc_messages = canonname;
2143 #endif
2144 
2145  if (locale_provider == COLLPROVIDER_ICU)
2146  {
2147  if (!icu_locale)
2148  pg_fatal("ICU locale must be specified");
2149 
2150  /*
2151  * In supported builds, the ICU locale ID will be checked by the
2152  * backend during post-bootstrap initialization.
2153  */
2154 #ifndef USE_ICU
2155  pg_fatal("ICU is not supported in this build");
2156 #endif
2157  }
2158 }
2159 
2160 /*
2161  * print help text
2162  */
2163 static void
2164 usage(const char *progname)
2165 {
2166  printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2167  printf(_("Usage:\n"));
2168  printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2169  printf(_("\nOptions:\n"));
2170  printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2171  printf(_(" --auth-host=METHOD default authentication method for local TCP/IP connections\n"));
2172  printf(_(" --auth-local=METHOD default authentication method for local-socket connections\n"));
2173  printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2174  printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2175  printf(_(" -g, --allow-group-access allow group read/execute on data directory\n"));
2176  printf(_(" --icu-locale=LOCALE set ICU locale ID for new databases\n"));
2177  printf(_(" -k, --data-checksums use data page checksums\n"));
2178  printf(_(" --locale=LOCALE set default locale for new databases\n"));
2179  printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2180  " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2181  " set default locale in the respective category for\n"
2182  " new databases (default taken from environment)\n"));
2183  printf(_(" --no-locale equivalent to --locale=C\n"));
2184  printf(_(" --locale-provider={libc|icu}\n"
2185  " set default locale provider for new databases\n"));
2186  printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2187  printf(_(" -T, --text-search-config=CFG\n"
2188  " default text search configuration\n"));
2189  printf(_(" -U, --username=NAME database superuser name\n"));
2190  printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2191  printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
2192  printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
2193  printf(_("\nLess commonly used options:\n"));
2194  printf(_(" -d, --debug generate lots of debugging output\n"));
2195  printf(_(" --discard-caches set debug_discard_caches=1\n"));
2196  printf(_(" -L DIRECTORY where to find the input files\n"));
2197  printf(_(" -n, --no-clean do not clean up after errors\n"));
2198  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
2199  printf(_(" --no-instructions do not print instructions for next steps\n"));
2200  printf(_(" -s, --show show internal settings\n"));
2201  printf(_(" -S, --sync-only only sync database files to disk, then exit\n"));
2202  printf(_("\nOther options:\n"));
2203  printf(_(" -V, --version output version information, then exit\n"));
2204  printf(_(" -?, --help show this help, then exit\n"));
2205  printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2206  "is used.\n"));
2207  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2208  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2209 }
2210 
2211 static void
2212 check_authmethod_unspecified(const char **authmethod)
2213 {
2214  if (*authmethod == NULL)
2215  {
2216  authwarning = true;
2217  *authmethod = "trust";
2218  }
2219 }
2220 
2221 static void
2222 check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
2223 {
2224  const char *const *p;
2225 
2226  for (p = valid_methods; *p; p++)
2227  {
2228  if (strcmp(authmethod, *p) == 0)
2229  return;
2230  /* with space = param */
2231  if (strchr(authmethod, ' '))
2232  if (strncmp(authmethod, *p, (authmethod - strchr(authmethod, ' '))) == 0)
2233  return;
2234  }
2235 
2236  pg_fatal("invalid authentication method \"%s\" for \"%s\" connections",
2237  authmethod, conntype);
2238 }
2239 
2240 static void
2242 {
2243  if ((strcmp(authmethodlocal, "md5") == 0 ||
2244  strcmp(authmethodlocal, "password") == 0 ||
2245  strcmp(authmethodlocal, "scram-sha-256") == 0) &&
2246  (strcmp(authmethodhost, "md5") == 0 ||
2247  strcmp(authmethodhost, "password") == 0 ||
2248  strcmp(authmethodhost, "scram-sha-256") == 0) &&
2249  !(pwprompt || pwfilename))
2250  pg_fatal("must specify a password for the superuser to enable password authentication");
2251 }
2252 
2253 
2254 void
2256 {
2257  char *pgdata_get_env;
2258 
2259  if (!pg_data)
2260  {
2261  pgdata_get_env = getenv("PGDATA");
2262  if (pgdata_get_env && strlen(pgdata_get_env))
2263  {
2264  /* PGDATA found */
2265  pg_data = pg_strdup(pgdata_get_env);
2266  }
2267  else
2268  {
2269  pg_log_error("no data directory specified");
2270  pg_log_error_hint("You must identify the directory where the data for this database system "
2271  "will reside. Do this with either the invocation option -D or the "
2272  "environment variable PGDATA.");
2273  exit(1);
2274  }
2275  }
2276 
2279 
2280  /*
2281  * we have to set PGDATA for postgres rather than pass it on the command
2282  * line to avoid dumb quoting problems on Windows, and we would especially
2283  * need quotes otherwise on Windows because paths there are most likely to
2284  * have embedded spaces.
2285  */
2286  if (setenv("PGDATA", pg_data, 1) != 0)
2287  pg_fatal("could not set environment");
2288 }
2289 
2290 
2291 void
2293 {
2294  int ret;
2295 
2296  if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
2297  backend_exec)) < 0)
2298  {
2299  char full_path[MAXPGPATH];
2300 
2301  if (find_my_exec(argv0, full_path) < 0)
2302  strlcpy(full_path, progname, sizeof(full_path));
2303 
2304  if (ret == -1)
2305  pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
2306  "postgres", progname, full_path);
2307  else
2308  pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
2309  "postgres", full_path, progname);
2310  }
2311 
2312  /* store binary directory */
2313  strcpy(bin_path, backend_exec);
2314  *last_dir_separator(bin_path) = '\0';
2316 
2317  if (!share_path)
2318  {
2321  }
2322  else if (!is_absolute_path(share_path))
2323  pg_fatal("input file location must be an absolute path");
2324 
2326 }
2327 
2328 void
2330 {
2331  setlocales();
2332 
2333  if (locale_provider == COLLPROVIDER_LIBC &&
2334  strcmp(lc_ctype, lc_collate) == 0 &&
2335  strcmp(lc_ctype, lc_time) == 0 &&
2336  strcmp(lc_ctype, lc_numeric) == 0 &&
2337  strcmp(lc_ctype, lc_monetary) == 0 &&
2338  strcmp(lc_ctype, lc_messages) == 0 &&
2339  (!icu_locale || strcmp(lc_ctype, icu_locale) == 0))
2340  printf(_("The database cluster will be initialized with locale \"%s\".\n"), lc_ctype);
2341  else
2342  {
2343  printf(_("The database cluster will be initialized with this locale configuration:\n"));
2344  printf(_(" provider: %s\n"), collprovider_name(locale_provider));
2345  if (icu_locale)
2346  printf(_(" ICU locale: %s\n"), icu_locale);
2347  printf(_(" LC_COLLATE: %s\n"
2348  " LC_CTYPE: %s\n"
2349  " LC_MESSAGES: %s\n"
2350  " LC_MONETARY: %s\n"
2351  " LC_NUMERIC: %s\n"
2352  " LC_TIME: %s\n"),
2353  lc_collate,
2354  lc_ctype,
2355  lc_messages,
2356  lc_monetary,
2357  lc_numeric,
2358  lc_time);
2359  }
2360 
2361  if (!encoding && locale_provider == COLLPROVIDER_ICU)
2362  encodingid = PG_UTF8;
2363  else if (!encoding)
2364  {
2365  int ctype_enc;
2366 
2367  ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);
2368 
2369  if (ctype_enc == -1)
2370  {
2371  /* Couldn't recognize the locale's codeset */
2372  pg_log_error("could not find suitable encoding for locale \"%s\"",
2373  lc_ctype);
2374  pg_log_error_hint("Rerun %s with the -E option.", progname);
2375  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2376  exit(1);
2377  }
2378  else if (!pg_valid_server_encoding_id(ctype_enc))
2379  {
2380  /*
2381  * We recognized it, but it's not a legal server encoding. On
2382  * Windows, UTF-8 works with any locale, so we can fall back to
2383  * UTF-8.
2384  */
2385 #ifdef WIN32
2386  encodingid = PG_UTF8;
2387  printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
2388  "The default database encoding will be set to \"%s\" instead.\n"),
2389  pg_encoding_to_char(ctype_enc),
2391 #else
2392  pg_log_error("locale \"%s\" requires unsupported encoding \"%s\"",
2393  lc_ctype, pg_encoding_to_char(ctype_enc));
2394  pg_log_error_detail("Encoding \"%s\" is not allowed as a server-side encoding.",
2395  pg_encoding_to_char(ctype_enc));
2396  pg_log_error_hint("Rerun %s with a different locale selection.",
2397  progname);
2398  exit(1);
2399 #endif
2400  }
2401  else
2402  {
2403  encodingid = ctype_enc;
2404  printf(_("The default database encoding has accordingly been set to \"%s\".\n"),
2406  }
2407  }
2408  else
2410 
2413  exit(1); /* check_locale_encoding printed the error */
2414 }
2415 
2416 
2417 void
2419 {
2420  set_input(&bki_file, "postgres.bki");
2421  set_input(&hba_file, "pg_hba.conf.sample");
2422  set_input(&ident_file, "pg_ident.conf.sample");
2423  set_input(&conf_file, "postgresql.conf.sample");
2424  set_input(&dictionary_file, "snowball_create.sql");
2425  set_input(&info_schema_file, "information_schema.sql");
2426  set_input(&features_file, "sql_features.txt");
2427  set_input(&system_constraints_file, "system_constraints.sql");
2428  set_input(&system_functions_file, "system_functions.sql");
2429  set_input(&system_views_file, "system_views.sql");
2430 
2431  if (show_setting || debug)
2432  {
2433  fprintf(stderr,
2434  "VERSION=%s\n"
2435  "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2436  "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2437  "POSTGRESQL_CONF_SAMPLE=%s\n"
2438  "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2439  PG_VERSION,
2441  username, bki_file,
2442  conf_file,
2443  hba_file, ident_file);
2444  if (show_setting)
2445  exit(0);
2446  }
2447 
2458 }
2459 
2460 
2461 void
2463 {
2465  {
2468  {
2469  pg_log_info("could not find suitable text search configuration for locale \"%s\"",
2470  lc_ctype);
2471  default_text_search_config = "simple";
2472  }
2473  }
2474  else
2475  {
2476  const char *checkmatch = find_matching_ts_config(lc_ctype);
2477 
2478  if (checkmatch == NULL)
2479  {
2480  pg_log_warning("suitable text search configuration for locale \"%s\" is unknown",
2481  lc_ctype);
2482  }
2483  else if (strcmp(checkmatch, default_text_search_config) != 0)
2484  {
2485  pg_log_warning("specified text search configuration \"%s\" might not match locale \"%s\"",
2487  }
2488  }
2489 
2490  printf(_("The default text search configuration will be set to \"%s\".\n"),
2492 }
2493 
2494 
2495 void
2497 {
2498  /* some of these are not valid on Windows */
2499 #ifdef SIGHUP
2501 #endif
2502 #ifdef SIGINT
2503  pqsignal(SIGINT, trapsig);
2504 #endif
2505 #ifdef SIGQUIT
2507 #endif
2508 #ifdef SIGTERM
2509  pqsignal(SIGTERM, trapsig);
2510 #endif
2511 
2512  /* Ignore SIGPIPE when writing to backend, so we can clean up */
2513 #ifdef SIGPIPE
2515 #endif
2516 
2517  /* Prevent SIGSYS so we can probe for kernel calls that might not work */
2518 #ifdef SIGSYS
2519  pqsignal(SIGSYS, SIG_IGN);
2520 #endif
2521 }
2522 
2523 
2524 void
2526 {
2527  int ret;
2528 
2529  switch ((ret = pg_check_dir(pg_data)))
2530  {
2531  case 0:
2532  /* PGDATA not there, must create it */
2533  printf(_("creating directory %s ... "),
2534  pg_data);
2535  fflush(stdout);
2536 
2538  pg_fatal("could not create directory \"%s\": %m", pg_data);
2539  else
2540  check_ok();
2541 
2542  made_new_pgdata = true;
2543  break;
2544 
2545  case 1:
2546  /* Present but empty, fix permissions and use it */
2547  printf(_("fixing permissions on existing directory %s ... "),
2548  pg_data);
2549  fflush(stdout);
2550 
2551  if (chmod(pg_data, pg_dir_create_mode) != 0)
2552  pg_fatal("could not change permissions of directory \"%s\": %m",
2553  pg_data);
2554  else
2555  check_ok();
2556 
2557  found_existing_pgdata = true;
2558  break;
2559 
2560  case 2:
2561  case 3:
2562  case 4:
2563  /* Present and not empty */
2564  pg_log_error("directory \"%s\" exists but is not empty", pg_data);
2565  if (ret != 4)
2566  warn_on_mount_point(ret);
2567  else
2568  pg_log_error_hint("If you want to create a new database system, either remove or empty "
2569  "the directory \"%s\" or run %s "
2570  "with an argument other than \"%s\".",
2572  exit(1); /* no further message needed */
2573 
2574  default:
2575  /* Trouble accessing directory */
2576  pg_fatal("could not access directory \"%s\": %m", pg_data);
2577  }
2578 }
2579 
2580 
2581 /* Create WAL directory, and symlink if required */
2582 void
2584 {
2585  char *subdirloc;
2586 
2587  /* form name of the place for the subdirectory or symlink */
2588  subdirloc = psprintf("%s/pg_wal", pg_data);
2589 
2590  if (xlog_dir)
2591  {
2592  int ret;
2593 
2594  /* clean up xlog directory name, check it's absolute */
2596  if (!is_absolute_path(xlog_dir))
2597  pg_fatal("WAL directory location must be an absolute path");
2598 
2599  /* check if the specified xlog directory exists/is empty */
2600  switch ((ret = pg_check_dir(xlog_dir)))
2601  {
2602  case 0:
2603  /* xlog directory not there, must create it */
2604  printf(_("creating directory %s ... "),
2605  xlog_dir);
2606  fflush(stdout);
2607 
2609  pg_fatal("could not create directory \"%s\": %m",
2610  xlog_dir);
2611  else
2612  check_ok();
2613 
2614  made_new_xlogdir = true;
2615  break;
2616 
2617  case 1:
2618  /* Present but empty, fix permissions and use it */
2619  printf(_("fixing permissions on existing directory %s ... "),
2620  xlog_dir);
2621  fflush(stdout);
2622 
2623  if (chmod(xlog_dir, pg_dir_create_mode) != 0)
2624  pg_fatal("could not change permissions of directory \"%s\": %m",
2625  xlog_dir);
2626  else
2627  check_ok();
2628 
2629  found_existing_xlogdir = true;
2630  break;
2631 
2632  case 2:
2633  case 3:
2634  case 4:
2635  /* Present and not empty */
2636  pg_log_error("directory \"%s\" exists but is not empty", xlog_dir);
2637  if (ret != 4)
2638  warn_on_mount_point(ret);
2639  else
2640  pg_log_error_hint("If you want to store the WAL there, either remove or empty the directory \"%s\".",
2641  xlog_dir);
2642  exit(1);
2643 
2644  default:
2645  /* Trouble accessing directory */
2646  pg_fatal("could not access directory \"%s\": %m", xlog_dir);
2647  }
2648 
2649 #ifdef HAVE_SYMLINK
2650  if (symlink(xlog_dir, subdirloc) != 0)
2651  pg_fatal("could not create symbolic link \"%s\": %m",
2652  subdirloc);
2653 #else
2654  pg_fatal("symlinks are not supported on this platform");
2655 #endif
2656  }
2657  else
2658  {
2659  /* Without -X option, just make the subdirectory normally */
2660  if (mkdir(subdirloc, pg_dir_create_mode) < 0)
2661  pg_fatal("could not create directory \"%s\": %m",
2662  subdirloc);
2663  }
2664 
2665  free(subdirloc);
2666 }
2667 
2668 
2669 void
2671 {
2672  if (error == 2)
2673  pg_log_error_detail("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.");
2674  else if (error == 3)
2675  pg_log_error_detail("It contains a lost+found directory, perhaps due to it being a mount point.");
2676 
2677  pg_log_error_hint("Using a mount point directly as the data directory is not recommended.\n"
2678  "Create a subdirectory under the mount point.");
2679 }
2680 
2681 
2682 void
2684 {
2685  PG_CMD_DECL;
2686  int i;
2687 
2688  setup_signals();
2689 
2690  /*
2691  * Set mask based on requested PGDATA permissions. pg_mode_mask, and
2692  * friends like pg_dir_create_mode, are set to owner-only by default and
2693  * then updated if -g is passed in by calling SetDataDirectoryCreatePerm()
2694  * when parsing our options (see above).
2695  */
2696  umask(pg_mode_mask);
2697 
2699 
2701 
2702  /* Create required subdirectories (other than pg_wal) */
2703  printf(_("creating subdirectories ... "));
2704  fflush(stdout);
2705 
2706  for (i = 0; i < lengthof(subdirs); i++)
2707  {
2708  char *path;
2709 
2710  path = psprintf("%s/%s", pg_data, subdirs[i]);
2711 
2712  /*
2713  * The parent directory already exists, so we only need mkdir() not
2714  * pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
2715  */
2716  if (mkdir(path, pg_dir_create_mode) < 0)
2717  pg_fatal("could not create directory \"%s\": %m", path);
2718 
2719  free(path);
2720  }
2721 
2722  check_ok();
2723 
2724  /* Top level PG_VERSION is checked by bootstrapper, so make it first */
2725  write_version_file(NULL);
2726 
2727  /* Select suitable configuration settings */
2728  set_null_conf();
2730 
2731  /* Now create all the text config files */
2732  setup_config();
2733 
2734  /* Bootstrap template1 */
2736 
2737  /*
2738  * Make the per-database PG_VERSION for template1 only after init'ing it
2739  */
2740  write_version_file("base/1");
2741 
2742  /*
2743  * Create the stuff we don't need to use bootstrap mode for, using a
2744  * backend running in simple standalone mode.
2745  */
2746  fputs(_("performing post-bootstrap initialization ... "), stdout);
2747  fflush(stdout);
2748 
2749  snprintf(cmd, sizeof(cmd),
2750  "\"%s\" %s %s template1 >%s",
2752  DEVNULL);
2753 
2754  PG_CMD_OPEN;
2755 
2756  setup_auth(cmdfd);
2757 
2759 
2761 
2762  setup_depend(cmdfd);
2763 
2764  /*
2765  * Note that no objects created after setup_depend() will be "pinned".
2766  * They are all droppable at the whim of the DBA.
2767  */
2768 
2770 
2771  setup_description(cmdfd);
2772 
2773  setup_collation(cmdfd);
2774 
2776 
2777  setup_privileges(cmdfd);
2778 
2779  setup_schema(cmdfd);
2780 
2781  load_plpgsql(cmdfd);
2782 
2783  vacuum_db(cmdfd);
2784 
2785  make_template0(cmdfd);
2786 
2787  make_postgres(cmdfd);
2788 
2789  PG_CMD_CLOSE;
2790 
2791  check_ok();
2792 }
2793 
2794 
2795 int
2796 main(int argc, char *argv[])
2797 {
2798  static struct option long_options[] = {
2799  {"pgdata", required_argument, NULL, 'D'},
2800  {"encoding", required_argument, NULL, 'E'},
2801  {"locale", required_argument, NULL, 1},
2802  {"lc-collate", required_argument, NULL, 2},
2803  {"lc-ctype", required_argument, NULL, 3},
2804  {"lc-monetary", required_argument, NULL, 4},
2805  {"lc-numeric", required_argument, NULL, 5},
2806  {"lc-time", required_argument, NULL, 6},
2807  {"lc-messages", required_argument, NULL, 7},
2808  {"no-locale", no_argument, NULL, 8},
2809  {"text-search-config", required_argument, NULL, 'T'},
2810  {"auth", required_argument, NULL, 'A'},
2811  {"auth-local", required_argument, NULL, 10},
2812  {"auth-host", required_argument, NULL, 11},
2813  {"pwprompt", no_argument, NULL, 'W'},
2814  {"pwfile", required_argument, NULL, 9},
2815  {"username", required_argument, NULL, 'U'},
2816  {"help", no_argument, NULL, '?'},
2817  {"version", no_argument, NULL, 'V'},
2818  {"debug", no_argument, NULL, 'd'},
2819  {"show", no_argument, NULL, 's'},
2820  {"noclean", no_argument, NULL, 'n'}, /* for backwards compatibility */
2821  {"no-clean", no_argument, NULL, 'n'},
2822  {"nosync", no_argument, NULL, 'N'}, /* for backwards compatibility */
2823  {"no-sync", no_argument, NULL, 'N'},
2824  {"no-instructions", no_argument, NULL, 13},
2825  {"sync-only", no_argument, NULL, 'S'},
2826  {"waldir", required_argument, NULL, 'X'},
2827  {"wal-segsize", required_argument, NULL, 12},
2828  {"data-checksums", no_argument, NULL, 'k'},
2829  {"allow-group-access", no_argument, NULL, 'g'},
2830  {"discard-caches", no_argument, NULL, 14},
2831  {"locale-provider", required_argument, NULL, 15},
2832  {"icu-locale", required_argument, NULL, 16},
2833  {NULL, 0, NULL, 0}
2834  };
2835 
2836  /*
2837  * options with no short version return a low integer, the rest return
2838  * their short version value
2839  */
2840  int c;
2841  int option_index;
2842  char *effective_user;
2843  PQExpBuffer start_db_cmd;
2844  char pg_ctl_path[MAXPGPATH];
2845 
2846  /*
2847  * Ensure that buffering behavior of stdout matches what it is in
2848  * interactive usage (at least on most platforms). This prevents
2849  * unexpected output ordering when, eg, output is redirected to a file.
2850  * POSIX says we must do this before any other usage of these files.
2851  */
2852  setvbuf(stdout, NULL, PG_IOLBF, 0);
2853 
2854  pg_logging_init(argv[0]);
2855  progname = get_progname(argv[0]);
2856  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
2857 
2858  if (argc > 1)
2859  {
2860  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2861  {
2862  usage(progname);
2863  exit(0);
2864  }
2865  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2866  {
2867  puts("initdb (PostgreSQL) " PG_VERSION);
2868  exit(0);
2869  }
2870  }
2871 
2872  /* process command-line options */
2873 
2874  while ((c = getopt_long(argc, argv, "A:dD:E:gkL:nNsST:U:WX:", long_options, &option_index)) != -1)
2875  {
2876  switch (c)
2877  {
2878  case 'A':
2880 
2881  /*
2882  * When ident is specified, use peer for local connections.
2883  * Mirrored, when peer is specified, use ident for TCP/IP
2884  * connections.
2885  */
2886  if (strcmp(authmethodhost, "ident") == 0)
2887  authmethodlocal = "peer";
2888  else if (strcmp(authmethodlocal, "peer") == 0)
2889  authmethodhost = "ident";
2890  break;
2891  case 10:
2893  break;
2894  case 11:
2896  break;
2897  case 'D':
2899  break;
2900  case 'E':
2902  break;
2903  case 'W':
2904  pwprompt = true;
2905  break;
2906  case 'U':
2908  break;
2909  case 'd':
2910  debug = true;
2911  printf(_("Running in debug mode.\n"));
2912  break;
2913  case 'n':
2914  noclean = true;
2915  printf(_("Running in no-clean mode. Mistakes will not be cleaned up.\n"));
2916  break;
2917  case 'N':
2918  do_sync = false;
2919  break;
2920  case 'S':
2921  sync_only = true;
2922  break;
2923  case 'k':
2924  data_checksums = true;
2925  break;
2926  case 'L':
2928  break;
2929  case 1:
2930  locale = pg_strdup(optarg);
2931  break;
2932  case 2:
2934  break;
2935  case 3:
2937  break;
2938  case 4:
2940  break;
2941  case 5:
2943  break;
2944  case 6:
2946  break;
2947  case 7:
2949  break;
2950  case 8:
2951  locale = "C";
2952  break;
2953  case 9:
2955  break;
2956  case 's':
2957  show_setting = true;
2958  break;
2959  case 'T':
2961  break;
2962  case 'X':
2964  break;
2965  case 12:
2967  break;
2968  case 13:
2969  noinstructions = true;
2970  break;
2971  case 'g':
2973  break;
2974  case 14:
2975  extra_options = psprintf("%s %s",
2976  extra_options,
2977  "-c debug_discard_caches=1");
2978  break;
2979  case 15:
2980  if (strcmp(optarg, "icu") == 0)
2981  locale_provider = COLLPROVIDER_ICU;
2982  else if (strcmp(optarg, "libc") == 0)
2983  locale_provider = COLLPROVIDER_LIBC;
2984  else
2985  pg_fatal("unrecognized locale provider: %s", optarg);
2986  break;
2987  case 16:
2989  break;
2990  default:
2991  /* getopt_long already emitted a complaint */
2992  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2993  exit(1);
2994  }
2995  }
2996 
2997 
2998  /*
2999  * Non-option argument specifies data directory as long as it wasn't
3000  * already specified with -D / --pgdata
3001  */
3002  if (optind < argc && !pg_data)
3003  {
3004  pg_data = pg_strdup(argv[optind]);
3005  optind++;
3006  }
3007 
3008  if (optind < argc)
3009  {
3010  pg_log_error("too many command-line arguments (first is \"%s\")",
3011  argv[optind]);
3012  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
3013  exit(1);
3014  }
3015 
3016  if (icu_locale && locale_provider != COLLPROVIDER_ICU)
3017  pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3018  "--icu-locale", "icu");
3019 
3021 
3022  /* If we only need to fsync, just do it and exit */
3023  if (sync_only)
3024  {
3025  setup_pgdata();
3026 
3027  /* must check that directory is readable */
3028  if (pg_check_dir(pg_data) <= 0)
3029  pg_fatal("could not access directory \"%s\": %m", pg_data);
3030 
3031  fputs(_("syncing data to disk ... "), stdout);
3032  fflush(stdout);
3033  fsync_pgdata(pg_data, PG_VERSION_NUM);
3034  check_ok();
3035  return 0;
3036  }
3037 
3038  if (pwprompt && pwfilename)
3039  pg_fatal("password prompt and password file cannot be specified together");
3040 
3043 
3046 
3048 
3049  /* set wal segment size */
3050  if (str_wal_segment_size_mb == NULL)
3051  wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024);
3052  else
3053  {
3054  char *endptr;
3055 
3056  /* check that the argument is a number */
3057  wal_segment_size_mb = strtol(str_wal_segment_size_mb, &endptr, 10);
3058 
3059  /* verify that wal segment size is valid */
3060  if (endptr == str_wal_segment_size_mb || *endptr != '\0')
3061  pg_fatal("argument of --wal-segsize must be a number");
3062  if (!IsValidWalSegSize(wal_segment_size_mb * 1024 * 1024))
3063  pg_fatal("argument of --wal-segsize must be a power of 2 between 1 and 1024");
3064  }
3065 
3067 
3068  setup_pgdata();
3069 
3070  setup_bin_paths(argv[0]);
3071 
3072  effective_user = get_id();
3073  if (!username)
3074  username = effective_user;
3075 
3076  if (strncmp(username, "pg_", 3) == 0)
3077  pg_fatal("superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"", username);
3078 
3079  printf(_("The files belonging to this database system will be owned "
3080  "by user \"%s\".\n"
3081  "This user must also own the server process.\n\n"),
3082  effective_user);
3083 
3084  set_info_version();
3085 
3087 
3089 
3091 
3092  printf("\n");
3093 
3094  if (data_checksums)
3095  printf(_("Data page checksums are enabled.\n"));
3096  else
3097  printf(_("Data page checksums are disabled.\n"));
3098 
3099  if (pwprompt || pwfilename)
3100  get_su_pwd();
3101 
3102  printf("\n");
3103 
3105 
3106  if (do_sync)
3107  {
3108  fputs(_("syncing data to disk ... "), stdout);
3109  fflush(stdout);
3110  fsync_pgdata(pg_data, PG_VERSION_NUM);
3111  check_ok();
3112  }
3113  else
3114  printf(_("\nSync to disk skipped.\nThe data directory might become corrupt if the operating system crashes.\n"));
3115 
3116  if (authwarning)
3117  {
3118  printf("\n");
3119  pg_log_warning("enabling \"trust\" authentication for local connections");
3120  pg_log_warning_hint("You can change this by editing pg_hba.conf or using the option -A, or "
3121  "--auth-local and --auth-host, the next time you run initdb.");
3122  }
3123 
3124  if (!noinstructions)
3125  {
3126  /*
3127  * Build up a shell command to tell the user how to start the server
3128  */
3129  start_db_cmd = createPQExpBuffer();
3130 
3131  /* Get directory specification used to start initdb ... */
3132  strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
3133  canonicalize_path(pg_ctl_path);
3134  get_parent_directory(pg_ctl_path);
3135  /* ... and tag on pg_ctl instead */
3136  join_path_components(pg_ctl_path, pg_ctl_path, "pg_ctl");
3137 
3138  /* Convert the path to use native separators */
3139  make_native_path(pg_ctl_path);
3140 
3141  /* path to pg_ctl, properly quoted */
3142  appendShellString(start_db_cmd, pg_ctl_path);
3143 
3144  /* add -D switch, with properly quoted data directory */
3145  appendPQExpBufferStr(start_db_cmd, " -D ");
3146  appendShellString(start_db_cmd, pgdata_native);
3147 
3148  /* add suggested -l switch and "start" command */
3149  /* translator: This is a placeholder in a shell command. */
3150  appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
3151 
3152  printf(_("\nSuccess. You can now start the database server using:\n\n"
3153  " %s\n\n"),
3154  start_db_cmd->data);
3155 
3156  destroyPQExpBuffer(start_db_cmd);
3157  }
3158 
3159 
3160  success = true;
3161  return 0;
3162 }
unsigned int uint32
Definition: c.h:452
char * Pointer
Definition: c.h:429
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1223
#define FLOAT8PASSBYVAL
Definition: c.h:581
#define CppAsString2(x)
Definition: c.h:300
#define lengthof(array)
Definition: c.h:745
#define PG_BINARY_W
Definition: c.h:1282
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:140
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:446
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:335
#define _(x)
Definition: elog.c:89
int pg_valid_server_encoding_id(int encoding)
Definition: encnames.c:514
int pg_valid_server_encoding(const char *name)
Definition: encnames.c:500
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
const char * name
Definition: encode.c:561
struct pg_encoding enc
Definition: encode.c:562
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int pg_file_create_mode
Definition: file_perm.c:19
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
int pg_mode_mask
Definition: file_perm.c:25
int pg_dir_create_mode
Definition: file_perm.c:18
#define PG_DIR_MODE_GROUP
Definition: file_perm.h:35
#define AI_NUMERICHOST
Definition: getaddrinfo.h:73
#define getaddrinfo
Definition: getaddrinfo.h:136
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
#define free(a)
Definition: header.h:65
static char * escape_quotes_bki(const char *src)
Definition: initdb.c:345
static void usage(const char *progname)
Definition: initdb.c:2164
static const char * default_timezone
Definition: initdb.c:184
static char * superuser_password
Definition: initdb.c:141
int main(int argc, char *argv[])
Definition: initdb.c:2796
static char * lc_collate
Definition: initdb.c:129
static char * lc_time
Definition: initdb.c:133
static char * get_id(void)
Definition: initdb.c:591
void warn_on_mount_point(int error)
Definition: initdb.c:2670
static bool noclean
Definition: initdb.c:145
static void setup_depend(FILE *cmdfd)
Definition: initdb.c:1483
static bool found_existing_pgdata
Definition: initdb.c:171
static bool found_existing_xlogdir
Definition: initdb.c:173
static char * hba_file
Definition: initdb.c:160
static char * pgdata_native
Definition: initdb.c:178
#define PG_CMD_PUTS(line)
Definition: initdb.c:312
static void check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
Definition: initdb.c:2222
static char * lc_ctype
Definition: initdb.c:130
static char ** readfile(const char *path)
Definition: initdb.c:455
static int n_connections
Definition: initdb.c:181
static bool noinstructions
Definition: initdb.c:146
void initialize_data_directory(void)
Definition: initdb.c:2683
static void setup_collation(FILE *cmdfd)
Definition: initdb.c:1545
static char * xlog_dir
Definition: initdb.c:151
static bool debug
Definition: initdb.c:144
static char backend_exec[MAXPGPATH]
Definition: initdb.c:240
static bool data_checksums
Definition: initdb.c:150
#define PG_CMD_CLOSE
Definition: initdb.c:306
#define PG_CMD_DECL
Definition: initdb.c:297
void setup_text_search(void)
Definition: initdb.c:2462
static int n_buffers
Definition: initdb.c:182
static int get_encoding_id(const char *encoding_name)
Definition: initdb.c:622
static char * ident_file
Definition: initdb.c:161
static char infoversion[100]
Definition: initdb.c:174
static FILE * popen_check(const char *command, const char *mode)
Definition: initdb.c:520
static const char * authmethodhost
Definition: initdb.c:142
void create_data_directory(void)
Definition: initdb.c:2525
static char * features_file
Definition: initdb.c:165
static void trapsig(int signum)
Definition: initdb.c:1900
static char * share_path
Definition: initdb.c:123
void setup_bin_paths(const char *argv0)
Definition: initdb.c:2292
static void check_authmethod_unspecified(const char **authmethod)
Definition: initdb.c:2212
static char locale_provider
Definition: initdb.c:135
static char * str_wal_segment_size_mb
Definition: initdb.c:152
static const char *const auth_methods_host[]
Definition: initdb.c:84
static int wal_segment_size_mb
Definition: initdb.c:153
static void bootstrap_template1(void)
Definition: initdb.c:1311
static bool success
Definition: initdb.c:169
static char ** filter_lines_with_token(char **lines, const char *token)
Definition: initdb.c:428
const char * select_default_timezone(const char *share_path)
void setup_locale_encoding(void)
Definition: initdb.c:2329
static void setup_run_file(FILE *cmdfd, const char *filename)
Definition: initdb.c:1503
static const char *const auth_methods_local[]
Definition: initdb.c:106
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:372
static const char * backend_options
Definition: initdb.c:207
static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
Definition: initdb.c:1936
static void setlocales(void)
Definition: initdb.c:2100
static bool sync_only
Definition: initdb.c:148
static bool made_new_xlogdir
Definition: initdb.c:172
#define PG_CMD_OPEN
Definition: initdb.c:299
static void setup_auth(FILE *cmdfd)
Definition: initdb.c:1400
static char * pg_data
Definition: initdb.c:126
static void check_need_password(const char *authmethodlocal, const char *authmethodhost)
Definition: initdb.c:2241
static int locale_date_order(const char *locale)
Definition: initdb.c:1945
static void make_postgres(FILE *cmdfd)
Definition: initdb.c:1859
static char * username
Definition: initdb.c:138
static bool do_sync
Definition: initdb.c:147
static void test_config_settings(void)
Definition: initdb.c:892
static void cleanup_directories_atexit(void)
Definition: initdb.c:538
static void setup_config(void)
Definition: initdb.c:1023
static char * locale
Definition: initdb.c:128
static char * lc_messages
Definition: initdb.c:134
static void setup_privileges(FILE *cmdfd)
Definition: initdb.c:1580
static void write_version_file(const char *extrapath)
Definition: initdb.c:798
static const char * boot_options
Definition: initdb.c:206
void setup_signals(void)
Definition: initdb.c:2496
static bool output_failed
Definition: initdb.c:176
static int output_errno
Definition: initdb.c:177
static char * system_views_file
Definition: initdb.c:168
static void setup_description(FILE *cmdfd)
Definition: initdb.c:1524
static char * escape_quotes(const char *src)
Definition: initdb.c:329
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:318
static bool authwarning
Definition: initdb.c:194
static void vacuum_db(FILE *cmdfd)
Definition: initdb.c:1786
static const char *const subdirs[]
Definition: initdb.c:212
static void set_info_version(void)
Definition: initdb.c:1730
static const struct tsearch_config_match tsearch_config_languages[]
Definition: initdb.c:645
static char * icu_locale
Definition: initdb.c:136
static const char * choose_dsm_implementation(void)
Definition: initdb.c:850
void setup_data_file_paths(void)
Definition: initdb.c:2418
static void set_input(char **dest, const char *filename)
Definition: initdb.c:758
static bool made_new_pgdata
Definition: initdb.c:170
static char * encoding
Definition: initdb.c:127
static char * pwfilename
Definition: initdb.c:140
#define MIN_BUFS_FOR_CONNS(nconns)
static char bin_path[MAXPGPATH]
Definition: initdb.c:239
static int encodingid
Definition: initdb.c:158
void create_xlog_or_symlink(void)
Definition: initdb.c:2583
static char * system_functions_file
Definition: initdb.c:167
static bool check_locale_encoding(const char *locale, int encoding)
Definition: initdb.c:2065
static const char * dynamic_shared_memory_type
Definition: initdb.c:183
static void check_input(char *path)
Definition: initdb.c:767
static char * encodingid_to_string(int enc)
Definition: initdb.c:610
static bool caught_signal
Definition: initdb.c:175
static const char * progname
Definition: initdb.c:157
#define AUTHTRUST_WARNING
Definition: initdb.c:189
static char * dictionary_file
Definition: initdb.c:163
static bool pwprompt
Definition: initdb.c:139
static void writefile(char *path, char **lines)
Definition: initdb.c:499
static char * lc_numeric
Definition: initdb.c:132
static void setup_schema(FILE *cmdfd)
Definition: initdb.c:1757
static char * conf_file
Definition: initdb.c:162
void setup_pgdata(void)
Definition: initdb.c:2255
static void check_ok(void)
Definition: initdb.c:1911
static bool show_setting
Definition: initdb.c:149
static char * system_constraints_file
Definition: initdb.c:166
static void set_null_conf(void)
Definition: initdb.c:821
static char * extra_options
Definition: initdb.c:210
static const char * find_matching_ts_config(const char *lc_type)
Definition: initdb.c:712
static const char * authmethodlocal
Definition: initdb.c:143
static void make_template0(FILE *cmdfd)
Definition: initdb.c:1796
static char * info_schema_file
Definition: initdb.c:164
static void load_plpgsql(FILE *cmdfd)
Definition: initdb.c:1777
static const char * default_text_search_config
Definition: initdb.c:137
static char * pretty_wal_size(int segment_count)
Definition: initdb.c:1006
static void check_locale_name(int category, const char *locale, char **canonname)
Definition: initdb.c:2008
static char * lc_monetary
Definition: initdb.c:131
static void get_su_pwd(void)
Definition: initdb.c:1424
static char * bki_file
Definition: initdb.c:159
#define close(a)
Definition: win32.h:12
int i
Definition: isn.c:73
static void const char fflush(stdout)
exit(1)
static void const char * fmt
static struct pg_tm tm
Definition: localtime.c:102
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
#define pg_log_info(...)
Definition: logging.h:124
#define pg_log_warning_hint(...)
Definition: logging.h:121
#define pg_log_error_detail(...)
Definition: logging.h:109
void pfree(void *pointer)
Definition: mcxt.c:1175
#define DATEORDER_DMY
Definition: miscadmin.h:237
#define DATEORDER_MDY
Definition: miscadmin.h:238
#define DATEORDER_YMD
Definition: miscadmin.h:236
#define pg_fatal(...)
static PgChecksumMode mode
Definition: pg_checksums.c:65
#define NAMEDATALEN
#define MAXPGPATH
#define DEFAULT_PGSOCKET_DIR
#define DEFAULT_XLOG_SEG_SIZE
#define DEFAULT_BACKEND_FLUSH_AFTER
#define DEFAULT_CHECKPOINT_FLUSH_AFTER
#define DEFAULT_BGWRITER_FLUSH_AFTER
const void * data
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:102
static char * argv0
Definition: pg_ctl.c:97
static char * filename
Definition: pg_dumpall.c:94
char * pg_get_line(FILE *stream, PromptInterruptContext *prompt_ctx)
Definition: pg_get_line.c:59
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:95
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
uint32 pg_prng_uint32(pg_prng_state *state)
Definition: pg_prng.c:185
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:83
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
static char * buf
Definition: pg_test_fsync.c:67
@ PG_SQL_ASCII
Definition: pg_wchar.h:224
@ PG_UTF8
Definition: pg_wchar.h:230
#define pg_log_warning(...)
Definition: pgfnames.c:24
void get_share_path(const char *my_exec_path, char *ret_path)
Definition: path.c:825
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:219
char * last_dir_separator(const char *filename)
Definition: path.c:139
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
#define is_absolute_path(filename)
Definition: port.h:89
#define PG_IOLBF
Definition: port.h:354
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define sprintf
Definition: port.h:227
void make_native_path(char *path)
Definition: path.c:167
void canonicalize_path(char *path)
Definition: path.c:264
void get_parent_directory(char *path)
Definition: path.c:977
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:33
const char * get_progname(const char *argv0)
Definition: path.c:574
#define strerror
Definition: port.h:238
#define snprintf
Definition: port.h:225
#define DEVNULL
Definition: port.h:156
#define PG_BACKEND_VERSIONSTR
Definition: port.h:139
#define fprintf
Definition: port.h:229
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:451
#define printf(...)
Definition: port.h:231
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * escape_single_quotes_ascii(const char *src)
Definition: quotes.c:33
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * c
static int fd(const char *x, int i)
Definition: preproc-init.c:105
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static chr newline(void)
Definition: regc_lex.c:1001
void get_restricted_token(void)
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static void error(void)
Definition: sql-dyntest.c:147
int pg_strip_crlf(char *str)
Definition: string.c:121
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
size_t ai_addrlen
Definition: getaddrinfo.h:104
struct sockaddr * ai_addr
Definition: getaddrinfo.h:105
char * ai_canonname
Definition: getaddrinfo.h:106
int ai_socktype
Definition: getaddrinfo.h:102
int ai_protocol
Definition: getaddrinfo.h:103
struct addrinfo * ai_next
Definition: getaddrinfo.h:107
int ai_flags
Definition: getaddrinfo.h:100
int ai_family
Definition: getaddrinfo.h:101
unsigned short st_mode
Definition: win32_port.h:268
const char * tsconfname
Definition: initdb.c:641
const char * langname
Definition: initdb.c:642
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74
#define SIGHUP
Definition: win32_port.h:167
#define stat
Definition: win32_port.h:283
#define unsetenv(x)
Definition: win32_port.h:508
#define SIGPIPE
Definition: win32_port.h:172
#define SIGQUIT
Definition: win32_port.h:168
#define mkdir(a, b)
Definition: win32_port.h:71
#define setenv(x, y, z)
Definition: win32_port.h:507
#define symlink(oldpath, newpath)
Definition: win32_port.h:235
#define S_ISREG(m)
Definition: win32_port.h:327
#define setlocale(a, b)
Definition: win32_port.h:446
#define SIG_IGN
Definition: win32_port.h:164
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96
#define DEFAULT_MAX_WAL_SEGS
Definition: xlog_internal.h:92
#define DEFAULT_MIN_WAL_SEGS
Definition: xlog_internal.h:91
static void infile(const char *filename)
Definition: zic.c:1241