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