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