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