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