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