📄 initdb.c
字号:
* their short version value */ static struct option long_options[] = { {"pgdata", required_argument, NULL, 'D'}, {"encoding", required_argument, NULL, 'E'}, {"locale", required_argument, NULL, 1}, {"lc-collate", required_argument, NULL, 2}, {"lc-ctype", required_argument, NULL, 3}, {"lc-monetary", required_argument, NULL, 4}, {"lc-numeric", required_argument, NULL, 5}, {"lc-time", required_argument, NULL, 6}, {"lc-messages", required_argument, NULL, 7}, {"no-locale", no_argument, NULL, 8}, {"auth", required_argument, NULL, 'A'}, {"pwprompt", no_argument, NULL, 'W'}, {"pwfile", required_argument, NULL, 9}, {"username", required_argument, NULL, 'U'}, {"help", no_argument, NULL, '?'}, {"version", no_argument, NULL, 'V'}, {"debug", no_argument, NULL, 'd'}, {"show", no_argument, NULL, 's'}, {"noclean", no_argument, NULL, 'n'}, {NULL, 0, NULL, 0} }; int c, i, ret; int option_index; char *short_version; char *effective_user; char *pgdenv; /* PGDATA value gotten from and sent to * environment */ char bin_dir[MAXPGPATH]; char *pg_data_native; static const char *subdirs[] = { "global", "pg_xlog", "pg_xlog/archive_status", "pg_clog", "pg_subtrans", "pg_twophase", "pg_multixact/members", "pg_multixact/offsets", "base", "base/1", "pg_tblspc" }; progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], "initdb"); if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { usage(progname); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { puts("initdb (PostgreSQL) " PG_VERSION); exit(0); } } /* process command-line options */ while ((c = getopt_long(argc, argv, "dD:E:L:nU:WA:s", long_options, &option_index)) != -1) { switch (c) { case 'A': authmethod = xstrdup(optarg); break; case 'D': pg_data = xstrdup(optarg); break; case 'E': encoding = xstrdup(optarg); break; case 'W': pwprompt = true; break; case 'U': username = xstrdup(optarg); break; case 'd': debug = true; printf(_("Running in debug mode.\n")); break; case 'n': noclean = true; printf(_("Running in noclean mode. Mistakes will not be cleaned up.\n")); break; case 'L': share_path = xstrdup(optarg); break; case 1: locale = xstrdup(optarg); break; case 2: lc_collate = xstrdup(optarg); break; case 3: lc_ctype = xstrdup(optarg); break; case 4: lc_monetary = xstrdup(optarg); break; case 5: lc_numeric = xstrdup(optarg); break; case 6: lc_time = xstrdup(optarg); break; case 7: lc_messages = xstrdup(optarg); break; case 8: locale = "C"; break; case 9: pwfilename = xstrdup(optarg); break; case 's': show_setting = true; break; default: /* getopt_long already emitted a complaint */ fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } } /* Non-option argument specifies data directory */ if (optind < argc) { pg_data = xstrdup(argv[optind]); optind++; } if (optind < argc) { fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind + 1]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); } if (pwprompt && pwfilename) { fprintf(stderr, _("%s: password prompt and password file may not be specified together\n"), progname); exit(1); } if (authmethod == NULL || !strlen(authmethod)) { authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n" "You can change this by editing pg_hba.conf or using the -A option the\n" "next time you run initdb.\n"); authmethod = "trust"; } if (strcmp(authmethod, "md5") && strcmp(authmethod, "ident") && strncmp(authmethod, "ident ", 6) && /* ident with space = param */ strcmp(authmethod, "trust") &&#ifdef USE_PAM strcmp(authmethod, "pam") && strncmp(authmethod, "pam ", 4) && /* pam with space = param */#endif strcmp(authmethod, "crypt") && strcmp(authmethod, "password") ) /* * Kerberos methods not listed because they are not supported over * local connections and are rejected in hba.c */ { fprintf(stderr, _("%s: unrecognized authentication method \"%s\"\n"), progname, authmethod); exit(1); } if ((!strcmp(authmethod, "md5") || !strcmp(authmethod, "crypt") || !strcmp(authmethod, "password")) && !(pwprompt || pwfilename)) { fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname, authmethod); exit(1); } if (strlen(pg_data) == 0) { pgdenv = getenv("PGDATA"); if (pgdenv && strlen(pgdenv)) { /* PGDATA found */ pg_data = xstrdup(pgdenv); } else { fprintf(stderr, _("%s: no data directory specified\n" "You must identify the directory where the data for this database system\n" "will reside. Do this with either the invocation option -D or the\n" "environment variable PGDATA.\n"), progname); exit(1); } } pg_data_native = pg_data; canonicalize_path(pg_data); /* * we have to set PGDATA for postgres rather than pass it on the command * line to avoid dumb quoting problems on Windows, and we would especially * need quotes otherwise on Windows because paths there are most likely to * have embedded spaces. */ pgdenv = pg_malloc(8 + strlen(pg_data)); sprintf(pgdenv, "PGDATA=%s", pg_data); putenv(pgdenv); if ((ret = find_other_exec(argv[0], "postgres", PG_VERSIONSTR, backend_exec)) < 0) { char full_path[MAXPGPATH]; if (find_my_exec(argv[0], full_path) < 0) StrNCpy(full_path, progname, MAXPGPATH); if (ret == -1) fprintf(stderr, _("The program \"postgres\" is needed by %s " "but was not found in the\n" "same directory as \"%s\".\n" "Check your installation.\n"), progname, full_path); else fprintf(stderr, _("The program \"postgres\" was found by \"%s\"\n" "but was not the same version as %s.\n" "Check your installation.\n"), full_path, progname); exit(1); } /* store binary directory */ strcpy(bin_path, backend_exec); *last_dir_separator(bin_path) = '\0'; canonicalize_path(bin_path); if (!share_path) { share_path = pg_malloc(MAXPGPATH); get_share_path(backend_exec, share_path); } else if (!is_absolute_path(share_path)) { fprintf(stderr, _("%s: input file location must be an absolute path\n"), progname); exit(1); } canonicalize_path(share_path); if ((short_version = get_short_version()) == NULL) { fprintf(stderr, _("%s: could not determine valid short version string\n"), progname); exit(1); } effective_user = get_id(); if (strlen(username) == 0) username = effective_user; if (strlen(encoding)) encodingid = get_encoding_id(encoding); set_input(&bki_file, "postgres.bki"); set_input(&desc_file, "postgres.description"); set_input(&hba_file, "pg_hba.conf.sample"); set_input(&ident_file, "pg_ident.conf.sample"); set_input(&conf_file, "postgresql.conf.sample"); set_input(&conversion_file, "conversion_create.sql"); set_input(&info_schema_file, "information_schema.sql"); set_input(&features_file, "sql_features.txt"); set_input(&system_views_file, "system_views.sql"); set_info_version(); if (show_setting || debug) { fprintf(stderr, "VERSION=%s\n" "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n" "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n" "POSTGRES_DESCR=%s\nPOSTGRESQL_CONF_SAMPLE=%s\n" "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n", PG_VERSION, pg_data, share_path, bin_path, username, bki_file, desc_file, conf_file, hba_file, ident_file); if (show_setting) exit(0); } check_input(bki_file); check_input(desc_file); check_input(hba_file); check_input(ident_file); check_input(conf_file); check_input(conversion_file); check_input(info_schema_file); check_input(features_file); check_input(system_views_file); setlocales(); printf(_("The files belonging to this database system will be owned " "by user \"%s\".\n" "This user must also own the server process.\n\n"), effective_user); if (strcmp(lc_ctype, lc_collate) == 0 && strcmp(lc_ctype, lc_time) == 0 && strcmp(lc_ctype, lc_numeric) == 0 && strcmp(lc_ctype, lc_monetary) == 0 && strcmp(lc_ctype, lc_messages) == 0) printf(_("The database cluster will be initialized with locale %s.\n"), lc_ctype); else { printf(_("The database cluster will be initialized with locales\n" " COLLATE: %s\n" " CTYPE: %s\n" " MESSAGES: %s\n" " MONETARY: %s\n" " NUMERIC: %s\n" " TIME: %s\n"), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time); }#if defined(HAVE_LANGINFO_H) && defined(CODESET) if (strcmp(lc_ctype, "C") != 0 && strcmp(lc_ctype, "POSIX") != 0) { if (strlen(encoding) == 0) { int tmp; tmp = find_matching_encoding(lc_ctype); if (tmp == -1) { fprintf(stderr, _("%s: could not find suitable encoding for locale \"%s\"\n"), progname, lc_ctype); fprintf(stderr, _("Rerun %s with the -E option.\n"), progname); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } else { encodingid = encodingid_to_string(tmp); printf(_("The default database encoding has accordingly been set to %s.\n"), pg_encoding_to_char(tmp)); } } else check_encodings_match(atoi(encodingid), lc_ctype); }#endif /* HAVE_LANGINFO_H && CODESET */ printf("\n"); umask(077); /* * now we are starting to do real work, trap signals so we can clean up */ /* some of these are not valid on Windows */#ifdef SIGHUP pqsignal(SIGHUP, trapsig);#endif#ifdef SIGINT pqsignal(SIGINT, trapsig);#endif#ifdef SIGQUIT pqsignal(SIGQUIT, trapsig);#endif#ifdef SIGTERM pqsignal(SIGTERM, trapsig);#endif /* Ignore SIGPIPE when writing to backend, so we can clean up */#ifdef SIGPIPE pqsignal(SIGPIPE, SIG_IGN);#endif switch (check_data_dir()) { case 0: /* PGDATA not there, must create it */ printf(_("creating directory %s ... "), pg_data); fflush(stdout); if (!mkdatadir(NULL)) exit_nicely(); else check_ok(); made_new_pgdata = true; break; case 1: /* Present but empty, fix permissions and use it */ printf(_("fixing permissions on existing directory %s ... "), pg_data); fflush(stdout); if (chmod(pg_data, 0700) != 0) { fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"), progname, pg_data, strerror(errno)); exit_nicely(); } else check_ok(); found_existing_pgdata = true; break; case 2: /* Present and not empty */ fprintf(stderr, _("%s: directory \"%s\" exists but is not empty\n" "If you want to create a new database system, either remove or empty\n" "the directory \"%s\" or run %s\n" "with an argument other than \"%s\".\n"), progname, pg_data, pg_data, progname, pg_data); exit(1); /* no further message needed */ default: /* Trouble accessing directory */ fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), progname, pg_data, strerror(errno)); exit_nicely(); } /* Create required subdirectories */ for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++) { printf(_("creating directory %s/%s ... "), pg_data, subdirs[i]); fflush(stdout); if (!mkdatadir(subdirs[i])) exit_nicely(); else check_ok(); } /* Top level PG_VERSION is checked by bootstrapper, so make it first */ set_short_version(short_version, NULL); /* * Determine platform-specific config settings * * Use reasonable values if kernel will let us, else scale back. Probe * for max_connections first since it is subject to more constraints than * shared_buffers. */ set_null_conf(); test_connections(); test_buffers(); /* Now create all the text config files */ setup_config(); /* Bootstrap template1 */ bootstrap_template1(short_version); /* * Make the per-database PG_VERSION for template1 only after init'ing it */ set_short_version(short_version, "base/1"); /* Create the stuff we don't need to use bootstrap mode for */ setup_auth(); if (pwprompt || pwfilename) get_set_pwd(); unlimit_systables(); setup_depend(); setup_sysviews(); setup_description(); setup_conversion(); setup_privileges(); setup_schema(); vacuum_db(); make_template0(); make_postgres(); if (authwarning != NULL) fprintf(stderr, "%s", authwarning); /* Get directory specification used to start this executable */ strcpy(bin_dir, argv[0]); get_parent_directory(bin_dir); printf(_("\nSuccess. You can now start the database server using:\n\n" " %s%s%spostmaster%s -D %s%s%s\n" "or\n" " %s%s%spg_ctl%s -D %s%s%s -l logfile start\n\n"), QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH, QUOTE_PATH, pg_data_native, QUOTE_PATH, QUOTE_PATH, bin_dir, (strlen(bin_dir) > 0) ? DIR_SEP : "", QUOTE_PATH, QUOTE_PATH, pg_data_native, QUOTE_PATH); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -