1 /*-------------------------------------------------------------------------
3 * initdb --- initialize a PostgreSQL installation
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.
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".
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.
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.
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.
33 * The program has some memory leakage - it isn't worth cleaning it up.
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
39 * This code is released under the terms of the PostgreSQL License.
41 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
42 * Portions Copyright (c) 1994, Regents of the University of California
43 * Portions taken from FreeBSD.
47 *-------------------------------------------------------------------------
50 #include "postgres_fe.h"
59 #include "libpq/pqsignal.h"
60 #include "mb/pg_wchar.h"
61 #include "getaddrinfo.h"
62 #include "getopt_long.h"
63 #include "miscadmin.h"
67 * these values are passed in by makefile defines
69 static char *share_path
= NULL
;
71 /* values to be obtained from arguments */
72 static char *pg_data
= "";
73 static char *encoding
= "";
74 static char *locale
= "";
75 static char *lc_collate
= "";
76 static char *lc_ctype
= "";
77 static char *lc_monetary
= "";
78 static char *lc_numeric
= "";
79 static char *lc_time
= "";
80 static char *lc_messages
= "";
81 static const char *default_text_search_config
= "";
82 static char *username
= "";
83 static bool pwprompt
= false;
84 static char *pwfilename
= NULL
;
85 static char *authmethod
= "";
86 static bool debug
= false;
87 static bool noclean
= false;
88 static bool show_setting
= false;
89 static char *xlog_dir
= "";
93 static const char *progname
;
94 static char *encodingid
= "0";
95 static char *bki_file
;
96 static char *desc_file
;
97 static char *shdesc_file
;
98 static char *hba_file
;
99 static char *ident_file
;
100 static char *conf_file
;
101 static char *conversion_file
;
102 static char *dictionary_file
;
103 static char *info_schema_file
;
104 static char *features_file
;
105 static char *system_views_file
;
106 static bool made_new_pgdata
= false;
107 static bool found_existing_pgdata
= false;
108 static bool made_new_xlogdir
= false;
109 static bool found_existing_xlogdir
= false;
110 static char infoversion
[100];
111 static bool caught_signal
= false;
112 static bool output_failed
= false;
113 static int output_errno
= 0;
116 static int n_connections
= 10;
117 static int n_buffers
= 50;
120 * Warning messages for authentication methods
122 #define AUTHTRUST_WARNING \
123 "# CAUTION: Configuring the system for local \"trust\" authentication allows\n" \
124 "# any local user to connect as any PostgreSQL user, including the database\n" \
125 "# superuser. If you do not trust all your local users, use another\n" \
126 "# authentication method.\n"
127 static char *authwarning
= NULL
;
130 * Centralized knowledge of switches to pass to backend
132 * Note: in the shell-script version, we also passed PGDATA as a -D switch,
133 * but here it is more convenient to pass it as an environment variable
134 * (no quoting to worry about).
136 static const char *boot_options
= "-F";
137 static const char *backend_options
= "--single -F -O -c search_path=pg_catalog -c exit_on_error=true";
140 /* path to 'initdb' binary directory */
141 static char bin_path
[MAXPGPATH
];
142 static char backend_exec
[MAXPGPATH
];
144 static void *pg_malloc(size_t size
);
145 static char *xstrdup(const char *s
);
146 static char **replace_token(char **lines
,
147 const char *token
, const char *replacement
);
149 #ifndef HAVE_UNIX_SOCKETS
150 static char **filter_lines_with_token(char **lines
, const char *token
);
152 static char **readfile(char *path
);
153 static void writefile(char *path
, char **lines
);
154 static FILE *popen_check(const char *command
, const char *mode
);
155 static int mkdir_p(char *path
, mode_t omode
);
156 static void exit_nicely(void);
157 static char *get_id(void);
158 static char *get_encoding_id(char *encoding_name
);
159 static char *get_short_version(void);
160 static int check_data_dir(char *dir
);
161 static bool mkdatadir(const char *subdir
);
162 static void set_input(char **dest
, char *filename
);
163 static void check_input(char *path
);
164 static void set_short_version(char *short_version
, char *extrapath
);
165 static void set_null_conf(void);
166 static void test_config_settings(void);
167 static void setup_config(void);
168 static void bootstrap_template1(char *short_version
);
169 static void setup_auth(void);
170 static void get_set_pwd(void);
171 static void setup_depend(void);
172 static void setup_sysviews(void);
173 static void setup_description(void);
174 static void setup_conversion(void);
175 static void setup_dictionary(void);
176 static void setup_privileges(void);
177 static void set_info_version(void);
178 static void setup_schema(void);
179 static void vacuum_db(void);
180 static void make_template0(void);
181 static void make_postgres(void);
182 static void trapsig(int signum
);
183 static void check_ok(void);
184 static char *escape_quotes(const char *src
);
185 static int locale_date_order(const char *locale
);
186 static bool check_locale_name(const char *locale
);
187 static bool check_locale_encoding(const char *locale
, int encoding
);
188 static void setlocales(void);
189 static void usage(const char *progname
);
192 static int CreateRestrictedProcess(char *cmd
, PROCESS_INFORMATION
*processInfo
);
197 * macros for running pipes to postgres
199 #define PG_CMD_DECL char cmd[MAXPGPATH]; FILE *cmdfd
201 #define PG_CMD_OPEN \
203 cmdfd = popen_check(cmd, "w"); \
205 exit_nicely(); /* message already printed by popen_check */ \
208 #define PG_CMD_CLOSE \
210 if (pclose_check(cmdfd)) \
211 exit_nicely(); /* message already printed by pclose_check */ \
214 #define PG_CMD_PUTS(line) \
216 if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
217 output_failed = true, output_errno = errno; \
220 #define PG_CMD_PRINTF1(fmt, arg1) \
222 if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
223 output_failed = true, output_errno = errno; \
226 #define PG_CMD_PRINTF2(fmt, arg1, arg2) \
228 if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
229 output_failed = true, output_errno = errno; \
233 #define QUOTE_PATH ""
236 #define QUOTE_PATH "\""
241 * routines to check mem allocations and fail noisily.
243 * Note that we can't call exit_nicely() on a memory failure, as it calls
244 * rmtree() which needs memory allocation. So we just exit with a bang.
247 pg_malloc(size_t size
)
251 result
= malloc(size
);
254 fprintf(stderr
, _("%s: out of memory\n"), progname
);
261 xstrdup(const char *s
)
268 fprintf(stderr
, _("%s: out of memory\n"), progname
);
275 * make a copy of the array of lines, with token replaced by replacement
276 * the first time it occurs on each line.
278 * This does most of what sed was used for in the shell script, but
279 * doesn't need any regexp stuff.
282 replace_token(char **lines
, const char *token
, const char *replacement
)
291 for (i
= 0; lines
[i
]; i
++)
294 result
= (char **) pg_malloc(numlines
* sizeof(char *));
296 toklen
= strlen(token
);
297 replen
= strlen(replacement
);
298 diff
= replen
- toklen
;
300 for (i
= 0; i
< numlines
; i
++)
306 /* just copy pointer if NULL or no change needed */
307 if (lines
[i
] == NULL
|| (where
= strstr(lines
[i
], token
)) == NULL
)
309 result
[i
] = lines
[i
];
313 /* if we get here a change is needed - set up new line */
315 newline
= (char *) pg_malloc(strlen(lines
[i
]) + diff
+ 1);
317 pre
= where
- lines
[i
];
319 strncpy(newline
, lines
[i
], pre
);
321 strcpy(newline
+ pre
, replacement
);
323 strcpy(newline
+ pre
+ replen
, lines
[i
] + pre
+ toklen
);
332 * make a copy of lines without any that contain the token
334 * a sort of poor man's grep -v
336 #ifndef HAVE_UNIX_SOCKETS
338 filter_lines_with_token(char **lines
, const char *token
)
346 for (i
= 0; lines
[i
]; i
++)
349 result
= (char **) pg_malloc(numlines
* sizeof(char *));
351 for (src
= 0, dst
= 0; src
< numlines
; src
++)
353 if (lines
[src
] == NULL
|| strstr(lines
[src
], token
) == NULL
)
354 result
[dst
++] = lines
[src
];
362 * get the lines from a text file
375 if ((infile
= fopen(path
, "r")) == NULL
)
377 fprintf(stderr
, _("%s: could not open file \"%s\" for reading: %s\n"),
378 progname
, path
, strerror(errno
));
382 /* pass over the file twice - the first time to size the result */
384 while ((c
= fgetc(infile
)) != EOF
)
390 if (linelen
> maxlength
)
396 /* handle last line without a terminating newline (yuck) */
400 if (linelen
> maxlength
)
403 /* set up the result and the line buffer */
405 result
= (char **) pg_malloc((nlines
+ 2) * sizeof(char *));
406 buffer
= (char *) pg_malloc(maxlength
+ 2);
408 /* now reprocess the file and store the lines */
412 while (fgets(buffer
, maxlength
+ 1, infile
) != NULL
)
414 result
[nlines
] = xstrdup(buffer
);
420 result
[nlines
] = NULL
;
426 * write an array of lines to a file
428 * This is only used to write text files. Use fopen "w" not PG_BINARY_W
429 * so that the resulting configuration files are nicely editable on Windows.
432 writefile(char *path
, char **lines
)
437 if ((out_file
= fopen(path
, "w")) == NULL
)
439 fprintf(stderr
, _("%s: could not open file \"%s\" for writing: %s\n"),
440 progname
, path
, strerror(errno
));
443 for (line
= lines
; *line
!= NULL
; line
++)
445 if (fputs(*line
, out_file
) < 0)
447 fprintf(stderr
, _("%s: could not write file \"%s\": %s\n"),
448 progname
, path
, strerror(errno
));
453 if (fclose(out_file
))
455 fprintf(stderr
, _("%s: could not write file \"%s\": %s\n"),
456 progname
, path
, strerror(errno
));
462 * Open a subcommand with suitable error messaging
465 popen_check(const char *command
, const char *mode
)
472 cmdfd
= popen(command
, mode
);
474 fprintf(stderr
, _("%s: could not execute command \"%s\": %s\n"),
475 progname
, command
, strerror(errno
));
479 /* source stolen from FreeBSD /src/bin/mkdir/mkdir.c and adapted */
482 * this tries to build all the elements of a path to a directory a la mkdir -p
483 * we assume the path is in canonical form, i.e. uses / as the separator
484 * we also assume it isn't null.
486 * note that on failure, the path arg has been modified to show the particular
487 * directory level we had problems with.
490 mkdir_p(char *path
, mode_t omode
)
505 /* skip network and drive specifiers for win32 */
508 if (p
[0] == '/' && p
[1] == '/')
511 p
= strstr(p
+ 2, "/");
515 else if (p
[1] == ':' &&
516 ((p
[0] >= 'a' && p
[0] <= 'z') ||
517 (p
[0] >= 'A' && p
[0] <= 'Z')))
525 if (p
[0] == '/') /* Skip leading '/'. */
527 for (first
= 1, last
= 0; !last
; ++p
)
531 else if (p
[0] != '/')
534 if (!last
&& p
[1] == '\0')
539 * POSIX 1003.2: For each dir operand that does not name an
540 * existing directory, effects equivalent to those caused by the
541 * following command shall occcur:
543 * mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode]
546 * We change the user's umask and then restore it, instead of
550 numask
= oumask
& ~(S_IWUSR
| S_IXUSR
);
551 (void) umask(numask
);
555 (void) umask(oumask
);
557 /* check for pre-existing directory; ok if it's a parent */
558 if (stat(path
, &sb
) == 0)
560 if (!S_ISDIR(sb
.st_mode
))
570 else if (mkdir(path
, last
? omode
: S_IRWXU
| S_IRWXG
| S_IRWXO
) < 0)
579 (void) umask(oumask
);
584 * clean up any files we created on failure
585 * if we created the data directory remove it too
594 fprintf(stderr
, _("%s: removing data directory \"%s\"\n"),
596 if (!rmtree(pg_data
, true))
597 fprintf(stderr
, _("%s: failed to remove data directory\n"),
600 else if (found_existing_pgdata
)
603 _("%s: removing contents of data directory \"%s\"\n"),
605 if (!rmtree(pg_data
, false))
606 fprintf(stderr
, _("%s: failed to remove contents of data directory\n"),
610 if (made_new_xlogdir
)
612 fprintf(stderr
, _("%s: removing transaction log directory \"%s\"\n"),
614 if (!rmtree(xlog_dir
, true))
615 fprintf(stderr
, _("%s: failed to remove transaction log directory\n"),
618 else if (found_existing_xlogdir
)
621 _("%s: removing contents of transaction log directory \"%s\"\n"),
623 if (!rmtree(xlog_dir
, false))
624 fprintf(stderr
, _("%s: failed to remove contents of transaction log directory\n"),
627 /* otherwise died during startup, do nothing! */
631 if (made_new_pgdata
|| found_existing_pgdata
)
633 _("%s: data directory \"%s\" not removed at user's request\n"),
636 if (made_new_xlogdir
|| found_existing_xlogdir
)
638 _("%s: transaction log directory \"%s\" not removed at user's request\n"),
646 * find the current user
648 * on unix make sure it isn't really root
657 if (geteuid() == 0) /* 0 is root's uid */
660 _("%s: cannot be run as root\n"
661 "Please log in (using, e.g., \"su\") as the "
662 "(unprivileged) user that will\n"
663 "own the server process.\n"),
668 pw
= getpwuid(geteuid());
672 _("%s: could not obtain information about current user: %s\n"),
673 progname
, strerror(errno
));
676 #else /* the windows code */
683 struct passwd_win32
*pw
= &pass_win32
;
684 DWORD pwname_size
= sizeof(pass_win32
.pw_name
) - 1;
687 if (!GetUserName(pw
->pw_name
, &pwname_size
))
689 fprintf(stderr
, _("%s: could not get current user name: %s\n"),
690 progname
, strerror(errno
));
695 return xstrdup(pw
->pw_name
);
699 encodingid_to_string(int enc
)
703 sprintf(result
, "%d", enc
);
704 return xstrdup(result
);
708 * get the encoding id for a given encoding name
711 get_encoding_id(char *encoding_name
)
715 if (encoding_name
&& *encoding_name
)
717 if ((enc
= pg_valid_server_encoding(encoding_name
)) >= 0)
718 return encodingid_to_string(enc
);
720 fprintf(stderr
, _("%s: \"%s\" is not a valid server encoding name\n"),
721 progname
, encoding_name
? encoding_name
: "(null)");
726 * Support for determining the best default text search configuration.
727 * We key this off the first part of LC_CTYPE (ie, the language name).
729 struct tsearch_config_match
731 const char *tsconfname
;
732 const char *langname
;
735 static const struct tsearch_config_match tsearch_config_languages
[] =
738 {"danish", "Danish"},
742 {"english", "POSIX"},
744 {"english", "English"},
746 {"finnish", "Finnish"},
748 {"french", "French"},
750 {"german", "German"},
752 {"hungarian", "Hungarian"},
754 {"italian", "Italian"},
756 {"norwegian", "Norwegian"},
757 {"portuguese", "pt"},
758 {"portuguese", "Portuguese"},
761 {"russian", "Russian"},
763 {"spanish", "Spanish"},
765 {"swedish", "Swedish"},
767 {"turkish", "Turkish"},
768 {NULL
, NULL
} /* end marker */
772 * Look for a text search configuration matching lc_ctype, and return its
773 * name; return NULL if no match.
776 find_matching_ts_config(const char *lc_type
)
783 * Convert lc_ctype to a language name by stripping everything after an
784 * underscore. Just for paranoia, we also stop at '.' or '@'.
787 langname
= xstrdup("");
790 ptr
= langname
= xstrdup(lc_type
);
791 while (*ptr
&& *ptr
!= '_' && *ptr
!= '.' && *ptr
!= '@')
796 for (i
= 0; tsearch_config_languages
[i
].tsconfname
; i
++)
798 if (pg_strcasecmp(tsearch_config_languages
[i
].langname
, langname
) == 0)
801 return tsearch_config_languages
[i
].tsconfname
;
811 * get short version of VERSION
814 get_short_version(void)
820 vr
= xstrdup(PG_VERSION
);
822 for (end
= 0; vr
[end
] != '\0'; end
++)
833 else if (vr
[end
] < '0' || vr
[end
] > '9')
835 /* gone past digits and dots */
839 if (end
== 0 || vr
[end
- 1] == '.' || !gotdot
)
847 * make sure the directory either doesn't exist or is empty
849 * Returns 0 if nonexistent, 1 if exists and empty, 2 if not empty,
850 * or -1 if trouble accessing directory
853 check_data_dir(char *dir
)
861 chkdir
= opendir(dir
);
864 return (errno
== ENOENT
) ? 0 : -1;
866 while ((file
= readdir(chkdir
)) != NULL
)
868 if (strcmp(".", file
->d_name
) == 0 ||
869 strcmp("..", file
->d_name
) == 0)
871 /* skip this and parent directory */
876 result
= 2; /* not empty */
884 * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
887 if (GetLastError() == ERROR_NO_MORE_FILES
)
894 result
= -1; /* some kind of I/O error? */
900 * make the data directory (or one of its subdirectories if subdir is not NULL)
903 mkdatadir(const char *subdir
)
907 path
= pg_malloc(strlen(pg_data
) + 2 +
908 (subdir
== NULL
? 0 : strlen(subdir
)));
911 sprintf(path
, "%s/%s", pg_data
, subdir
);
913 strcpy(path
, pg_data
);
915 if (mkdir_p(path
, 0700) == 0)
918 fprintf(stderr
, _("%s: could not create directory \"%s\": %s\n"),
919 progname
, path
, strerror(errno
));
926 * set name of given input file variable under data directory
929 set_input(char **dest
, char *filename
)
931 *dest
= pg_malloc(strlen(share_path
) + strlen(filename
) + 2);
932 sprintf(*dest
, "%s/%s", share_path
, filename
);
936 * check that given input file exists
939 check_input(char *path
)
943 if (stat(path
, &statbuf
) != 0)
948 _("%s: file \"%s\" does not exist\n"), progname
, path
);
950 _("This might mean you have a corrupted installation or identified\n"
951 "the wrong directory with the invocation option -L.\n"));
956 _("%s: could not access file \"%s\": %s\n"), progname
, path
,
959 _("This might mean you have a corrupted installation or identified\n"
960 "the wrong directory with the invocation option -L.\n"));
964 if (!S_ISREG(statbuf
.st_mode
))
967 _("%s: file \"%s\" is not a regular file\n"), progname
, path
);
969 _("This might mean you have a corrupted installation or identified\n"
970 "the wrong directory with the invocation option -L.\n"));
976 * write out the PG_VERSION file in the data dir, or its subdirectory
977 * if extrapath is not NULL
980 set_short_version(char *short_version
, char *extrapath
)
985 if (extrapath
== NULL
)
987 path
= pg_malloc(strlen(pg_data
) + 12);
988 sprintf(path
, "%s/PG_VERSION", pg_data
);
992 path
= pg_malloc(strlen(pg_data
) + strlen(extrapath
) + 13);
993 sprintf(path
, "%s/%s/PG_VERSION", pg_data
, extrapath
);
995 version_file
= fopen(path
, PG_BINARY_W
);
996 if (version_file
== NULL
)
998 fprintf(stderr
, _("%s: could not open file \"%s\" for writing: %s\n"),
999 progname
, path
, strerror(errno
));
1002 if (fprintf(version_file
, "%s\n", short_version
) < 0 ||
1003 fclose(version_file
))
1005 fprintf(stderr
, _("%s: could not write file \"%s\": %s\n"),
1006 progname
, path
, strerror(errno
));
1013 * set up an empty config file so we can check config settings by launching
1022 path
= pg_malloc(strlen(pg_data
) + 17);
1023 sprintf(path
, "%s/postgresql.conf", pg_data
);
1024 conf_file
= fopen(path
, PG_BINARY_W
);
1025 if (conf_file
== NULL
)
1027 fprintf(stderr
, _("%s: could not open file \"%s\" for writing: %s\n"),
1028 progname
, path
, strerror(errno
));
1031 if (fclose(conf_file
))
1033 fprintf(stderr
, _("%s: could not write file \"%s\": %s\n"),
1034 progname
, path
, strerror(errno
));
1041 * Determine platform-specific config settings
1043 * Use reasonable values if kernel will let us, else scale back. Probe
1044 * for max_connections first since it is subject to more constraints than
1048 test_config_settings(void)
1051 * This macro defines the minimum shared_buffers we want for a given
1052 * max_connections value. The arrays show the settings to try.
1054 #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
1056 static const int trial_conns
[] = {
1057 100, 50, 40, 30, 20, 10
1059 static const int trial_bufs
[] = {
1060 4096, 3584, 3072, 2560, 2048, 1536,
1061 1000, 900, 800, 700, 600, 500,
1062 400, 300, 200, 100, 50
1065 char cmd
[MAXPGPATH
];
1066 const int connslen
= sizeof(trial_conns
) / sizeof(int);
1067 const int bufslen
= sizeof(trial_bufs
) / sizeof(int);
1075 printf(_("selecting default max_connections ... "));
1078 for (i
= 0; i
< connslen
; i
++)
1080 test_conns
= trial_conns
[i
];
1081 test_buffs
= MIN_BUFS_FOR_CONNS(test_conns
);
1083 snprintf(cmd
, sizeof(cmd
),
1084 SYSTEMQUOTE
"\"%s\" --boot -x0 %s "
1085 "-c max_connections=%d "
1086 "-c shared_buffers=%d "
1087 "< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE
,
1088 backend_exec
, boot_options
,
1089 test_conns
, test_buffs
,
1091 status
= system(cmd
);
1094 ok_buffers
= test_buffs
;
1100 n_connections
= trial_conns
[i
];
1102 printf("%d\n", n_connections
);
1104 printf(_("selecting default shared_buffers ... "));
1107 for (i
= 0; i
< bufslen
; i
++)
1109 /* Use same amount of memory, independent of BLCKSZ */
1110 test_buffs
= (trial_bufs
[i
] * 8192) / BLCKSZ
;
1111 if (test_buffs
<= ok_buffers
)
1113 test_buffs
= ok_buffers
;
1117 snprintf(cmd
, sizeof(cmd
),
1118 SYSTEMQUOTE
"\"%s\" --boot -x0 %s "
1119 "-c max_connections=%d "
1120 "-c shared_buffers=%d "
1121 "< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE
,
1122 backend_exec
, boot_options
,
1123 n_connections
, test_buffs
,
1125 status
= system(cmd
);
1129 n_buffers
= test_buffs
;
1131 if ((n_buffers
* (BLCKSZ
/ 1024)) % 1024 == 0)
1132 printf("%dMB\n", (n_buffers
* (BLCKSZ
/ 1024)) / 1024);
1134 printf("%dkB\n", n_buffers
* (BLCKSZ
/ 1024));
1138 * set up all the config files
1145 char path
[MAXPGPATH
];
1147 fputs(_("creating configuration files ... "), stdout
);
1150 /* postgresql.conf */
1152 conflines
= readfile(conf_file
);
1154 snprintf(repltok
, sizeof(repltok
), "max_connections = %d", n_connections
);
1155 conflines
= replace_token(conflines
, "#max_connections = 100", repltok
);
1157 if ((n_buffers
* (BLCKSZ
/ 1024)) % 1024 == 0)
1158 snprintf(repltok
, sizeof(repltok
), "shared_buffers = %dMB",
1159 (n_buffers
* (BLCKSZ
/ 1024)) / 1024);
1161 snprintf(repltok
, sizeof(repltok
), "shared_buffers = %dkB",
1162 n_buffers
* (BLCKSZ
/ 1024));
1163 conflines
= replace_token(conflines
, "#shared_buffers = 32MB", repltok
);
1165 #if DEF_PGPORT != 5432
1166 snprintf(repltok
, sizeof(repltok
), "#port = %d", DEF_PGPORT
);
1167 conflines
= replace_token(conflines
, "#port = 5432", repltok
);
1170 snprintf(repltok
, sizeof(repltok
), "lc_messages = '%s'",
1171 escape_quotes(lc_messages
));
1172 conflines
= replace_token(conflines
, "#lc_messages = 'C'", repltok
);
1174 snprintf(repltok
, sizeof(repltok
), "lc_monetary = '%s'",
1175 escape_quotes(lc_monetary
));
1176 conflines
= replace_token(conflines
, "#lc_monetary = 'C'", repltok
);
1178 snprintf(repltok
, sizeof(repltok
), "lc_numeric = '%s'",
1179 escape_quotes(lc_numeric
));
1180 conflines
= replace_token(conflines
, "#lc_numeric = 'C'", repltok
);
1182 snprintf(repltok
, sizeof(repltok
), "lc_time = '%s'",
1183 escape_quotes(lc_time
));
1184 conflines
= replace_token(conflines
, "#lc_time = 'C'", repltok
);
1186 switch (locale_date_order(lc_time
))
1189 strcpy(repltok
, "datestyle = 'iso, ymd'");
1192 strcpy(repltok
, "datestyle = 'iso, dmy'");
1196 strcpy(repltok
, "datestyle = 'iso, mdy'");
1199 conflines
= replace_token(conflines
, "#datestyle = 'iso, mdy'", repltok
);
1201 snprintf(repltok
, sizeof(repltok
),
1202 "default_text_search_config = 'pg_catalog.%s'",
1203 escape_quotes(default_text_search_config
));
1204 conflines
= replace_token(conflines
,
1205 "#default_text_search_config = 'pg_catalog.simple'",
1208 snprintf(path
, sizeof(path
), "%s/postgresql.conf", pg_data
);
1210 writefile(path
, conflines
);
1218 conflines
= readfile(hba_file
);
1220 #ifndef HAVE_UNIX_SOCKETS
1221 conflines
= filter_lines_with_token(conflines
, "@remove-line-for-nolocal@");
1223 conflines
= replace_token(conflines
, "@remove-line-for-nolocal@", "");
1229 * Probe to see if there is really any platform support for IPv6, and
1230 * comment out the relevant pg_hba line if not. This avoids runtime
1231 * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1232 * useful on Windows, where executables built on a machine with IPv6 may
1233 * have to run on a machine without.
1236 struct addrinfo
*gai_result
;
1237 struct addrinfo hints
;
1241 /* need to call WSAStartup before calling getaddrinfo */
1244 err
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
1247 /* for best results, this code should match parse_hba() */
1248 hints
.ai_flags
= AI_NUMERICHOST
;
1249 hints
.ai_family
= PF_UNSPEC
;
1250 hints
.ai_socktype
= 0;
1251 hints
.ai_protocol
= 0;
1252 hints
.ai_addrlen
= 0;
1253 hints
.ai_canonname
= NULL
;
1254 hints
.ai_addr
= NULL
;
1255 hints
.ai_next
= NULL
;
1258 getaddrinfo("::1", NULL
, &hints
, &gai_result
) != 0)
1259 conflines
= replace_token(conflines
,
1261 "#host all all ::1");
1263 #else /* !HAVE_IPV6 */
1264 /* If we didn't compile IPV6 support at all, always comment it out */
1265 conflines
= replace_token(conflines
,
1267 "#host all all ::1");
1268 #endif /* HAVE_IPV6 */
1270 /* Replace default authentication methods */
1271 conflines
= replace_token(conflines
,
1275 conflines
= replace_token(conflines
,
1277 strcmp(authmethod
, "trust") ? "" : AUTHTRUST_WARNING
);
1279 snprintf(path
, sizeof(path
), "%s/pg_hba.conf", pg_data
);
1281 writefile(path
, conflines
);
1288 conflines
= readfile(ident_file
);
1290 snprintf(path
, sizeof(path
), "%s/pg_ident.conf", pg_data
);
1292 writefile(path
, conflines
);
1302 * run the BKI script in bootstrap mode to create template1
1305 bootstrap_template1(char *short_version
)
1309 char *talkargs
= "";
1311 char headerline
[MAXPGPATH
];
1314 printf(_("creating template1 database in %s/base/1 ... "), pg_data
);
1320 bki_lines
= readfile(bki_file
);
1322 /* Check that bki file appears to be of the right version */
1324 snprintf(headerline
, sizeof(headerline
), "# PostgreSQL %s\n",
1327 if (strcmp(headerline
, *bki_lines
) != 0)
1330 _("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
1331 "Check your installation or specify the correct path "
1332 "using the option -L.\n"),
1333 progname
, bki_file
, PG_VERSION
);
1337 /* Substitute for various symbols used in the BKI file */
1339 sprintf(buf
, "%d", NAMEDATALEN
);
1340 bki_lines
= replace_token(bki_lines
, "NAMEDATALEN", buf
);
1342 sprintf(buf
, "%d", (int) sizeof(Pointer
));
1343 bki_lines
= replace_token(bki_lines
, "SIZEOF_POINTER", buf
);
1345 bki_lines
= replace_token(bki_lines
, "ALIGNOF_POINTER",
1346 (sizeof(Pointer
) == 4) ? "i" : "d");
1348 bki_lines
= replace_token(bki_lines
, "FLOAT4PASSBYVAL",
1349 FLOAT4PASSBYVAL
? "true" : "false");
1351 bki_lines
= replace_token(bki_lines
, "FLOAT8PASSBYVAL",
1352 FLOAT8PASSBYVAL
? "true" : "false");
1354 bki_lines
= replace_token(bki_lines
, "POSTGRES", username
);
1356 bki_lines
= replace_token(bki_lines
, "ENCODING", encodingid
);
1358 bki_lines
= replace_token(bki_lines
, "LC_COLLATE", lc_collate
);
1360 bki_lines
= replace_token(bki_lines
, "LC_CTYPE", lc_ctype
);
1363 * Pass correct LC_xxx environment to bootstrap.
1365 * The shell script arranged to restore the LC settings afterwards, but
1366 * there doesn't seem to be any compelling reason to do that.
1368 snprintf(cmd
, sizeof(cmd
), "LC_COLLATE=%s", lc_collate
);
1369 putenv(xstrdup(cmd
));
1371 snprintf(cmd
, sizeof(cmd
), "LC_CTYPE=%s", lc_ctype
);
1372 putenv(xstrdup(cmd
));
1376 /* Also ensure backend isn't confused by this environment var: */
1377 unsetenv("PGCLIENTENCODING");
1379 snprintf(cmd
, sizeof(cmd
),
1380 "\"%s\" --boot -x1 %s %s",
1381 backend_exec
, boot_options
, talkargs
);
1385 for (line
= bki_lines
; *line
!= NULL
; line
++)
1399 * set up the shadow password table
1406 static const char *pg_authid_setup
[] = {
1408 * Create triggers to ensure manual updates to shared catalogs will be
1409 * reflected into their "flat file" copies.
1411 "CREATE TRIGGER pg_sync_pg_database "
1412 " AFTER INSERT OR UPDATE OR DELETE ON pg_database "
1413 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1414 "CREATE TRIGGER pg_sync_pg_authid "
1415 " AFTER INSERT OR UPDATE OR DELETE ON pg_authid "
1416 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1417 "CREATE TRIGGER pg_sync_pg_auth_members "
1418 " AFTER INSERT OR UPDATE OR DELETE ON pg_auth_members "
1419 " FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();\n",
1422 * The authid table shouldn't be readable except through views, to
1423 * ensure passwords are not publicly visible.
1425 "REVOKE ALL on pg_authid FROM public;\n",
1429 fputs(_("initializing pg_authid ... "), stdout
);
1432 snprintf(cmd
, sizeof(cmd
),
1433 "\"%s\" %s template1 >%s",
1434 backend_exec
, backend_options
,
1439 for (line
= pg_authid_setup
; *line
!= NULL
; line
++)
1448 * get the superuser password if required, and call postgres to set it
1457 char pwdpath
[MAXPGPATH
];
1458 struct stat statbuf
;
1463 * Read password from terminal
1465 pwd1
= simple_prompt("Enter new superuser password: ", 100, false);
1466 pwd2
= simple_prompt("Enter it again: ", 100, false);
1467 if (strcmp(pwd1
, pwd2
) != 0)
1469 fprintf(stderr
, _("Passwords didn't match.\n"));
1477 * Read password from file
1479 * Ideally this should insist that the file not be world-readable.
1480 * However, this option is mainly intended for use on Windows where
1481 * file permissions may not exist at all, so we'll skip the paranoia
1484 FILE *pwf
= fopen(pwfilename
, "r");
1485 char pwdbuf
[MAXPGPATH
];
1490 fprintf(stderr
, _("%s: could not open file \"%s\" for reading: %s\n"),
1491 progname
, pwfilename
, strerror(errno
));
1494 if (!fgets(pwdbuf
, sizeof(pwdbuf
), pwf
))
1496 fprintf(stderr
, _("%s: could not read password from file \"%s\": %s\n"),
1497 progname
, pwfilename
, strerror(errno
));
1503 while (i
> 0 && (pwdbuf
[i
- 1] == '\r' || pwdbuf
[i
- 1] == '\n'))
1506 pwd1
= xstrdup(pwdbuf
);
1509 printf(_("setting password ... "));
1512 snprintf(cmd
, sizeof(cmd
),
1513 "\"%s\" %s template1 >%s",
1514 backend_exec
, backend_options
,
1519 PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n",
1520 username
, escape_quotes(pwd1
));
1522 /* MM: pwd1 is no longer needed, freeing it */
1529 snprintf(pwdpath
, sizeof(pwdpath
), "%s/global/pg_auth", pg_data
);
1530 if (stat(pwdpath
, &statbuf
) != 0 || !S_ISREG(statbuf
.st_mode
))
1533 _("%s: The password file was not generated. "
1534 "Please report this problem.\n"),
1548 static const char *pg_depend_setup
[] = {
1550 * Make PIN entries in pg_depend for all objects made so far in the
1551 * tables that the dependency code handles. This is overkill (the
1552 * system doesn't really depend on having every last weird datatype,
1553 * for instance) but generating only the minimum required set of
1554 * dependencies seems hard.
1556 * Note that we deliberately do not pin the system views, which
1557 * haven't been created yet. Also, no conversions, databases, or
1558 * tablespaces are pinned.
1560 * First delete any already-made entries; PINs override all else, and
1561 * must be the only entries for their objects.
1563 "DELETE FROM pg_depend;\n",
1564 "VACUUM pg_depend;\n",
1565 "DELETE FROM pg_shdepend;\n",
1566 "VACUUM pg_shdepend;\n",
1568 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1569 " FROM pg_class;\n",
1570 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1572 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1574 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1576 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1577 " FROM pg_constraint;\n",
1578 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1579 " FROM pg_attrdef;\n",
1580 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1581 " FROM pg_language;\n",
1582 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1583 " FROM pg_operator;\n",
1584 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1585 " FROM pg_opclass;\n",
1586 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1587 " FROM pg_opfamily;\n",
1588 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1590 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1591 " FROM pg_amproc;\n",
1592 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1593 " FROM pg_rewrite;\n",
1594 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1595 " FROM pg_trigger;\n",
1598 * restriction here to avoid pinning the public namespace
1600 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1601 " FROM pg_namespace "
1602 " WHERE nspname LIKE 'pg%';\n",
1604 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1605 " FROM pg_ts_parser;\n",
1606 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1607 " FROM pg_ts_dict;\n",
1608 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1609 " FROM pg_ts_template;\n",
1610 "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1611 " FROM pg_ts_config;\n",
1612 "INSERT INTO pg_shdepend SELECT 0,0,0,0, tableoid,oid, 'p' "
1613 " FROM pg_authid;\n",
1617 fputs(_("initializing dependencies ... "), stdout
);
1620 snprintf(cmd
, sizeof(cmd
),
1621 "\"%s\" %s template1 >%s",
1622 backend_exec
, backend_options
,
1627 for (line
= pg_depend_setup
; *line
!= NULL
; line
++)
1636 * set up system views
1639 setup_sysviews(void)
1643 char **sysviews_setup
;
1645 fputs(_("creating system views ... "), stdout
);
1648 sysviews_setup
= readfile(system_views_file
);
1651 * We use -j here to avoid backslashing stuff in system_views.sql
1653 snprintf(cmd
, sizeof(cmd
),
1654 "\"%s\" %s -j template1 >%s",
1655 backend_exec
, backend_options
,
1660 for (line
= sysviews_setup
; *line
!= NULL
; line
++)
1668 free(sysviews_setup
);
1674 * load description data
1677 setup_description(void)
1681 fputs(_("loading system objects' descriptions ... "), stdout
);
1684 snprintf(cmd
, sizeof(cmd
),
1685 "\"%s\" %s template1 >%s",
1686 backend_exec
, backend_options
,
1691 PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_description ( "
1695 " description text) WITHOUT OIDS;\n");
1697 PG_CMD_PRINTF1("COPY tmp_pg_description FROM E'%s';\n",
1698 escape_quotes(desc_file
));
1700 PG_CMD_PUTS("INSERT INTO pg_description "
1701 " SELECT t.objoid, c.oid, t.objsubid, t.description "
1702 " FROM tmp_pg_description t, pg_class c "
1703 " WHERE c.relname = t.classname;\n");
1705 PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_shdescription ( "
1708 " description text) WITHOUT OIDS;\n");
1710 PG_CMD_PRINTF1("COPY tmp_pg_shdescription FROM E'%s';\n",
1711 escape_quotes(shdesc_file
));
1713 PG_CMD_PUTS("INSERT INTO pg_shdescription "
1714 " SELECT t.objoid, c.oid, t.description "
1715 " FROM tmp_pg_shdescription t, pg_class c "
1716 " WHERE c.relname = t.classname;\n");
1724 * load conversion functions
1727 setup_conversion(void)
1733 fputs(_("creating conversions ... "), stdout
);
1736 snprintf(cmd
, sizeof(cmd
),
1737 "\"%s\" %s template1 >%s",
1738 backend_exec
, backend_options
,
1743 conv_lines
= readfile(conversion_file
);
1744 for (line
= conv_lines
; *line
!= NULL
; line
++)
1746 if (strstr(*line
, "DROP CONVERSION") != *line
)
1759 * load extra dictionaries (Snowball stemmers)
1762 setup_dictionary(void)
1768 fputs(_("creating dictionaries ... "), stdout
);
1772 * We use -j here to avoid backslashing stuff
1774 snprintf(cmd
, sizeof(cmd
),
1775 "\"%s\" %s -j template1 >%s",
1776 backend_exec
, backend_options
,
1781 conv_lines
= readfile(dictionary_file
);
1782 for (line
= conv_lines
; *line
!= NULL
; line
++)
1798 * We mark most system catalogs as world-readable. We don't currently have
1799 * to touch functions, languages, or databases, because their default
1800 * permissions are OK.
1802 * Some objects may require different permissions by default, so we
1803 * make sure we don't overwrite privilege sets that have already been
1807 setup_privileges(void)
1812 static char *privileges_setup
[] = {
1814 " SET relacl = E'{\"=r/\\\\\"$POSTGRES_SUPERUSERNAME\\\\\"\"}' "
1815 " WHERE relkind IN ('r', 'v', 'S') AND relacl IS NULL;\n",
1816 "GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n",
1817 "GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n",
1821 fputs(_("setting privileges on built-in objects ... "), stdout
);
1824 snprintf(cmd
, sizeof(cmd
),
1825 "\"%s\" %s template1 >%s",
1826 backend_exec
, backend_options
,
1831 priv_lines
= replace_token(privileges_setup
,
1832 "$POSTGRES_SUPERUSERNAME", username
);
1833 for (line
= priv_lines
; *line
!= NULL
; line
++)
1842 * extract the strange version of version required for information schema
1846 set_info_version(void)
1848 char *letterversion
;
1853 char *vstr
= xstrdup(PG_VERSION
);
1856 ptr
= vstr
+ (strlen(vstr
) - 1);
1857 while (ptr
!= vstr
&& (*ptr
< '0' || *ptr
> '9'))
1859 letterversion
= ptr
+ 1;
1860 major
= strtol(vstr
, &endptr
, 10);
1862 minor
= strtol(endptr
+ 1, &endptr
, 10);
1864 micro
= strtol(endptr
+ 1, &endptr
, 10);
1865 snprintf(infoversion
, sizeof(infoversion
), "%02ld.%02ld.%04ld%s",
1866 major
, minor
, micro
, letterversion
);
1870 * load info schema and populate from features file
1879 fputs(_("creating information schema ... "), stdout
);
1882 lines
= readfile(info_schema_file
);
1885 * We use -j here to avoid backslashing stuff in information_schema.sql
1887 snprintf(cmd
, sizeof(cmd
),
1888 "\"%s\" %s -j template1 >%s",
1889 backend_exec
, backend_options
,
1894 for (line
= lines
; *line
!= NULL
; line
++)
1904 snprintf(cmd
, sizeof(cmd
),
1905 "\"%s\" %s template1 >%s",
1906 backend_exec
, backend_options
,
1911 PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "
1912 " SET character_value = '%s' "
1913 " WHERE implementation_info_name = 'DBMS VERSION';\n",
1916 PG_CMD_PRINTF1("COPY information_schema.sql_features "
1917 " (feature_id, feature_name, sub_feature_id, "
1918 " sub_feature_name, is_supported, comments) "
1920 escape_quotes(features_file
));
1928 * clean everything up in template1
1935 fputs(_("vacuuming database template1 ... "), stdout
);
1938 snprintf(cmd
, sizeof(cmd
),
1939 "\"%s\" %s template1 >%s",
1940 backend_exec
, backend_options
,
1945 PG_CMD_PUTS("ANALYZE;\nVACUUM FULL;\nVACUUM FREEZE;\n");
1953 * copy template1 to template0
1956 make_template0(void)
1960 static const char *template0_setup
[] = {
1961 "CREATE DATABASE template0;\n",
1962 "UPDATE pg_database SET "
1963 " datistemplate = 't', "
1964 " datallowconn = 'f' "
1965 " WHERE datname = 'template0';\n",
1968 * We use the OID of template0 to determine lastsysoid
1970 "UPDATE pg_database SET datlastsysoid = "
1971 " (SELECT oid FROM pg_database "
1972 " WHERE datname = 'template0');\n",
1975 * Explicitly revoke public create-schema and create-temp-table
1976 * privileges in template1 and template0; else the latter would be on
1979 "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n",
1980 "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n",
1983 * Finally vacuum to clean up dead rows in pg_database
1985 "VACUUM FULL pg_database;\n",
1989 fputs(_("copying template1 to template0 ... "), stdout
);
1992 snprintf(cmd
, sizeof(cmd
),
1993 "\"%s\" %s template1 >%s",
1994 backend_exec
, backend_options
,
1999 for (line
= template0_setup
; *line
; line
++)
2008 * copy template1 to postgres
2015 static const char *postgres_setup
[] = {
2016 "CREATE DATABASE postgres;\n",
2020 fputs(_("copying template1 to postgres ... "), stdout
);
2023 snprintf(cmd
, sizeof(cmd
),
2024 "\"%s\" %s template1 >%s",
2025 backend_exec
, backend_options
,
2030 for (line
= postgres_setup
; *line
; line
++)
2040 * signal handler in case we are interrupted.
2042 * The Windows runtime docs at
2043 * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_signal.asp
2044 * specifically forbid a number of things being done from a signal handler,
2045 * including IO, memory allocation and system calls, and only allow jmpbuf
2046 * if you are handling SIGFPE.
2048 * I avoided doing the forbidden things by setting a flag instead of calling
2049 * exit_nicely() directly.
2051 * Also note the behaviour of Windows with SIGINT, which says this:
2052 * Note SIGINT is not supported for any Win32 application, including
2053 * Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs,
2054 * Win32 operating systems generate a new thread to specifically handle
2055 * that interrupt. This can cause a single-thread application such as UNIX,
2056 * to become multithreaded, resulting in unexpected behavior.
2058 * I have no idea how to handle this. (Strange they call UNIX an application!)
2059 * So this will need some testing on Windows.
2064 /* handle systems that reset the handler, like Windows (grr) */
2065 pqsignal(signum
, trapsig
);
2066 caught_signal
= true;
2070 * call exit_nicely() if we got a signal, or else output "ok".
2077 printf(_("caught signal\n"));
2081 else if (output_failed
)
2083 printf(_("could not write to child process: %s\n"),
2084 strerror(output_errno
));
2090 /* all seems well */
2097 * Escape (by doubling) any single quotes or backslashes in given string
2099 * Note: this is used to process both postgresql.conf entries and SQL
2100 * string literals. Since postgresql.conf strings are defined to treat
2101 * backslashes as escapes, we have to double backslashes here. Hence,
2102 * when using this for a SQL string literal, use E'' syntax.
2104 * We do not need to worry about encoding considerations because all
2105 * valid backend encodings are ASCII-safe.
2108 escape_quotes(const char *src
)
2110 int len
= strlen(src
),
2113 char *result
= pg_malloc(len
* 2 + 1);
2115 for (i
= 0, j
= 0; i
< len
; i
++)
2117 if (SQL_STR_DOUBLE(src
[i
], true))
2118 result
[j
++] = src
[i
];
2119 result
[j
++] = src
[i
];
2125 /* Hack to suppress a warning about %x from some versions of gcc */
2126 static inline size_t
2127 my_strftime(char *s
, size_t max
, const char *fmt
, const struct tm
* tm
)
2129 return strftime(s
, max
, fmt
, tm
);
2133 * Determine likely date order from locale
2136 locale_date_order(const char *locale
)
2147 result
= DATEORDER_MDY
; /* default */
2149 save
= setlocale(LC_TIME
, NULL
);
2152 save
= xstrdup(save
);
2154 setlocale(LC_TIME
, locale
);
2156 memset(&testtime
, 0, sizeof(testtime
));
2157 testtime
.tm_mday
= 22;
2158 testtime
.tm_mon
= 10; /* November, should come out as "11" */
2159 testtime
.tm_year
= 133; /* 2033 */
2161 res
= my_strftime(buf
, sizeof(buf
), "%x", &testtime
);
2163 setlocale(LC_TIME
, save
);
2169 posM
= strstr(buf
, "11");
2170 posD
= strstr(buf
, "22");
2171 posY
= strstr(buf
, "33");
2173 if (!posM
|| !posD
|| !posY
)
2176 if (posY
< posM
&& posM
< posD
)
2177 result
= DATEORDER_YMD
;
2178 else if (posD
< posM
)
2179 result
= DATEORDER_DMY
;
2181 result
= DATEORDER_MDY
;
2187 * check if given string is a valid locale specifier
2189 * this should match the backend check_locale() function
2192 check_locale_name(const char *locale
)
2195 int category
= LC_CTYPE
;
2198 save
= setlocale(category
, NULL
);
2200 return false; /* should not happen; */
2202 save
= xstrdup(save
);
2204 ret
= (setlocale(category
, locale
) != NULL
);
2206 setlocale(category
, save
);
2209 /* should we exit here? */
2211 fprintf(stderr
, _("%s: invalid locale name \"%s\"\n"), progname
, locale
);
2217 * check if the chosen encoding matches the encoding required by the locale
2219 * this should match the similar check in the backend createdb() function
2222 check_locale_encoding(const char *locale
, int user_enc
)
2226 locale_enc
= pg_get_encoding_from_locale(locale
);
2228 /* We allow selection of SQL_ASCII --- see notes in createdb() */
2229 if (!(locale_enc
== user_enc
||
2230 locale_enc
== PG_SQL_ASCII
||
2231 user_enc
== PG_SQL_ASCII
2235 * On win32, if the encoding chosen is UTF8, all locales are OK (assuming
2236 * the actual locale name passed the checks above). This is because UTF8
2237 * is a pseudo-codepage, that we convert to UTF16 before doing any
2238 * operations on, and UTF16 supports all locales.
2240 || user_enc
== PG_UTF8
2244 fprintf(stderr
, _("%s: encoding mismatch\n"), progname
);
2246 _("The encoding you selected (%s) and the encoding that the\n"
2247 "selected locale uses (%s) do not match. This would lead to\n"
2248 "misbehavior in various character string processing functions.\n"
2249 "Rerun %s and either do not specify an encoding explicitly,\n"
2250 "or choose a matching combination.\n"),
2251 pg_encoding_to_char(user_enc
),
2252 pg_encoding_to_char(locale_enc
),
2261 * set up the locale variables
2263 * assumes we have called setlocale(LC_ALL,"")
2268 /* set empty lc_* values to locale config if set */
2270 if (strlen(locale
) > 0)
2272 if (strlen(lc_ctype
) == 0)
2274 if (strlen(lc_collate
) == 0)
2275 lc_collate
= locale
;
2276 if (strlen(lc_numeric
) == 0)
2277 lc_numeric
= locale
;
2278 if (strlen(lc_time
) == 0)
2280 if (strlen(lc_monetary
) == 0)
2281 lc_monetary
= locale
;
2282 if (strlen(lc_messages
) == 0)
2283 lc_messages
= locale
;
2287 * override absent/invalid config settings from initdb's locale settings
2290 if (strlen(lc_ctype
) == 0 || !check_locale_name(lc_ctype
))
2291 lc_ctype
= xstrdup(setlocale(LC_CTYPE
, NULL
));
2292 if (strlen(lc_collate
) == 0 || !check_locale_name(lc_collate
))
2293 lc_collate
= xstrdup(setlocale(LC_COLLATE
, NULL
));
2294 if (strlen(lc_numeric
) == 0 || !check_locale_name(lc_numeric
))
2295 lc_numeric
= xstrdup(setlocale(LC_NUMERIC
, NULL
));
2296 if (strlen(lc_time
) == 0 || !check_locale_name(lc_time
))
2297 lc_time
= xstrdup(setlocale(LC_TIME
, NULL
));
2298 if (strlen(lc_monetary
) == 0 || !check_locale_name(lc_monetary
))
2299 lc_monetary
= xstrdup(setlocale(LC_MONETARY
, NULL
));
2300 if (strlen(lc_messages
) == 0 || !check_locale_name(lc_messages
))
2301 #if defined(LC_MESSAGES) && !defined(WIN32)
2303 /* when available get the current locale setting */
2304 lc_messages
= xstrdup(setlocale(LC_MESSAGES
, NULL
));
2308 /* when not available, get the CTYPE setting */
2309 lc_messages
= xstrdup(setlocale(LC_CTYPE
, NULL
));
2316 typedef BOOL (WINAPI
* __CreateRestrictedToken
) (HANDLE
, DWORD
, DWORD
, PSID_AND_ATTRIBUTES
, DWORD
, PLUID_AND_ATTRIBUTES
, DWORD
, PSID_AND_ATTRIBUTES
, PHANDLE
);
2318 #define DISABLE_MAX_PRIVILEGE 0x1
2321 * Create a restricted token and execute the specified process with it.
2323 * Returns 0 on failure, non-zero on success, same as CreateProcess().
2325 * On NT4, or any other system not containing the required functions, will
2326 * NOT execute anything.
2329 CreateRestrictedProcess(char *cmd
, PROCESS_INFORMATION
*processInfo
)
2334 HANDLE restrictedToken
;
2335 SID_IDENTIFIER_AUTHORITY NtAuthority
= {SECURITY_NT_AUTHORITY
};
2336 SID_AND_ATTRIBUTES dropSids
[2];
2337 __CreateRestrictedToken _CreateRestrictedToken
= NULL
;
2338 HANDLE Advapi32Handle
;
2340 ZeroMemory(&si
, sizeof(si
));
2343 Advapi32Handle
= LoadLibrary("ADVAPI32.DLL");
2344 if (Advapi32Handle
!= NULL
)
2346 _CreateRestrictedToken
= (__CreateRestrictedToken
) GetProcAddress(Advapi32Handle
, "CreateRestrictedToken");
2349 if (_CreateRestrictedToken
== NULL
)
2351 fprintf(stderr
, "WARNING: cannot create restricted tokens on this platform\n");
2352 if (Advapi32Handle
!= NULL
)
2353 FreeLibrary(Advapi32Handle
);
2357 /* Open the current token to use as a base for the restricted one */
2358 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &origToken
))
2360 fprintf(stderr
, "Failed to open process token: %lu\n", GetLastError());
2364 /* Allocate list of SIDs to remove */
2365 ZeroMemory(&dropSids
, sizeof(dropSids
));
2366 if (!AllocateAndInitializeSid(&NtAuthority
, 2,
2367 SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_ADMINS
, 0, 0, 0, 0, 0,
2368 0, &dropSids
[0].Sid
) ||
2369 !AllocateAndInitializeSid(&NtAuthority
, 2,
2370 SECURITY_BUILTIN_DOMAIN_RID
, DOMAIN_ALIAS_RID_POWER_USERS
, 0, 0, 0, 0, 0,
2371 0, &dropSids
[1].Sid
))
2373 fprintf(stderr
, "Failed to allocate SIDs: %lu\n", GetLastError());
2377 b
= _CreateRestrictedToken(origToken
,
2378 DISABLE_MAX_PRIVILEGE
,
2379 sizeof(dropSids
) / sizeof(dropSids
[0]),
2385 FreeSid(dropSids
[1].Sid
);
2386 FreeSid(dropSids
[0].Sid
);
2387 CloseHandle(origToken
);
2388 FreeLibrary(Advapi32Handle
);
2392 fprintf(stderr
, "Failed to create restricted token: %lu\n", GetLastError());
2396 if (!CreateProcessAsUser(restrictedToken
,
2409 fprintf(stderr
, "CreateProcessAsUser failed: %lu\n", GetLastError());
2414 AddUserToDacl(processInfo
->hProcess
);
2417 return ResumeThread(processInfo
->hThread
);
2425 usage(const char *progname
)
2427 printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname
);
2428 printf(_("Usage:\n"));
2429 printf(_(" %s [OPTION]... [DATADIR]\n"), progname
);
2430 printf(_("\nOptions:\n"));
2431 printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2432 printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2433 printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2434 printf(_(" --locale=LOCALE set default locale for new databases\n"));
2435 printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2436 " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2437 " set default locale in the respective category for\n"
2438 " new databases (default taken from environment)\n"));
2439 printf(_(" --no-locale equivalent to --locale=C\n"));
2440 printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2441 printf(_(" -T, --text-search-config=CFG\n"
2442 " default text search configuration\n"));
2443 printf(_(" -U, --username=NAME database superuser name\n"));
2444 printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2445 printf(_(" -X, --xlogdir=XLOGDIR location for the transaction log directory\n"));
2446 printf(_("\nLess commonly used options:\n"));
2447 printf(_(" -d, --debug generate lots of debugging output\n"));
2448 printf(_(" -L DIRECTORY where to find the input files\n"));
2449 printf(_(" -n, --noclean do not clean up after errors\n"));
2450 printf(_(" -s, --show show internal settings\n"));
2451 printf(_("\nOther options:\n"));
2452 printf(_(" -?, --help show this help, then exit\n"));
2453 printf(_(" -V, --version output version information, then exit\n"));
2454 printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2456 printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
2460 main(int argc
, char *argv
[])
2463 * options with no short version return a low integer, the rest return
2464 * their short version value
2466 static struct option long_options
[] = {
2467 {"pgdata", required_argument
, NULL
, 'D'},
2468 {"encoding", required_argument
, NULL
, 'E'},
2469 {"locale", required_argument
, NULL
, 1},
2470 {"lc-collate", required_argument
, NULL
, 2},
2471 {"lc-ctype", required_argument
, NULL
, 3},
2472 {"lc-monetary", required_argument
, NULL
, 4},
2473 {"lc-numeric", required_argument
, NULL
, 5},
2474 {"lc-time", required_argument
, NULL
, 6},
2475 {"lc-messages", required_argument
, NULL
, 7},
2476 {"no-locale", no_argument
, NULL
, 8},
2477 {"text-search-config", required_argument
, NULL
, 'T'},
2478 {"auth", required_argument
, NULL
, 'A'},
2479 {"pwprompt", no_argument
, NULL
, 'W'},
2480 {"pwfile", required_argument
, NULL
, 9},
2481 {"username", required_argument
, NULL
, 'U'},
2482 {"help", no_argument
, NULL
, '?'},
2483 {"version", no_argument
, NULL
, 'V'},
2484 {"debug", no_argument
, NULL
, 'd'},
2485 {"show", no_argument
, NULL
, 's'},
2486 {"noclean", no_argument
, NULL
, 'n'},
2487 {"xlogdir", required_argument
, NULL
, 'X'},
2495 char *short_version
;
2496 char *effective_user
;
2497 char *pgdenv
; /* PGDATA value gotten from and sent to
2499 char bin_dir
[MAXPGPATH
];
2500 char *pg_data_native
;
2506 static const char *subdirs
[] = {
2509 "pg_xlog/archive_status",
2513 "pg_multixact/members",
2514 "pg_multixact/offsets",
2521 progname
= get_progname(argv
[0]);
2522 set_pglocale_pgservice(argv
[0], PG_TEXTDOMAIN("initdb"));
2526 if (strcmp(argv
[1], "--help") == 0 || strcmp(argv
[1], "-?") == 0)
2531 if (strcmp(argv
[1], "--version") == 0 || strcmp(argv
[1], "-V") == 0)
2533 puts("initdb (PostgreSQL) " PG_VERSION
);
2538 /* process command-line options */
2540 while ((c
= getopt_long(argc
, argv
, "dD:E:L:nU:WA:sT:X:", long_options
, &option_index
)) != -1)
2545 authmethod
= xstrdup(optarg
);
2548 pg_data
= xstrdup(optarg
);
2551 encoding
= xstrdup(optarg
);
2557 username
= xstrdup(optarg
);
2561 printf(_("Running in debug mode.\n"));
2565 printf(_("Running in noclean mode. Mistakes will not be cleaned up.\n"));
2568 share_path
= xstrdup(optarg
);
2571 locale
= xstrdup(optarg
);
2574 lc_collate
= xstrdup(optarg
);
2577 lc_ctype
= xstrdup(optarg
);
2580 lc_monetary
= xstrdup(optarg
);
2583 lc_numeric
= xstrdup(optarg
);
2586 lc_time
= xstrdup(optarg
);
2589 lc_messages
= xstrdup(optarg
);
2595 pwfilename
= xstrdup(optarg
);
2598 show_setting
= true;
2601 default_text_search_config
= xstrdup(optarg
);
2604 xlog_dir
= xstrdup(optarg
);
2607 /* getopt_long already emitted a complaint */
2608 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"),
2615 /* Non-option argument specifies data directory */
2618 pg_data
= xstrdup(argv
[optind
]);
2624 fprintf(stderr
, _("%s: too many command-line arguments (first is \"%s\")\n"),
2625 progname
, argv
[optind
+ 1]);
2626 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"),
2631 if (pwprompt
&& pwfilename
)
2633 fprintf(stderr
, _("%s: password prompt and password file cannot be specified together\n"), progname
);
2637 if (authmethod
== NULL
|| !strlen(authmethod
))
2639 authwarning
= _("\nWARNING: enabling \"trust\" authentication for local connections\n"
2640 "You can change this by editing pg_hba.conf or using the -A option the\n"
2641 "next time you run initdb.\n");
2642 authmethod
= "trust";
2645 if (strcmp(authmethod
, "md5") &&
2646 strcmp(authmethod
, "ident") &&
2647 strncmp(authmethod
, "ident ", 6) && /* ident with space = param */
2648 strcmp(authmethod
, "trust") &&
2650 strcmp(authmethod
, "pam") &&
2651 strncmp(authmethod
, "pam ", 4) && /* pam with space = param */
2653 strcmp(authmethod
, "crypt") &&
2654 strcmp(authmethod
, "password")
2658 * Kerberos methods not listed because they are not supported over
2659 * local connections and are rejected in hba.c
2662 fprintf(stderr
, _("%s: unrecognized authentication method \"%s\"\n"),
2663 progname
, authmethod
);
2667 if ((!strcmp(authmethod
, "md5") ||
2668 !strcmp(authmethod
, "crypt") ||
2669 !strcmp(authmethod
, "password")) &&
2670 !(pwprompt
|| pwfilename
))
2672 fprintf(stderr
, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname
, authmethod
);
2676 if (strlen(pg_data
) == 0)
2678 pgdenv
= getenv("PGDATA");
2679 if (pgdenv
&& strlen(pgdenv
))
2682 pg_data
= xstrdup(pgdenv
);
2687 _("%s: no data directory specified\n"
2688 "You must identify the directory where the data for this database system\n"
2689 "will reside. Do this with either the invocation option -D or the\n"
2690 "environment variable PGDATA.\n"),
2696 pg_data_native
= pg_data
;
2697 canonicalize_path(pg_data
);
2702 * Before we execute another program, make sure that we are running with a
2703 * restricted token. If not, re-execute ourselves with one.
2706 if ((restrict_env
= getenv("PG_RESTRICT_EXEC")) == NULL
2707 || strcmp(restrict_env
, "1") != 0)
2709 PROCESS_INFORMATION pi
;
2712 ZeroMemory(&pi
, sizeof(pi
));
2714 cmdline
= xstrdup(GetCommandLine());
2716 putenv("PG_RESTRICT_EXEC=1");
2718 if (!CreateRestrictedProcess(cmdline
, &pi
))
2720 fprintf(stderr
, "Failed to re-exec with restricted token: %lu.\n", GetLastError());
2725 * Successfully re-execed. Now wait for child process to capture
2730 CloseHandle(pi
.hThread
);
2731 WaitForSingleObject(pi
.hProcess
, INFINITE
);
2733 if (!GetExitCodeProcess(pi
.hProcess
, &x
))
2735 fprintf(stderr
, "Failed to get exit code from subprocess: %lu\n", GetLastError());
2744 * we have to set PGDATA for postgres rather than pass it on the command
2745 * line to avoid dumb quoting problems on Windows, and we would especially
2746 * need quotes otherwise on Windows because paths there are most likely to
2747 * have embedded spaces.
2749 pgdenv
= pg_malloc(8 + strlen(pg_data
));
2750 sprintf(pgdenv
, "PGDATA=%s", pg_data
);
2753 if ((ret
= find_other_exec(argv
[0], "postgres", PG_BACKEND_VERSIONSTR
,
2756 char full_path
[MAXPGPATH
];
2758 if (find_my_exec(argv
[0], full_path
) < 0)
2759 strlcpy(full_path
, progname
, sizeof(full_path
));
2763 _("The program \"postgres\" is needed by %s "
2764 "but was not found in the\n"
2765 "same directory as \"%s\".\n"
2766 "Check your installation.\n"),
2767 progname
, full_path
);
2770 _("The program \"postgres\" was found by \"%s\"\n"
2771 "but was not the same version as %s.\n"
2772 "Check your installation.\n"),
2773 full_path
, progname
);
2777 /* store binary directory */
2778 strcpy(bin_path
, backend_exec
);
2779 *last_dir_separator(bin_path
) = '\0';
2780 canonicalize_path(bin_path
);
2784 share_path
= pg_malloc(MAXPGPATH
);
2785 get_share_path(backend_exec
, share_path
);
2787 else if (!is_absolute_path(share_path
))
2789 fprintf(stderr
, _("%s: input file location must be an absolute path\n"), progname
);
2793 canonicalize_path(share_path
);
2795 if ((short_version
= get_short_version()) == NULL
)
2797 fprintf(stderr
, _("%s: could not determine valid short version string\n"), progname
);
2801 effective_user
= get_id();
2802 if (strlen(username
) == 0)
2803 username
= effective_user
;
2805 set_input(&bki_file
, "postgres.bki");
2806 set_input(&desc_file
, "postgres.description");
2807 set_input(&shdesc_file
, "postgres.shdescription");
2808 set_input(&hba_file
, "pg_hba.conf.sample");
2809 set_input(&ident_file
, "pg_ident.conf.sample");
2810 set_input(&conf_file
, "postgresql.conf.sample");
2811 set_input(&conversion_file
, "conversion_create.sql");
2812 set_input(&dictionary_file
, "snowball_create.sql");
2813 set_input(&info_schema_file
, "information_schema.sql");
2814 set_input(&features_file
, "sql_features.txt");
2815 set_input(&system_views_file
, "system_views.sql");
2819 if (show_setting
|| debug
)
2823 "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2824 "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2825 "POSTGRES_DESCR=%s\nPOSTGRES_SHDESCR=%s\n"
2826 "POSTGRESQL_CONF_SAMPLE=%s\n"
2827 "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2829 pg_data
, share_path
, bin_path
,
2831 desc_file
, shdesc_file
,
2833 hba_file
, ident_file
);
2838 check_input(bki_file
);
2839 check_input(desc_file
);
2840 check_input(shdesc_file
);
2841 check_input(hba_file
);
2842 check_input(ident_file
);
2843 check_input(conf_file
);
2844 check_input(conversion_file
);
2845 check_input(dictionary_file
);
2846 check_input(info_schema_file
);
2847 check_input(features_file
);
2848 check_input(system_views_file
);
2852 printf(_("The files belonging to this database system will be owned "
2854 "This user must also own the server process.\n\n"),
2857 if (strcmp(lc_ctype
, lc_collate
) == 0 &&
2858 strcmp(lc_ctype
, lc_time
) == 0 &&
2859 strcmp(lc_ctype
, lc_numeric
) == 0 &&
2860 strcmp(lc_ctype
, lc_monetary
) == 0 &&
2861 strcmp(lc_ctype
, lc_messages
) == 0)
2862 printf(_("The database cluster will be initialized with locale %s.\n"), lc_ctype
);
2865 printf(_("The database cluster will be initialized with locales\n"
2880 if (strlen(encoding
) == 0)
2884 ctype_enc
= pg_get_encoding_from_locale(lc_ctype
);
2886 if (ctype_enc
== PG_SQL_ASCII
&&
2887 !(pg_strcasecmp(lc_ctype
, "C") == 0 ||
2888 pg_strcasecmp(lc_ctype
, "POSIX") == 0))
2890 /* Hmm, couldn't recognize the locale's codeset */
2891 fprintf(stderr
, _("%s: could not find suitable encoding for locale %s\n"),
2892 progname
, lc_ctype
);
2893 fprintf(stderr
, _("Rerun %s with the -E option.\n"), progname
);
2894 fprintf(stderr
, _("Try \"%s --help\" for more information.\n"),
2898 else if (!pg_valid_server_encoding_id(ctype_enc
))
2900 /* We recognized it, but it's not a legal server encoding */
2902 _("%s: locale %s requires unsupported encoding %s\n"),
2903 progname
, lc_ctype
, pg_encoding_to_char(ctype_enc
));
2905 _("Encoding %s is not allowed as a server-side encoding.\n"
2906 "Rerun %s with a different locale selection.\n"),
2907 pg_encoding_to_char(ctype_enc
), progname
);
2912 encodingid
= encodingid_to_string(ctype_enc
);
2913 printf(_("The default database encoding has accordingly been set to %s.\n"),
2914 pg_encoding_to_char(ctype_enc
));
2918 encodingid
= get_encoding_id(encoding
);
2920 user_enc
= atoi(encodingid
);
2921 if (!check_locale_encoding(lc_ctype
, user_enc
) ||
2922 !check_locale_encoding(lc_collate
, user_enc
))
2923 exit(1); /* check_locale_encoding printed the error */
2925 if (strlen(default_text_search_config
) == 0)
2927 default_text_search_config
= find_matching_ts_config(lc_ctype
);
2928 if (default_text_search_config
== NULL
)
2930 printf(_("%s: could not find suitable text search configuration for locale %s\n"),
2931 progname
, lc_ctype
);
2932 default_text_search_config
= "simple";
2937 const char *checkmatch
= find_matching_ts_config(lc_ctype
);
2939 if (checkmatch
== NULL
)
2941 printf(_("%s: warning: suitable text search configuration for locale %s is unknown\n"),
2942 progname
, lc_ctype
);
2944 else if (strcmp(checkmatch
, default_text_search_config
) != 0)
2946 printf(_("%s: warning: specified text search configuration \"%s\" might not match locale %s\n"),
2947 progname
, default_text_search_config
, lc_ctype
);
2951 printf(_("The default text search configuration will be set to \"%s\".\n"),
2952 default_text_search_config
);
2959 * now we are starting to do real work, trap signals so we can clean up
2962 /* some of these are not valid on Windows */
2964 pqsignal(SIGHUP
, trapsig
);
2967 pqsignal(SIGINT
, trapsig
);
2970 pqsignal(SIGQUIT
, trapsig
);
2973 pqsignal(SIGTERM
, trapsig
);
2976 /* Ignore SIGPIPE when writing to backend, so we can clean up */
2978 pqsignal(SIGPIPE
, SIG_IGN
);
2981 switch (check_data_dir(pg_data
))
2984 /* PGDATA not there, must create it */
2985 printf(_("creating directory %s ... "),
2989 if (!mkdatadir(NULL
))
2994 made_new_pgdata
= true;
2998 /* Present but empty, fix permissions and use it */
2999 printf(_("fixing permissions on existing directory %s ... "),
3003 if (chmod(pg_data
, 0700) != 0)
3005 fprintf(stderr
, _("%s: could not change permissions of directory \"%s\": %s\n"),
3006 progname
, pg_data
, strerror(errno
));
3012 found_existing_pgdata
= true;
3016 /* Present and not empty */
3018 _("%s: directory \"%s\" exists but is not empty\n"),
3021 _("If you want to create a new database system, either remove or empty\n"
3022 "the directory \"%s\" or run %s\n"
3023 "with an argument other than \"%s\".\n"),
3024 pg_data
, progname
, pg_data
);
3025 exit(1); /* no further message needed */
3028 /* Trouble accessing directory */
3029 fprintf(stderr
, _("%s: could not access directory \"%s\": %s\n"),
3030 progname
, pg_data
, strerror(errno
));
3034 /* Create transaction log symlink, if required */
3035 if (strcmp(xlog_dir
, "") != 0)
3039 /* clean up xlog directory name, check it's absolute */
3040 canonicalize_path(xlog_dir
);
3041 if (!is_absolute_path(xlog_dir
))
3043 fprintf(stderr
, _("%s: transaction log directory location must be an absolute path\n"), progname
);
3047 /* check if the specified xlog directory is empty */
3048 switch (check_data_dir(xlog_dir
))
3051 /* xlog directory not there, must create it */
3052 printf(_("creating directory %s ... "),
3056 if (mkdir_p(xlog_dir
, 0700) != 0)
3058 fprintf(stderr
, _("%s: could not create directory \"%s\": %s\n"),
3059 progname
, xlog_dir
, strerror(errno
));
3065 made_new_xlogdir
= true;
3068 /* Present but empty, fix permissions and use it */
3069 printf(_("fixing permissions on existing directory %s ... "),
3073 if (chmod(xlog_dir
, 0700) != 0)
3075 fprintf(stderr
, _("%s: could not change permissions of directory \"%s\": %s\n"),
3076 progname
, xlog_dir
, strerror(errno
));
3082 found_existing_xlogdir
= true;
3085 /* Present and not empty */
3087 _("%s: directory \"%s\" exists but is not empty\n"),
3088 progname
, xlog_dir
);
3090 _("If you want to store the transaction log there, either\n"
3091 "remove or empty the directory \"%s\".\n"),
3096 /* Trouble accessing directory */
3097 fprintf(stderr
, _("%s: could not access directory \"%s\": %s\n"),
3098 progname
, xlog_dir
, strerror(errno
));
3102 /* form name of the place where the symlink must go */
3103 linkloc
= (char *) pg_malloc(strlen(pg_data
) + 8 + 1);
3104 sprintf(linkloc
, "%s/pg_xlog", pg_data
);
3107 if (symlink(xlog_dir
, linkloc
) != 0)
3109 fprintf(stderr
, _("%s: could not create symbolic link \"%s\": %s\n"),
3110 progname
, linkloc
, strerror(errno
));
3114 fprintf(stderr
, _("%s: symlinks are not supported on this platform"));
3119 /* Create required subdirectories */
3120 printf(_("creating subdirectories ... "));
3123 for (i
= 0; i
< (sizeof(subdirs
) / sizeof(char *)); i
++)
3125 if (!mkdatadir(subdirs
[i
]))
3131 /* Top level PG_VERSION is checked by bootstrapper, so make it first */
3132 set_short_version(short_version
, NULL
);
3134 /* Select suitable configuration settings */
3136 test_config_settings();
3138 /* Now create all the text config files */
3141 /* Bootstrap template1 */
3142 bootstrap_template1(short_version
);
3145 * Make the per-database PG_VERSION for template1 only after init'ing it
3147 set_short_version(short_version
, "base/1");
3149 /* Create the stuff we don't need to use bootstrap mode for */
3152 if (pwprompt
|| pwfilename
)
3159 setup_description();
3175 if (authwarning
!= NULL
)
3176 fprintf(stderr
, "%s", authwarning
);
3178 /* Get directory specification used to start this executable */
3179 strcpy(bin_dir
, argv
[0]);
3180 get_parent_directory(bin_dir
);
3182 printf(_("\nSuccess. You can now start the database server using:\n\n"
3183 " %s%s%spostgres%s -D %s%s%s\n"
3185 " %s%s%spg_ctl%s -D %s%s%s -l logfile start\n\n"),
3186 QUOTE_PATH
, bin_dir
, (strlen(bin_dir
) > 0) ? DIR_SEP
: "", QUOTE_PATH
,
3187 QUOTE_PATH
, pg_data_native
, QUOTE_PATH
,
3188 QUOTE_PATH
, bin_dir
, (strlen(bin_dir
) > 0) ? DIR_SEP
: "", QUOTE_PATH
,
3189 QUOTE_PATH
, pg_data_native
, QUOTE_PATH
);