PostgreSQL Source Code  git master
initdb.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "access/xlog_internal.h"
#include "catalog/pg_authid_d.h"
#include "catalog/pg_class_d.h"
#include "catalog/pg_collation_d.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/logging.h"
#include "common/restricted_token.h"
#include "common/string.h"
#include "common/username.h"
#include "fe_utils/string_utils.h"
#include "getaddrinfo.h"
#include "getopt_long.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
Include dependency graph for initdb.c:

Go to the source code of this file.

Data Structures

struct  tsearch_config_match
 

Macros

#define AUTHTRUST_WARNING
 
#define PG_CMD_DECL   char cmd[MAXPGPATH]; FILE *cmdfd
 
#define PG_CMD_OPEN
 
#define PG_CMD_CLOSE
 
#define PG_CMD_PUTS(line)
 
#define PG_CMD_PRINTF(fmt, ...)
 
#define MIN_BUFS_FOR_CONNS(nconns)   ((nconns) * 10)
 

Functions

const char * select_default_timezone (const char *share_path)
 
static char ** replace_token (char **lines, const char *token, const char *replacement)
 
static char ** filter_lines_with_token (char **lines, const char *token)
 
static char ** readfile (const char *path)
 
static void writefile (char *path, char **lines)
 
static FILE * popen_check (const char *command, const char *mode)
 
static char * get_id (void)
 
static int get_encoding_id (const char *encoding_name)
 
static void set_input (char **dest, const char *filename)
 
static void check_input (char *path)
 
static void write_version_file (const char *extrapath)
 
static void set_null_conf (void)
 
static void test_config_settings (void)
 
static void setup_config (void)
 
static void bootstrap_template1 (void)
 
static void setup_auth (FILE *cmdfd)
 
static void get_su_pwd (void)
 
static void setup_depend (FILE *cmdfd)
 
static void setup_sysviews (FILE *cmdfd)
 
static void setup_description (FILE *cmdfd)
 
static void setup_collation (FILE *cmdfd)
 
static void setup_dictionary (FILE *cmdfd)
 
static void setup_privileges (FILE *cmdfd)
 
static void set_info_version (void)
 
static void setup_schema (FILE *cmdfd)
 
static void load_plpgsql (FILE *cmdfd)
 
static void vacuum_db (FILE *cmdfd)
 
static void make_template0 (FILE *cmdfd)
 
static void make_postgres (FILE *cmdfd)
 
static void trapsig (int signum)
 
static void check_ok (void)
 
static char * escape_quotes (const char *src)
 
static char * escape_quotes_bki (const char *src)
 
static int locale_date_order (const char *locale)
 
static void check_locale_name (int category, const char *locale, char **canonname)
 
static bool check_locale_encoding (const char *locale, int encoding)
 
static void setlocales (void)
 
static void usage (const char *progname)
 
void setup_pgdata (void)
 
void setup_bin_paths (const char *argv0)
 
void setup_data_file_paths (void)
 
void setup_locale_encoding (void)
 
void setup_signals (void)
 
void setup_text_search (void)
 
void create_data_directory (void)
 
void create_xlog_or_symlink (void)
 
void warn_on_mount_point (int error)
 
void initialize_data_directory (void)
 
static void cleanup_directories_atexit (void)
 
static char * encodingid_to_string (int enc)
 
static const char * find_matching_ts_config (const char *lc_type)
 
static const char * choose_dsm_implementation (void)
 
static char * pretty_wal_size (int segment_count)
 
static size_t my_strftime (char *s, size_t max, const char *fmt, const struct tm *tm)
 
static void check_authmethod_unspecified (const char **authmethod)
 
static void check_authmethod_valid (const char *authmethod, const char *const *valid_methods, const char *conntype)
 
static void check_need_password (const char *authmethodlocal, const char *authmethodhost)
 
int main (int argc, char *argv[])
 

Variables

static const char *const auth_methods_host []
 
static const char *const auth_methods_local []
 
static char * share_path = NULL
 
static char * pg_data = NULL
 
static char * encoding = NULL
 
static char * locale = NULL
 
static char * lc_collate = NULL
 
static char * lc_ctype = NULL
 
static char * lc_monetary = NULL
 
static char * lc_numeric = NULL
 
static char * lc_time = NULL
 
static char * lc_messages = NULL
 
static const char * default_text_search_config = NULL
 
static char * username = NULL
 
static bool pwprompt = false
 
static char * pwfilename = NULL
 
static char * superuser_password = NULL
 
static const char * authmethodhost = NULL
 
static const char * authmethodlocal = NULL
 
static bool debug = false
 
static bool noclean = false
 
static bool noinstructions = false
 
static bool do_sync = true
 
static bool sync_only = false
 
static bool show_setting = false
 
static bool data_checksums = false
 
static char * xlog_dir = NULL
 
static char * str_wal_segment_size_mb = NULL
 
static int wal_segment_size_mb
 
static const char * progname
 
static int encodingid
 
static char * bki_file
 
static char * hba_file
 
static char * ident_file
 
static char * conf_file
 
static char * dictionary_file
 
static char * info_schema_file
 
static char * features_file
 
static char * system_views_file
 
static bool success = false
 
static bool made_new_pgdata = false
 
static bool found_existing_pgdata = false
 
static bool made_new_xlogdir = false
 
static bool found_existing_xlogdir = false
 
static char infoversion [100]
 
static bool caught_signal = false
 
static bool output_failed = false
 
static int output_errno = 0
 
static char * pgdata_native
 
static int n_connections = 10
 
static int n_buffers = 50
 
static const char * dynamic_shared_memory_type = NULL
 
static const char * default_timezone = NULL
 
static bool authwarning = false
 
static const char * boot_options = "-F"
 
static const char * backend_options = "--single -F -O -j -c search_path=pg_catalog -c exit_on_error=true"
 
static const char *const subdirs []
 
static char bin_path [MAXPGPATH]
 
static char backend_exec [MAXPGPATH]
 
static const struct tsearch_config_match tsearch_config_languages []
 

Macro Definition Documentation

◆ AUTHTRUST_WARNING

#define AUTHTRUST_WARNING
Value:
"# CAUTION: Configuring the system for local \"trust\" authentication\n" \
"# allows any local user to connect as any PostgreSQL user, including\n" \
"# the database superuser. If you do not trust all your local users,\n" \
"# use another authentication method.\n"

Definition at line 183 of file initdb.c.

Referenced by setup_config().

◆ MIN_BUFS_FOR_CONNS

#define MIN_BUFS_FOR_CONNS (   nconns)    ((nconns) * 10)

Referenced by test_config_settings().

◆ PG_CMD_CLOSE

#define PG_CMD_CLOSE
Value:
do { \
if (pclose_check(cmdfd)) \
exit(1); /* message already printed by pclose_check */ \
} while (0)
int pclose_check(FILE *stream)
Definition: exec.c:396

Definition at line 298 of file initdb.c.

Referenced by bootstrap_template1(), and initialize_data_directory().

◆ PG_CMD_DECL

#define PG_CMD_DECL   char cmd[MAXPGPATH]; FILE *cmdfd

Definition at line 289 of file initdb.c.

Referenced by bootstrap_template1(), and initialize_data_directory().

◆ PG_CMD_OPEN

#define PG_CMD_OPEN
Value:
do { \
cmdfd = popen_check(cmd, "w"); \
if (cmdfd == NULL) \
exit(1); /* message already printed by popen_check */ \
} while (0)
static FILE * popen_check(const char *command, const char *mode)
Definition: initdb.c:527

Definition at line 291 of file initdb.c.

Referenced by bootstrap_template1(), and initialize_data_directory().

◆ PG_CMD_PRINTF

#define PG_CMD_PRINTF (   fmt,
  ... 
)
Value:
do { \
if (fprintf(cmdfd, fmt, __VA_ARGS__) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)
#define fprintf
Definition: port.h:219
static int output_errno
Definition: initdb.c:171

Definition at line 310 of file initdb.c.

Referenced by setup_auth(), setup_collation(), and setup_schema().

◆ PG_CMD_PUTS

#define PG_CMD_PUTS (   line)
Value:
do { \
if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)
static int output_errno
Definition: initdb.c:171

Definition at line 304 of file initdb.c.

Referenced by bootstrap_template1(), load_plpgsql(), make_postgres(), make_template0(), setup_auth(), setup_collation(), setup_depend(), setup_description(), setup_dictionary(), setup_privileges(), setup_schema(), setup_sysviews(), and vacuum_db().

Function Documentation

◆ bootstrap_template1()

static void bootstrap_template1 ( void  )
static

Definition at line 1341 of file initdb.c.

References _, backend_exec, bki_file, boot_options, buf, check_ok(), data_checksums, debug, encodingid, encodingid_to_string(), escape_quotes_bki(), FLOAT8PASSBYVAL, fprintf, free, lc_collate, lc_ctype, MAXPGPATH, NAMEDATALEN, PG_CMD_CLOSE, PG_CMD_DECL, PG_CMD_OPEN, PG_CMD_PUTS, pg_log_error, printf, readfile(), replace_token(), snprintf, sprintf, generate_unaccent_rules::stdout, unsetenv, username, and wal_segment_size_mb.

Referenced by initialize_data_directory().

1342 {
1343  PG_CMD_DECL;
1344  char **line;
1345  char **bki_lines;
1346  char headerline[MAXPGPATH];
1347  char buf[64];
1348 
1349  printf(_("running bootstrap script ... "));
1350  fflush(stdout);
1351 
1352  bki_lines = readfile(bki_file);
1353 
1354  /* Check that bki file appears to be of the right version */
1355 
1356  snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1357  PG_MAJORVERSION);
1358 
1359  if (strcmp(headerline, *bki_lines) != 0)
1360  {
1361  pg_log_error("input file \"%s\" does not belong to PostgreSQL %s",
1362  bki_file, PG_VERSION);
1363  fprintf(stderr,
1364  _("Check your installation or specify the correct path "
1365  "using the option -L.\n"));
1366  exit(1);
1367  }
1368 
1369  /* Substitute for various symbols used in the BKI file */
1370 
1371  sprintf(buf, "%d", NAMEDATALEN);
1372  bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1373 
1374  sprintf(buf, "%d", (int) sizeof(Pointer));
1375  bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
1376 
1377  bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
1378  (sizeof(Pointer) == 4) ? "i" : "d");
1379 
1380  bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1381  FLOAT8PASSBYVAL ? "true" : "false");
1382 
1383  bki_lines = replace_token(bki_lines, "POSTGRES",
1385 
1386  bki_lines = replace_token(bki_lines, "ENCODING",
1388 
1389  bki_lines = replace_token(bki_lines, "LC_COLLATE",
1391 
1392  bki_lines = replace_token(bki_lines, "LC_CTYPE",
1394 
1395  /* Also ensure backend isn't confused by this environment var: */
1396  unsetenv("PGCLIENTENCODING");
1397 
1398  snprintf(cmd, sizeof(cmd),
1399  "\"%s\" --boot -x1 -X %u %s %s %s",
1400  backend_exec,
1401  wal_segment_size_mb * (1024 * 1024),
1402  data_checksums ? "-k" : "",
1403  boot_options,
1404  debug ? "-d 5" : "");
1405 
1406 
1407  PG_CMD_OPEN;
1408 
1409  for (line = bki_lines; *line != NULL; line++)
1410  {
1411  PG_CMD_PUTS(*line);
1412  free(*line);
1413  }
1414 
1415  PG_CMD_CLOSE;
1416 
1417  free(bki_lines);
1418 
1419  check_ok();
1420 }
static int encodingid
Definition: initdb.c:154
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:367
static bool debug
Definition: initdb.c:140
static char * lc_collate
Definition: initdb.c:127
static char * encodingid_to_string(int enc)
Definition: initdb.c:619
#define pg_log_error(...)
Definition: logging.h:80
#define printf(...)
Definition: port.h:221
#define PG_CMD_PUTS(line)
Definition: initdb.c:304
#define fprintf
Definition: port.h:219
#define NAMEDATALEN
#define sprintf
Definition: port.h:217
char * Pointer
Definition: c.h:406
#define PG_CMD_DECL
Definition: initdb.c:289
#define MAXPGPATH
static char * lc_ctype
Definition: initdb.c:128
#define PG_CMD_OPEN
Definition: initdb.c:291
static char * buf
Definition: pg_test_fsync.c:68
static int wal_segment_size_mb
Definition: initdb.c:149
static void check_ok(void)
Definition: initdb.c:2019
#define PG_CMD_CLOSE
Definition: initdb.c:298
static const char * boot_options
Definition: initdb.c:200
static bool data_checksums
Definition: initdb.c:146
static char * username
Definition: initdb.c:134
#define free(a)
Definition: header.h:65
#define FLOAT8PASSBYVAL
Definition: c.h:558
static char backend_exec[MAXPGPATH]
Definition: initdb.c:231
static char ** readfile(const char *path)
Definition: initdb.c:450
static char * bki_file
Definition: initdb.c:155
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:89
static char * escape_quotes_bki(const char *src)
Definition: initdb.c:340
#define unsetenv(x)
Definition: win32_port.h:498

◆ check_authmethod_unspecified()

static void check_authmethod_unspecified ( const char **  authmethod)
static

Definition at line 2311 of file initdb.c.

References authwarning.

Referenced by main().

2312 {
2313  if (*authmethod == NULL)
2314  {
2315  authwarning = true;
2316  *authmethod = "trust";
2317  }
2318 }
static bool authwarning
Definition: initdb.c:188

◆ check_authmethod_valid()

static void check_authmethod_valid ( const char *  authmethod,
const char *const *  valid_methods,
const char *  conntype 
)
static

Definition at line 2321 of file initdb.c.

References pg_log_error.

Referenced by main().

2322 {
2323  const char *const *p;
2324 
2325  for (p = valid_methods; *p; p++)
2326  {
2327  if (strcmp(authmethod, *p) == 0)
2328  return;
2329  /* with space = param */
2330  if (strchr(authmethod, ' '))
2331  if (strncmp(authmethod, *p, (authmethod - strchr(authmethod, ' '))) == 0)
2332  return;
2333  }
2334 
2335  pg_log_error("invalid authentication method \"%s\" for \"%s\" connections",
2336  authmethod, conntype);
2337  exit(1);
2338 }
#define pg_log_error(...)
Definition: logging.h:80

◆ check_input()

static void check_input ( char *  path)
static

Definition at line 771 of file initdb.c.

References _, fprintf, pg_log_error, S_ISREG, stat::st_mode, and stat.

Referenced by setup_data_file_paths().

772 {
773  struct stat statbuf;
774 
775  if (stat(path, &statbuf) != 0)
776  {
777  if (errno == ENOENT)
778  {
779  pg_log_error("file \"%s\" does not exist", path);
780  fprintf(stderr,
781  _("This might mean you have a corrupted installation or identified\n"
782  "the wrong directory with the invocation option -L.\n"));
783  }
784  else
785  {
786  pg_log_error("could not access file \"%s\": %m", path);
787  fprintf(stderr,
788  _("This might mean you have a corrupted installation or identified\n"
789  "the wrong directory with the invocation option -L.\n"));
790  }
791  exit(1);
792  }
793  if (!S_ISREG(statbuf.st_mode))
794  {
795  pg_log_error("file \"%s\" is not a regular file", path);
796  fprintf(stderr,
797  _("This might mean you have a corrupted installation or identified\n"
798  "the wrong directory with the invocation option -L.\n"));
799  exit(1);
800  }
801 }
#define pg_log_error(...)
Definition: logging.h:80
#define fprintf
Definition: port.h:219
#define S_ISREG(m)
Definition: win32_port.h:319
#define _(x)
Definition: elog.c:89
#define stat
Definition: win32_port.h:275

◆ check_locale_encoding()

static bool check_locale_encoding ( const char *  locale,
int  encoding 
)
static

Definition at line 2180 of file initdb.c.

References _, fprintf, pg_encoding_to_char(), pg_get_encoding_from_locale(), pg_log_error, PG_SQL_ASCII, PG_UTF8, and progname.

Referenced by setup_locale_encoding().

2181 {
2182  int locale_enc;
2183 
2184  locale_enc = pg_get_encoding_from_locale(locale, true);
2185 
2186  /* See notes in createdb() to understand these tests */
2187  if (!(locale_enc == user_enc ||
2188  locale_enc == PG_SQL_ASCII ||
2189  locale_enc == -1 ||
2190 #ifdef WIN32
2191  user_enc == PG_UTF8 ||
2192 #endif
2193  user_enc == PG_SQL_ASCII))
2194  {
2195  pg_log_error("encoding mismatch");
2196  fprintf(stderr,
2197  _("The encoding you selected (%s) and the encoding that the\n"
2198  "selected locale uses (%s) do not match. This would lead to\n"
2199  "misbehavior in various character string processing functions.\n"
2200  "Rerun %s and either do not specify an encoding explicitly,\n"
2201  "or choose a matching combination.\n"),
2202  pg_encoding_to_char(user_enc),
2203  pg_encoding_to_char(locale_enc),
2204  progname);
2205  return false;
2206  }
2207  return true;
2208 }
#define pg_log_error(...)
Definition: logging.h:80
#define fprintf
Definition: port.h:219
static const char * progname
Definition: initdb.c:153
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:452
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
static char * locale
Definition: initdb.c:126
#define _(x)
Definition: elog.c:89

◆ check_locale_name()

static void check_locale_name ( int  category,
const char *  locale,
char **  canonname 
)
static

Definition at line 2116 of file initdb.c.

References free, pg_log_error, pg_strdup(), and setlocale.

Referenced by setlocales().

2117 {
2118  char *save;
2119  char *res;
2120 
2121  if (canonname)
2122  *canonname = NULL; /* in case of failure */
2123 
2124  save = setlocale(category, NULL);
2125  if (!save)
2126  {
2127  pg_log_error("setlocale() failed");
2128  exit(1);
2129  }
2130 
2131  /* save may be pointing at a modifiable scratch variable, so copy it. */
2132  save = pg_strdup(save);
2133 
2134  /* for setlocale() call */
2135  if (!locale)
2136  locale = "";
2137 
2138  /* set the locale with setlocale, to see if it accepts it. */
2139  res = setlocale(category, locale);
2140 
2141  /* save canonical name if requested. */
2142  if (res && canonname)
2143  *canonname = pg_strdup(res);
2144 
2145  /* restore old value. */
2146  if (!setlocale(category, save))
2147  {
2148  pg_log_error("failed to restore old locale \"%s\"", save);
2149  exit(1);
2150  }
2151  free(save);
2152 
2153  /* complain if locale wasn't valid */
2154  if (res == NULL)
2155  {
2156  if (*locale)
2157  pg_log_error("invalid locale name \"%s\"", locale);
2158  else
2159  {
2160  /*
2161  * If no relevant switch was given on command line, locale is an
2162  * empty string, which is not too helpful to report. Presumably
2163  * setlocale() found something it did not like in the environment.
2164  * Ideally we'd report the bad environment variable, but since
2165  * setlocale's behavior is implementation-specific, it's hard to
2166  * be sure what it didn't like. Print a safe generic message.
2167  */
2168  pg_log_error("invalid locale settings; check LANG and LC_* environment variables");
2169  }
2170  exit(1);
2171  }
2172 }
#define setlocale(a, b)
Definition: win32_port.h:436
#define pg_log_error(...)
Definition: logging.h:80
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
static char * locale
Definition: initdb.c:126

◆ check_need_password()

static void check_need_password ( const char *  authmethodlocal,
const char *  authmethodhost 
)
static

Definition at line 2341 of file initdb.c.

References pg_log_error, pwfilename, and pwprompt.

Referenced by main().

2342 {
2343  if ((strcmp(authmethodlocal, "md5") == 0 ||
2344  strcmp(authmethodlocal, "password") == 0 ||
2345  strcmp(authmethodlocal, "scram-sha-256") == 0) &&
2346  (strcmp(authmethodhost, "md5") == 0 ||
2347  strcmp(authmethodhost, "password") == 0 ||
2348  strcmp(authmethodhost, "scram-sha-256") == 0) &&
2349  !(pwprompt || pwfilename))
2350  {
2351  pg_log_error("must specify a password for the superuser to enable password authentication");
2352  exit(1);
2353  }
2354 }
#define pg_log_error(...)
Definition: logging.h:80
static bool pwprompt
Definition: initdb.c:135
static const char * authmethodlocal
Definition: initdb.c:139
static char * pwfilename
Definition: initdb.c:136
static const char * authmethodhost
Definition: initdb.c:138

◆ check_ok()

static void check_ok ( void  )
static

Definition at line 2019 of file initdb.c.

References _, caught_signal, output_errno, output_failed, printf, generate_unaccent_rules::stdout, and strerror.

Referenced by adjust_data_dir(), bootstrap_template1(), check_cluster_versions(), check_for_isn_and_int8_passing_mismatch(), check_for_jsonb_9_4_usage(), check_for_new_tablespace_dir(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_reg_data_type_usage(), check_for_tables_with_oids(), check_for_user_defined_postfix_ops(), check_is_install_user(), check_loadable_libraries(), check_proper_datallowconn(), copy_subdir_files(), copy_xact_xlog_xid(), create_data_directory(), create_new_objects(), create_script_for_old_cluster_deletion(), create_xlog_or_symlink(), disable_old_cluster(), generate_old_dump(), initialize_data_directory(), main(), new_9_0_populate_pg_largeobject_metadata(), old_11_check_for_sql_identifier_data_type_usage(), old_9_3_check_for_line_data_type_usage(), old_9_6_check_for_unknown_data_type_usage(), old_9_6_invalidate_hash_indexes(), prepare_new_cluster(), prepare_new_globals(), remove_new_subdir(), set_frozenxids(), setup_config(), and transfer_all_new_tablespaces().

2020 {
2021  if (caught_signal)
2022  {
2023  printf(_("caught signal\n"));
2024  fflush(stdout);
2025  exit(1);
2026  }
2027  else if (output_failed)
2028  {
2029  printf(_("could not write to child process: %s\n"),
2031  fflush(stdout);
2032  exit(1);
2033  }
2034  else
2035  {
2036  /* all seems well */
2037  printf(_("ok\n"));
2038  fflush(stdout);
2039  }
2040 }
static bool caught_signal
Definition: initdb.c:169
#define printf(...)
Definition: port.h:221
#define strerror
Definition: port.h:228
static bool output_failed
Definition: initdb.c:170
static int output_errno
Definition: initdb.c:171
#define _(x)
Definition: elog.c:89

◆ choose_dsm_implementation()

static const char* choose_dsm_implementation ( void  )
static

Definition at line 869 of file initdb.c.

References close, fd(), name, random(), snprintf, and srandom().

Referenced by test_config_settings().

870 {
871 #ifdef HAVE_SHM_OPEN
872  int ntries = 10;
873 
874  /* Initialize random(); this function is its only user in this program. */
875  srandom((unsigned int) (getpid() ^ time(NULL)));
876 
877  while (ntries > 0)
878  {
879  uint32 handle;
880  char name[64];
881  int fd;
882 
883  handle = random();
884  snprintf(name, 64, "/PostgreSQL.%u", handle);
885  if ((fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0600)) != -1)
886  {
887  close(fd);
888  shm_unlink(name);
889  return "posix";
890  }
891  if (errno != EEXIST)
892  break;
893  --ntries;
894  }
895 #endif
896 
897 #ifdef WIN32
898  return "windows";
899 #else
900  return "sysv";
901 #endif
902 }
long random(void)
Definition: random.c:22
static int fd(const char *x, int i)
Definition: preproc-init.c:105
unsigned int uint32
Definition: c.h:429
const char * name
Definition: encode.c:515
void srandom(unsigned int seed)
Definition: srandom.c:22
#define close(a)
Definition: win32.h:12
#define snprintf
Definition: port.h:215

◆ cleanup_directories_atexit()

static void cleanup_directories_atexit ( void  )
static

Definition at line 545 of file initdb.c.

References found_existing_pgdata, found_existing_xlogdir, made_new_pgdata, made_new_xlogdir, noclean, pg_data, pg_log_error, pg_log_info, rmtree(), success, and xlog_dir.

Referenced by main().

546 {
547  if (success)
548  return;
549 
550  if (!noclean)
551  {
552  if (made_new_pgdata)
553  {
554  pg_log_info("removing data directory \"%s\"", pg_data);
555  if (!rmtree(pg_data, true))
556  pg_log_error("failed to remove data directory");
557  }
558  else if (found_existing_pgdata)
559  {
560  pg_log_info("removing contents of data directory \"%s\"",
561  pg_data);
562  if (!rmtree(pg_data, false))
563  pg_log_error("failed to remove contents of data directory");
564  }
565 
566  if (made_new_xlogdir)
567  {
568  pg_log_info("removing WAL directory \"%s\"", xlog_dir);
569  if (!rmtree(xlog_dir, true))
570  pg_log_error("failed to remove WAL directory");
571  }
572  else if (found_existing_xlogdir)
573  {
574  pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
575  if (!rmtree(xlog_dir, false))
576  pg_log_error("failed to remove contents of WAL directory");
577  }
578  /* otherwise died during startup, do nothing! */
579  }
580  else
581  {
583  pg_log_info("data directory \"%s\" not removed at user's request",
584  pg_data);
585 
587  pg_log_info("WAL directory \"%s\" not removed at user's request",
588  xlog_dir);
589  }
590 }
#define pg_log_error(...)
Definition: logging.h:80
static bool made_new_xlogdir
Definition: initdb.c:166
static bool found_existing_pgdata
Definition: initdb.c:165
static char * pg_data
Definition: initdb.c:124
static bool found_existing_xlogdir
Definition: initdb.c:167
static bool made_new_pgdata
Definition: initdb.c:164
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
static char * xlog_dir
Definition: initdb.c:147
static bool success
Definition: initdb.c:163
#define pg_log_info(...)
Definition: logging.h:88
static bool noclean
Definition: initdb.c:141

◆ create_data_directory()

void create_data_directory ( void  )

Definition at line 2632 of file initdb.c.

References _, check_ok(), found_existing_pgdata, fprintf, made_new_pgdata, pg_check_dir(), pg_data, pg_dir_create_mode, pg_log_error, pg_mkdir_p(), printf, progname, generate_unaccent_rules::stdout, and warn_on_mount_point().

Referenced by initialize_data_directory().

2633 {
2634  int ret;
2635 
2636  switch ((ret = pg_check_dir(pg_data)))
2637  {
2638  case 0:
2639  /* PGDATA not there, must create it */
2640  printf(_("creating directory %s ... "),
2641  pg_data);
2642  fflush(stdout);
2643 
2645  {
2646  pg_log_error("could not create directory \"%s\": %m", pg_data);
2647  exit(1);
2648  }
2649  else
2650  check_ok();
2651 
2652  made_new_pgdata = true;
2653  break;
2654 
2655  case 1:
2656  /* Present but empty, fix permissions and use it */
2657  printf(_("fixing permissions on existing directory %s ... "),
2658  pg_data);
2659  fflush(stdout);
2660 
2661  if (chmod(pg_data, pg_dir_create_mode) != 0)
2662  {
2663  pg_log_error("could not change permissions of directory \"%s\": %m",
2664  pg_data);
2665  exit(1);
2666  }
2667  else
2668  check_ok();
2669 
2670  found_existing_pgdata = true;
2671  break;
2672 
2673  case 2:
2674  case 3:
2675  case 4:
2676  /* Present and not empty */
2677  pg_log_error("directory \"%s\" exists but is not empty", pg_data);
2678  if (ret != 4)
2679  warn_on_mount_point(ret);
2680  else
2681  fprintf(stderr,
2682  _("If you want to create a new database system, either remove or empty\n"
2683  "the directory \"%s\" or run %s\n"
2684  "with an argument other than \"%s\".\n"),
2686  exit(1); /* no further message needed */
2687 
2688  default:
2689  /* Trouble accessing directory */
2690  pg_log_error("could not access directory \"%s\": %m", pg_data);
2691  exit(1);
2692  }
2693 }
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
void warn_on_mount_point(int error)
Definition: initdb.c:2804
#define pg_log_error(...)
Definition: logging.h:80
#define printf(...)
Definition: port.h:221
#define fprintf
Definition: port.h:219
static bool found_existing_pgdata
Definition: initdb.c:165
static char * pg_data
Definition: initdb.c:124
static void check_ok(void)
Definition: initdb.c:2019
static bool made_new_pgdata
Definition: initdb.c:164
static const char * progname
Definition: initdb.c:153
int pg_dir_create_mode
Definition: file_perm.c:18
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:31
#define _(x)
Definition: elog.c:89

◆ create_xlog_or_symlink()

void create_xlog_or_symlink ( void  )

Definition at line 2698 of file initdb.c.

References _, canonicalize_path(), check_ok(), found_existing_xlogdir, fprintf, free, is_absolute_path, made_new_xlogdir, mkdir, pg_check_dir(), pg_data, pg_dir_create_mode, pg_log_error, pg_mkdir_p(), printf, psprintf(), generate_unaccent_rules::stdout, symlink, warn_on_mount_point(), and xlog_dir.

Referenced by initialize_data_directory().

2699 {
2700  char *subdirloc;
2701 
2702  /* form name of the place for the subdirectory or symlink */
2703  subdirloc = psprintf("%s/pg_wal", pg_data);
2704 
2705  if (xlog_dir)
2706  {
2707  int ret;
2708 
2709  /* clean up xlog directory name, check it's absolute */
2711  if (!is_absolute_path(xlog_dir))
2712  {
2713  pg_log_error("WAL directory location must be an absolute path");
2714  exit(1);
2715  }
2716 
2717  /* check if the specified xlog directory exists/is empty */
2718  switch ((ret = pg_check_dir(xlog_dir)))
2719  {
2720  case 0:
2721  /* xlog directory not there, must create it */
2722  printf(_("creating directory %s ... "),
2723  xlog_dir);
2724  fflush(stdout);
2725 
2727  {
2728  pg_log_error("could not create directory \"%s\": %m",
2729  xlog_dir);
2730  exit(1);
2731  }
2732  else
2733  check_ok();
2734 
2735  made_new_xlogdir = true;
2736  break;
2737 
2738  case 1:
2739  /* Present but empty, fix permissions and use it */
2740  printf(_("fixing permissions on existing directory %s ... "),
2741  xlog_dir);
2742  fflush(stdout);
2743 
2744  if (chmod(xlog_dir, pg_dir_create_mode) != 0)
2745  {
2746  pg_log_error("could not change permissions of directory \"%s\": %m",
2747  xlog_dir);
2748  exit(1);
2749  }
2750  else
2751  check_ok();
2752 
2753  found_existing_xlogdir = true;
2754  break;
2755 
2756  case 2:
2757  case 3:
2758  case 4:
2759  /* Present and not empty */
2760  pg_log_error("directory \"%s\" exists but is not empty", xlog_dir);
2761  if (ret != 4)
2762  warn_on_mount_point(ret);
2763  else
2764  fprintf(stderr,
2765  _("If you want to store the WAL there, either remove or empty the directory\n"
2766  "\"%s\".\n"),
2767  xlog_dir);
2768  exit(1);
2769 
2770  default:
2771  /* Trouble accessing directory */
2772  pg_log_error("could not access directory \"%s\": %m", xlog_dir);
2773  exit(1);
2774  }
2775 
2776 #ifdef HAVE_SYMLINK
2777  if (symlink(xlog_dir, subdirloc) != 0)
2778  {
2779  pg_log_error("could not create symbolic link \"%s\": %m",
2780  subdirloc);
2781  exit(1);
2782  }
2783 #else
2784  pg_log_error("symlinks are not supported on this platform");
2785  exit(1);
2786 #endif
2787  }
2788  else
2789  {
2790  /* Without -X option, just make the subdirectory normally */
2791  if (mkdir(subdirloc, pg_dir_create_mode) < 0)
2792  {
2793  pg_log_error("could not create directory \"%s\": %m",
2794  subdirloc);
2795  exit(1);
2796  }
2797  }
2798 
2799  free(subdirloc);
2800 }
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
void warn_on_mount_point(int error)
Definition: initdb.c:2804
#define pg_log_error(...)
Definition: logging.h:80
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static bool made_new_xlogdir
Definition: initdb.c:166
void canonicalize_path(char *path)
Definition: path.c:254
#define printf(...)
Definition: port.h:221
#define fprintf
Definition: port.h:219
#define symlink(oldpath, newpath)
Definition: win32_port.h:227
#define is_absolute_path(filename)
Definition: port.h:86
static char * pg_data
Definition: initdb.c:124
static void check_ok(void)
Definition: initdb.c:2019
static bool found_existing_xlogdir
Definition: initdb.c:167
int pg_dir_create_mode
Definition: file_perm.c:18
static char * xlog_dir
Definition: initdb.c:147
#define free(a)
Definition: header.h:65
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:31
#define mkdir(a, b)
Definition: win32_port.h:63
#define _(x)
Definition: elog.c:89

◆ encodingid_to_string()

static char* encodingid_to_string ( int  enc)
static

Definition at line 619 of file initdb.c.

References pg_strdup(), and sprintf.

Referenced by bootstrap_template1().

620 {
621  char result[20];
622 
623  sprintf(result, "%d", enc);
624  return pg_strdup(result);
625 }
#define sprintf
Definition: port.h:217
struct pg_encoding enc
Definition: encode.c:516
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85

◆ escape_quotes()

static char * escape_quotes ( const char *  src)
static

Definition at line 321 of file initdb.c.

References escape_single_quotes_ascii(), and pg_log_error.

Referenced by escape_quotes_bki(), setup_auth(), setup_config(), setup_privileges(), and setup_schema().

322 {
323  char *result = escape_single_quotes_ascii(src);
324 
325  if (!result)
326  {
327  pg_log_error("out of memory");
328  exit(1);
329  }
330  return result;
331 }
#define pg_log_error(...)
Definition: logging.h:80
char * escape_single_quotes_ascii(const char *src)
Definition: quotes.c:33

◆ escape_quotes_bki()

static char * escape_quotes_bki ( const char *  src)
static

Definition at line 340 of file initdb.c.

References escape_quotes(), free, and pg_malloc().

Referenced by bootstrap_template1().

341 {
342  char *result;
343  char *data = escape_quotes(src);
344  char *resultp;
345  char *datap;
346 
347  result = (char *) pg_malloc(strlen(data) + 3);
348  resultp = result;
349  *resultp++ = '\'';
350  for (datap = data; *datap; datap++)
351  *resultp++ = *datap;
352  *resultp++ = '\'';
353  *resultp = '\0';
354 
355  free(data);
356  return result;
357 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define free(a)
Definition: header.h:65
static char * escape_quotes(const char *src)
Definition: initdb.c:321

◆ filter_lines_with_token()

static char ** filter_lines_with_token ( char **  lines,
const char *  token 
)
static

Definition at line 423 of file initdb.c.

References i, and pg_malloc().

Referenced by setup_config().

424 {
425  int numlines = 1;
426  int i,
427  src,
428  dst;
429  char **result;
430 
431  for (i = 0; lines[i]; i++)
432  numlines++;
433 
434  result = (char **) pg_malloc(numlines * sizeof(char *));
435 
436  for (src = 0, dst = 0; src < numlines; src++)
437  {
438  if (lines[src] == NULL || strstr(lines[src], token) == NULL)
439  result[dst++] = lines[src];
440  }
441 
442  return result;
443 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int i

◆ find_matching_ts_config()

static const char* find_matching_ts_config ( const char *  lc_type)
static

Definition at line 716 of file initdb.c.

References free, i, tsearch_config_match::langname, pg_strcasecmp(), pg_strdup(), and tsearch_config_match::tsconfname.

Referenced by setup_text_search().

717 {
718  int i;
719  char *langname,
720  *ptr;
721 
722  /*
723  * Convert lc_ctype to a language name by stripping everything after an
724  * underscore (usual case) or a hyphen (Windows "locale name"; see
725  * comments at IsoLocaleName()).
726  *
727  * XXX Should ' ' be a stop character? This would select "norwegian" for
728  * the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
729  * should also accept the "nn" and "nb" Unix locales.
730  *
731  * Just for paranoia, we also stop at '.' or '@'.
732  */
733  if (lc_type == NULL)
734  langname = pg_strdup("");
735  else
736  {
737  ptr = langname = pg_strdup(lc_type);
738  while (*ptr &&
739  *ptr != '_' && *ptr != '-' && *ptr != '.' && *ptr != '@')
740  ptr++;
741  *ptr = '\0';
742  }
743 
744  for (i = 0; tsearch_config_languages[i].tsconfname; i++)
745  {
746  if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
747  {
748  free(langname);
750  }
751  }
752 
753  free(langname);
754  return NULL;
755 }
static const struct tsearch_config_match tsearch_config_languages[]
Definition: initdb.c:655
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * tsconfname
Definition: initdb.c:651
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
int i

◆ get_encoding_id()

static int get_encoding_id ( const char *  encoding_name)
static

Definition at line 631 of file initdb.c.

References enc, pg_log_error, and pg_valid_server_encoding().

Referenced by setup_locale_encoding().

632 {
633  int enc;
634 
635  if (encoding_name && *encoding_name)
636  {
637  if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
638  return enc;
639  }
640  pg_log_error("\"%s\" is not a valid server encoding name",
641  encoding_name ? encoding_name : "(null)");
642  exit(1);
643 }
int pg_valid_server_encoding(const char *name)
Definition: encnames.c:500
#define pg_log_error(...)
Definition: logging.h:80
struct pg_encoding enc
Definition: encode.c:516

◆ get_id()

static char * get_id ( void  )
static

Definition at line 598 of file initdb.c.

References _, fprintf, get_user_name_or_exit(), pg_log_error, pg_strdup(), progname, and username.

Referenced by main().

599 {
600  const char *username;
601 
602 #ifndef WIN32
603  if (geteuid() == 0) /* 0 is root's uid */
604  {
605  pg_log_error("cannot be run as root");
606  fprintf(stderr,
607  _("Please log in (using, e.g., \"su\") as the (unprivileged) user that will\n"
608  "own the server process.\n"));
609  exit(1);
610  }
611 #endif
612 
613  username = get_user_name_or_exit(progname);
614 
615  return pg_strdup(username);
616 }
#define pg_log_error(...)
Definition: logging.h:80
#define fprintf
Definition: port.h:219
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static const char * progname
Definition: initdb.c:153
static char * username
Definition: initdb.c:134
#define _(x)
Definition: elog.c:89
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74

◆ get_su_pwd()

static void get_su_pwd ( void  )
static

Definition at line 1450 of file initdb.c.

References _, fprintf, free, pg_get_line(), pg_log_error, pg_strip_crlf(), printf, pwfilename, pwprompt, simple_prompt(), generate_unaccent_rules::stdout, and superuser_password.

Referenced by main().

1451 {
1452  char *pwd1;
1453 
1454  if (pwprompt)
1455  {
1456  /*
1457  * Read password from terminal
1458  */
1459  char *pwd2;
1460 
1461  printf("\n");
1462  fflush(stdout);
1463  pwd1 = simple_prompt("Enter new superuser password: ", false);
1464  pwd2 = simple_prompt("Enter it again: ", false);
1465  if (strcmp(pwd1, pwd2) != 0)
1466  {
1467  fprintf(stderr, _("Passwords didn't match.\n"));
1468  exit(1);
1469  }
1470  free(pwd2);
1471  }
1472  else
1473  {
1474  /*
1475  * Read password from file
1476  *
1477  * Ideally this should insist that the file not be world-readable.
1478  * However, this option is mainly intended for use on Windows where
1479  * file permissions may not exist at all, so we'll skip the paranoia
1480  * for now.
1481  */
1482  FILE *pwf = fopen(pwfilename, "r");
1483 
1484  if (!pwf)
1485  {
1486  pg_log_error("could not open file \"%s\" for reading: %m",
1487  pwfilename);
1488  exit(1);
1489  }
1490  pwd1 = pg_get_line(pwf);
1491  if (!pwd1)
1492  {
1493  if (ferror(pwf))
1494  pg_log_error("could not read password from file \"%s\": %m",
1495  pwfilename);
1496  else
1497  pg_log_error("password file \"%s\" is empty",
1498  pwfilename);
1499  exit(1);
1500  }
1501  fclose(pwf);
1502 
1503  (void) pg_strip_crlf(pwd1);
1504  }
1505 
1506  superuser_password = pwd1;
1507 }
int pg_strip_crlf(char *str)
Definition: string.c:121
#define pg_log_error(...)
Definition: logging.h:80
#define printf(...)
Definition: port.h:221
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static bool pwprompt
Definition: initdb.c:135
#define fprintf
Definition: port.h:219
static char * pwfilename
Definition: initdb.c:136
#define free(a)
Definition: header.h:65
static char * superuser_password
Definition: initdb.c:137
char * pg_get_line(FILE *stream)
Definition: pg_get_line.c:52
#define _(x)
Definition: elog.c:89

◆ initialize_data_directory()

void initialize_data_directory ( void  )

Definition at line 2820 of file initdb.c.

References _, backend_exec, backend_options, bootstrap_template1(), check_ok(), create_data_directory(), create_xlog_or_symlink(), DEVNULL, free, i, lengthof, load_plpgsql(), make_postgres(), make_template0(), mkdir, PG_CMD_CLOSE, PG_CMD_DECL, PG_CMD_OPEN, pg_data, pg_dir_create_mode, pg_log_error, pg_mode_mask, printf, psprintf(), set_null_conf(), setup_auth(), setup_collation(), setup_config(), setup_depend(), setup_description(), setup_dictionary(), setup_privileges(), setup_schema(), setup_signals(), setup_sysviews(), snprintf, generate_unaccent_rules::stdout, subdirs, test_config_settings(), vacuum_db(), and write_version_file().

Referenced by main().

2821 {
2822  PG_CMD_DECL;
2823  int i;
2824 
2825  setup_signals();
2826 
2827  /*
2828  * Set mask based on requested PGDATA permissions. pg_mode_mask, and
2829  * friends like pg_dir_create_mode, are set to owner-only by default and
2830  * then updated if -g is passed in by calling SetDataDirectoryCreatePerm()
2831  * when parsing our options (see above).
2832  */
2833  umask(pg_mode_mask);
2834 
2836 
2838 
2839  /* Create required subdirectories (other than pg_wal) */
2840  printf(_("creating subdirectories ... "));
2841  fflush(stdout);
2842 
2843  for (i = 0; i < lengthof(subdirs); i++)
2844  {
2845  char *path;
2846 
2847  path = psprintf("%s/%s", pg_data, subdirs[i]);
2848 
2849  /*
2850  * The parent directory already exists, so we only need mkdir() not
2851  * pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
2852  */
2853  if (mkdir(path, pg_dir_create_mode) < 0)
2854  {
2855  pg_log_error("could not create directory \"%s\": %m", path);
2856  exit(1);
2857  }
2858 
2859  free(path);
2860  }
2861 
2862  check_ok();
2863 
2864  /* Top level PG_VERSION is checked by bootstrapper, so make it first */
2865  write_version_file(NULL);
2866 
2867  /* Select suitable configuration settings */
2868  set_null_conf();
2870 
2871  /* Now create all the text config files */
2872  setup_config();
2873 
2874  /* Bootstrap template1 */
2876 
2877  /*
2878  * Make the per-database PG_VERSION for template1 only after init'ing it
2879  */
2880  write_version_file("base/1");
2881 
2882  /*
2883  * Create the stuff we don't need to use bootstrap mode for, using a
2884  * backend running in simple standalone mode.
2885  */
2886  fputs(_("performing post-bootstrap initialization ... "), stdout);
2887  fflush(stdout);
2888 
2889  snprintf(cmd, sizeof(cmd),
2890  "\"%s\" %s template1 >%s",
2892  DEVNULL);
2893 
2894  PG_CMD_OPEN;
2895 
2896  setup_auth(cmdfd);
2897 
2898  setup_depend(cmdfd);
2899 
2900  /*
2901  * Note that no objects created after setup_depend() will be "pinned".
2902  * They are all droppable at the whim of the DBA.
2903  */
2904 
2905  setup_sysviews(cmdfd);
2906 
2907  setup_description(cmdfd);
2908 
2909  setup_collation(cmdfd);
2910 
2911  setup_dictionary(cmdfd);
2912 
2913  setup_privileges(cmdfd);
2914 
2915  setup_schema(cmdfd);
2916 
2917  load_plpgsql(cmdfd);
2918 
2919  vacuum_db(cmdfd);
2920 
2921  make_template0(cmdfd);
2922 
2923  make_postgres(cmdfd);
2924 
2925  PG_CMD_CLOSE;
2926 
2927  check_ok();
2928 }
static void setup_depend(FILE *cmdfd)
Definition: initdb.c:1513
static void vacuum_db(FILE *cmdfd)
Definition: initdb.c:1925
static const char * backend_options
Definition: initdb.c:201
#define pg_log_error(...)
Definition: logging.h:80
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void setup_auth(FILE *cmdfd)
Definition: initdb.c:1426
static void setup_dictionary(FILE *cmdfd)
Definition: initdb.c:1668
void create_xlog_or_symlink(void)
Definition: initdb.c:2698
static void bootstrap_template1(void)
Definition: initdb.c:1341
#define printf(...)
Definition: port.h:221
void setup_signals(void)
Definition: initdb.c:2603
#define lengthof(array)
Definition: c.h:722
static void set_null_conf(void)
Definition: initdb.c:837
#define PG_CMD_DECL
Definition: initdb.c:289
static const char *const subdirs[]
Definition: initdb.c:203
#define PG_CMD_OPEN
Definition: initdb.c:291
static void setup_config(void)
Definition: initdb.c:1041
static void load_plpgsql(FILE *cmdfd)
Definition: initdb.c:1916
static void write_version_file(const char *extrapath)
Definition: initdb.c:808
static void setup_description(FILE *cmdfd)
Definition: initdb.c:1628
static char * pg_data
Definition: initdb.c:124
#define DEVNULL
Definition: port.h:146
static void check_ok(void)
Definition: initdb.c:2019
static void test_config_settings(void)
Definition: initdb.c:910
void create_data_directory(void)
Definition: initdb.c:2632
int pg_dir_create_mode
Definition: file_perm.c:18
#define PG_CMD_CLOSE
Definition: initdb.c:298
static void make_template0(FILE *cmdfd)
Definition: initdb.c:1935
#define free(a)
Definition: header.h:65
static void setup_collation(FILE *cmdfd)
Definition: initdb.c:1649
static void setup_schema(FILE *cmdfd)
Definition: initdb.c:1883
static void setup_privileges(FILE *cmdfd)
Definition: initdb.c:1705
static void make_postgres(FILE *cmdfd)
Definition: initdb.c:1973
int i
static char backend_exec[MAXPGPATH]
Definition: initdb.c:231
#define mkdir(a, b)
Definition: win32_port.h:63
static void setup_sysviews(FILE *cmdfd)
Definition: initdb.c:1606
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:89
int pg_mode_mask
Definition: file_perm.c:25

◆ load_plpgsql()

static void load_plpgsql ( FILE *  cmdfd)
static

Definition at line 1916 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1917 {
1918  PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n\n");
1919 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ locale_date_order()

static int locale_date_order ( const char *  locale)
static

Definition at line 2053 of file initdb.c.

References buf, DATEORDER_DMY, DATEORDER_MDY, DATEORDER_YMD, free, my_strftime(), pg_strdup(), setlocale, and tm.

Referenced by setup_config().

2054 {
2055  struct tm testtime;
2056  char buf[128];
2057  char *posD;
2058  char *posM;
2059  char *posY;
2060  char *save;
2061  size_t res;
2062  int result;
2063 
2064  result = DATEORDER_MDY; /* default */
2065 
2066  save = setlocale(LC_TIME, NULL);
2067  if (!save)
2068  return result;
2069  save = pg_strdup(save);
2070 
2071  setlocale(LC_TIME, locale);
2072 
2073  memset(&testtime, 0, sizeof(testtime));
2074  testtime.tm_mday = 22;
2075  testtime.tm_mon = 10; /* November, should come out as "11" */
2076  testtime.tm_year = 133; /* 2033 */
2077 
2078  res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2079 
2080  setlocale(LC_TIME, save);
2081  free(save);
2082 
2083  if (res == 0)
2084  return result;
2085 
2086  posM = strstr(buf, "11");
2087  posD = strstr(buf, "22");
2088  posY = strstr(buf, "33");
2089 
2090  if (!posM || !posD || !posY)
2091  return result;
2092 
2093  if (posY < posM && posM < posD)
2094  result = DATEORDER_YMD;
2095  else if (posD < posM)
2096  result = DATEORDER_DMY;
2097  else
2098  result = DATEORDER_MDY;
2099 
2100  return result;
2101 }
#define setlocale(a, b)
Definition: win32_port.h:436
static struct pg_tm tm
Definition: localtime.c:102
static char * buf
Definition: pg_test_fsync.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define DATEORDER_YMD
Definition: miscadmin.h:221
static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
Definition: initdb.c:2044
#define DATEORDER_DMY
Definition: miscadmin.h:222
#define DATEORDER_MDY
Definition: miscadmin.h:223
#define free(a)
Definition: header.h:65
static char * locale
Definition: initdb.c:126

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 2932 of file initdb.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), auth_methods_host, auth_methods_local, authmethodhost, authmethodlocal, authwarning, canonicalize_path(), check_authmethod_unspecified(), check_authmethod_valid(), check_need_password(), check_ok(), cleanup_directories_atexit(), createPQExpBuffer(), PQExpBufferData::data, data_checksums, debug, default_text_search_config, DEFAULT_XLOG_SEG_SIZE, destroyPQExpBuffer(), do_sync, encoding, fprintf, get_id(), get_parent_directory(), get_progname(), get_restricted_token(), get_su_pwd(), getopt_long(), initialize_data_directory(), IsValidWalSegSize, join_path_components(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, locale, MAXPGPATH, no_argument, noclean, noinstructions, optarg, optind, pg_check_dir(), pg_data, PG_DIR_MODE_GROUP, PG_IOLBF, pg_log_error, pg_log_warning, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, pgdata_native, printf, progname, pwfilename, pwprompt, required_argument, set_info_version(), set_pglocale_pgservice(), SetDataDirectoryCreatePerm(), setup_bin_paths(), setup_data_file_paths(), setup_locale_encoding(), setup_pgdata(), setup_text_search(), share_path, show_setting, generate_unaccent_rules::stdout, str_wal_segment_size_mb, strlcpy(), success, sync_only, usage(), username, wal_segment_size_mb, and xlog_dir.

2933 {
2934  static struct option long_options[] = {
2935  {"pgdata", required_argument, NULL, 'D'},
2936  {"encoding", required_argument, NULL, 'E'},
2937  {"locale", required_argument, NULL, 1},
2938  {"lc-collate", required_argument, NULL, 2},
2939  {"lc-ctype", required_argument, NULL, 3},
2940  {"lc-monetary", required_argument, NULL, 4},
2941  {"lc-numeric", required_argument, NULL, 5},
2942  {"lc-time", required_argument, NULL, 6},
2943  {"lc-messages", required_argument, NULL, 7},
2944  {"no-locale", no_argument, NULL, 8},
2945  {"text-search-config", required_argument, NULL, 'T'},
2946  {"auth", required_argument, NULL, 'A'},
2947  {"auth-local", required_argument, NULL, 10},
2948  {"auth-host", required_argument, NULL, 11},
2949  {"pwprompt", no_argument, NULL, 'W'},
2950  {"pwfile", required_argument, NULL, 9},
2951  {"username", required_argument, NULL, 'U'},
2952  {"help", no_argument, NULL, '?'},
2953  {"version", no_argument, NULL, 'V'},
2954  {"debug", no_argument, NULL, 'd'},
2955  {"show", no_argument, NULL, 's'},
2956  {"noclean", no_argument, NULL, 'n'}, /* for backwards compatibility */
2957  {"no-clean", no_argument, NULL, 'n'},
2958  {"nosync", no_argument, NULL, 'N'}, /* for backwards compatibility */
2959  {"no-sync", no_argument, NULL, 'N'},
2960  {"no-instructions", no_argument, NULL, 13},
2961  {"sync-only", no_argument, NULL, 'S'},
2962  {"waldir", required_argument, NULL, 'X'},
2963  {"wal-segsize", required_argument, NULL, 12},
2964  {"data-checksums", no_argument, NULL, 'k'},
2965  {"allow-group-access", no_argument, NULL, 'g'},
2966  {NULL, 0, NULL, 0}
2967  };
2968 
2969  /*
2970  * options with no short version return a low integer, the rest return
2971  * their short version value
2972  */
2973  int c;
2974  int option_index;
2975  char *effective_user;
2976  PQExpBuffer start_db_cmd;
2977  char pg_ctl_path[MAXPGPATH];
2978 
2979  /*
2980  * Ensure that buffering behavior of stdout matches what it is in
2981  * interactive usage (at least on most platforms). This prevents
2982  * unexpected output ordering when, eg, output is redirected to a file.
2983  * POSIX says we must do this before any other usage of these files.
2984  */
2985  setvbuf(stdout, NULL, PG_IOLBF, 0);
2986 
2987  pg_logging_init(argv[0]);
2988  progname = get_progname(argv[0]);
2989  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
2990 
2991  if (argc > 1)
2992  {
2993  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2994  {
2995  usage(progname);
2996  exit(0);
2997  }
2998  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2999  {
3000  puts("initdb (PostgreSQL) " PG_VERSION);
3001  exit(0);
3002  }
3003  }
3004 
3005  /* process command-line options */
3006 
3007  while ((c = getopt_long(argc, argv, "A:dD:E:gkL:nNsST:U:WX:", long_options, &option_index)) != -1)
3008  {
3009  switch (c)
3010  {
3011  case 'A':
3013 
3014  /*
3015  * When ident is specified, use peer for local connections.
3016  * Mirrored, when peer is specified, use ident for TCP/IP
3017  * connections.
3018  */
3019  if (strcmp(authmethodhost, "ident") == 0)
3020  authmethodlocal = "peer";
3021  else if (strcmp(authmethodlocal, "peer") == 0)
3022  authmethodhost = "ident";
3023  break;
3024  case 10:
3026  break;
3027  case 11:
3029  break;
3030  case 'D':
3032  break;
3033  case 'E':
3035  break;
3036  case 'W':
3037  pwprompt = true;
3038  break;
3039  case 'U':
3041  break;
3042  case 'd':
3043  debug = true;
3044  printf(_("Running in debug mode.\n"));
3045  break;
3046  case 'n':
3047  noclean = true;
3048  printf(_("Running in no-clean mode. Mistakes will not be cleaned up.\n"));
3049  break;
3050  case 'N':
3051  do_sync = false;
3052  break;
3053  case 'S':
3054  sync_only = true;
3055  break;
3056  case 'k':
3057  data_checksums = true;
3058  break;
3059  case 'L':
3061  break;
3062  case 1:
3063  locale = pg_strdup(optarg);
3064  break;
3065  case 2:
3067  break;
3068  case 3:
3070  break;
3071  case 4:
3073  break;
3074  case 5:
3076  break;
3077  case 6:
3079  break;
3080  case 7:
3082  break;
3083  case 8:
3084  locale = "C";
3085  break;
3086  case 9:
3088  break;
3089  case 's':
3090  show_setting = true;
3091  break;
3092  case 'T':
3094  break;
3095  case 'X':
3097  break;
3098  case 12:
3100  break;
3101  case 13:
3102  noinstructions = true;
3103  break;
3104  case 'g':
3106  break;
3107  default:
3108  /* getopt_long already emitted a complaint */
3109  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
3110  progname);
3111  exit(1);
3112  }
3113  }
3114 
3115 
3116  /*
3117  * Non-option argument specifies data directory as long as it wasn't
3118  * already specified with -D / --pgdata
3119  */
3120  if (optind < argc && !pg_data)
3121  {
3122  pg_data = pg_strdup(argv[optind]);
3123  optind++;
3124  }
3125 
3126  if (optind < argc)
3127  {
3128  pg_log_error("too many command-line arguments (first is \"%s\")",
3129  argv[optind]);
3130  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
3131  progname);
3132  exit(1);
3133  }
3134 
3136 
3137  /* If we only need to fsync, just do it and exit */
3138  if (sync_only)
3139  {
3140  setup_pgdata();
3141 
3142  /* must check that directory is readable */
3143  if (pg_check_dir(pg_data) <= 0)
3144  {
3145  pg_log_error("could not access directory \"%s\": %m", pg_data);
3146  exit(1);
3147  }
3148 
3149  fputs(_("syncing data to disk ... "), stdout);
3150  fflush(stdout);
3151  fsync_pgdata(pg_data, PG_VERSION_NUM);
3152  check_ok();
3153  return 0;
3154  }
3155 
3156  if (pwprompt && pwfilename)
3157  {
3158  pg_log_error("password prompt and password file cannot be specified together");
3159  exit(1);
3160  }
3161 
3164 
3167 
3169 
3170  /* set wal segment size */
3171  if (str_wal_segment_size_mb == NULL)
3172  wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024);
3173  else
3174  {
3175  char *endptr;
3176 
3177  /* check that the argument is a number */
3178  wal_segment_size_mb = strtol(str_wal_segment_size_mb, &endptr, 10);
3179 
3180  /* verify that wal segment size is valid */
3181  if (endptr == str_wal_segment_size_mb || *endptr != '\0')
3182  {
3183  pg_log_error("argument of --wal-segsize must be a number");
3184  exit(1);
3185  }
3186  if (!IsValidWalSegSize(wal_segment_size_mb * 1024 * 1024))
3187  {
3188  pg_log_error("argument of --wal-segsize must be a power of 2 between 1 and 1024");
3189  exit(1);
3190  }
3191  }
3192 
3194 
3195  setup_pgdata();
3196 
3197  setup_bin_paths(argv[0]);
3198 
3199  effective_user = get_id();
3200  if (!username)
3201  username = effective_user;
3202 
3203  if (strncmp(username, "pg_", 3) == 0)
3204  {
3205  pg_log_error("superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"", username);
3206  exit(1);
3207  }
3208 
3209  printf(_("The files belonging to this database system will be owned "
3210  "by user \"%s\".\n"
3211  "This user must also own the server process.\n\n"),
3212  effective_user);
3213 
3214  set_info_version();
3215 
3217 
3219 
3221 
3222  printf("\n");
3223 
3224  if (data_checksums)
3225  printf(_("Data page checksums are enabled.\n"));
3226  else
3227  printf(_("Data page checksums are disabled.\n"));
3228 
3229  if (pwprompt || pwfilename)
3230  get_su_pwd();
3231 
3232  printf("\n");
3233 
3235 
3236  if (do_sync)
3237  {
3238  fputs(_("syncing data to disk ... "), stdout);
3239  fflush(stdout);
3240  fsync_pgdata(pg_data, PG_VERSION_NUM);
3241  check_ok();
3242  }
3243  else
3244  printf(_("\nSync to disk skipped.\nThe data directory might become corrupt if the operating system crashes.\n"));
3245 
3246  if (authwarning)
3247  {
3248  printf("\n");
3249  pg_log_warning("enabling \"trust\" authentication for local connections");
3250  fprintf(stderr, _("You can change this by editing pg_hba.conf or using the option -A, or\n"
3251  "--auth-local and --auth-host, the next time you run initdb.\n"));
3252  }
3253 
3254  if (!noinstructions)
3255  {
3256  /*
3257  * Build up a shell command to tell the user how to start the server
3258  */
3259  start_db_cmd = createPQExpBuffer();
3260 
3261  /* Get directory specification used to start initdb ... */
3262  strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
3263  canonicalize_path(pg_ctl_path);
3264  get_parent_directory(pg_ctl_path);
3265  /* ... and tag on pg_ctl instead */
3266  join_path_components(pg_ctl_path, pg_ctl_path, "pg_ctl");
3267 
3268  /* path to pg_ctl, properly quoted */
3269  appendShellString(start_db_cmd, pg_ctl_path);
3270 
3271  /* add -D switch, with properly quoted data directory */
3272  appendPQExpBufferStr(start_db_cmd, " -D ");
3273  appendShellString(start_db_cmd, pgdata_native);
3274 
3275  /* add suggested -l switch and "start" command */
3276  /* translator: This is a placeholder in a shell command. */
3277  appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
3278 
3279  printf(_("\nSuccess. You can now start the database server using:\n\n"
3280  " %s\n\n"),
3281  start_db_cmd->data);
3282 
3283  destroyPQExpBuffer(start_db_cmd);
3284 
3285  printf(_("\nSuccess.\n"));
3286  }
3287 
3288 
3289  success = true;
3290  return 0;
3291 }
static char * lc_monetary
Definition: initdb.c:129
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:94
static void cleanup_directories_atexit(void)
Definition: initdb.c:545
void setup_data_file_paths(void)
Definition: initdb.c:2528
void setup_locale_encoding(void)
Definition: initdb.c:2444
static bool debug
Definition: initdb.c:140
static char * lc_collate
Definition: initdb.c:127
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:80
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void get_restricted_token(void)
void pg_logging_init(const char *argv0)
Definition: logging.c:81
#define DEFAULT_XLOG_SEG_SIZE
void canonicalize_path(char *path)
Definition: path.c:254
#define printf(...)
Definition: port.h:221
static bool pwprompt
Definition: initdb.c:135
static char * pgdata_native
Definition: initdb.c:172
static void get_su_pwd(void)
Definition: initdb.c:1450
#define fprintf
Definition: port.h:219
static char * get_id(void)
Definition: initdb.c:598
static char * share_path
Definition: initdb.c:121
static const char * authmethodlocal
Definition: initdb.c:139
static char * lc_time
Definition: initdb.c:131
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static void check_authmethod_unspecified(const char **authmethod)
Definition: initdb.c:2311
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
static bool authwarning
Definition: initdb.c:188
static char * lc_messages
Definition: initdb.c:132
#define MAXPGPATH
static const char * default_text_search_config
Definition: initdb.c:133
static char * lc_ctype
Definition: initdb.c:128
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * lc_numeric
Definition: initdb.c:130
char * c
void initialize_data_directory(void)
Definition: initdb.c:2820
static int wal_segment_size_mb
Definition: initdb.c:149
void get_parent_directory(char *path)
Definition: path.c:854
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * pg_data
Definition: initdb.c:124
static char * pwfilename
Definition: initdb.c:136
static void check_ok(void)
Definition: initdb.c:2019
void setup_bin_paths(const char *argv0)
Definition: initdb.c:2399
static const char * progname
Definition: initdb.c:153
#define no_argument
Definition: getopt_long.h:24
static char * xlog_dir
Definition: initdb.c:147
#define PG_DIR_MODE_GROUP
Definition: file_perm.h:35
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1203
static bool data_checksums
Definition: initdb.c:146
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define PG_IOLBF
Definition: port.h:343
static char * username
Definition: initdb.c:134
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
static char * encoding
Definition: initdb.c:125
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void setup_text_search(void)
Definition: initdb.c:2568
static char * str_wal_segment_size_mb
Definition: initdb.c:148
static bool do_sync
Definition: initdb.c:143
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:218
static const char *const auth_methods_host[]
Definition: initdb.c:82
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:31
static void usage(const char *progname)
Definition: initdb.c:2267
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
static void check_need_password(const char *authmethodlocal, const char *authmethodhost)
Definition: initdb.c:2341
static char * locale
Definition: initdb.c:126
static const char * authmethodhost
Definition: initdb.c:138
static bool sync_only
Definition: initdb.c:144
#define pg_log_warning(...)
Definition: pgfnames.c:24
static const char *const auth_methods_local[]
Definition: initdb.c:104
static bool success
Definition: initdb.c:163
static void check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
Definition: initdb.c:2321
void setup_pgdata(void)
Definition: initdb.c:2358
#define _(x)
Definition: elog.c:89
static void set_info_version(void)
Definition: initdb.c:1856
static bool show_setting
Definition: initdb.c:145
static bool noinstructions
Definition: initdb.c:142
static bool noclean
Definition: initdb.c:141

◆ make_postgres()

static void make_postgres ( FILE *  cmdfd)
static

Definition at line 1973 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1974 {
1975  const char *const *line;
1976  static const char *const postgres_setup[] = {
1977  "CREATE DATABASE postgres;\n\n",
1978  "COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
1979  NULL
1980  };
1981 
1982  for (line = postgres_setup; *line; line++)
1983  PG_CMD_PUTS(*line);
1984 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ make_template0()

static void make_template0 ( FILE *  cmdfd)
static

Definition at line 1935 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1936 {
1937  const char *const *line;
1938  static const char *const template0_setup[] = {
1939  "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
1940 
1941  /*
1942  * We use the OID of template0 to determine datlastsysoid
1943  */
1944  "UPDATE pg_database SET datlastsysoid = "
1945  " (SELECT oid FROM pg_database "
1946  " WHERE datname = 'template0');\n\n",
1947 
1948  /*
1949  * Explicitly revoke public create-schema and create-temp-table
1950  * privileges in template1 and template0; else the latter would be on
1951  * by default
1952  */
1953  "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n",
1954  "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n",
1955 
1956  "COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n",
1957 
1958  /*
1959  * Finally vacuum to clean up dead rows in pg_database
1960  */
1961  "VACUUM pg_database;\n\n",
1962  NULL
1963  };
1964 
1965  for (line = template0_setup; *line; line++)
1966  PG_CMD_PUTS(*line);
1967 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ my_strftime()

static size_t my_strftime ( char *  s,
size_t  max,
const char *  fmt,
const struct tm tm 
)
inlinestatic

Definition at line 2044 of file initdb.c.

Referenced by locale_date_order().

2045 {
2046  return strftime(s, max, fmt, tm);
2047 }
static struct pg_tm tm
Definition: localtime.c:102

◆ popen_check()

static FILE * popen_check ( const char *  command,
const char *  mode 
)
static

Definition at line 527 of file initdb.c.

References pg_log_error, and generate_unaccent_rules::stdout.

528 {
529  FILE *cmdfd;
530 
531  fflush(stdout);
532  fflush(stderr);
533  errno = 0;
534  cmdfd = popen(command, mode);
535  if (cmdfd == NULL)
536  pg_log_error("could not execute command \"%s\": %m", command);
537  return cmdfd;
538 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define pg_log_error(...)
Definition: logging.h:80

◆ pretty_wal_size()

static char* pretty_wal_size ( int  segment_count)
static

Definition at line 1024 of file initdb.c.

References pg_malloc(), snprintf, and wal_segment_size_mb.

Referenced by setup_config().

1025 {
1026  int sz = wal_segment_size_mb * segment_count;
1027  char *result = pg_malloc(14);
1028 
1029  if ((sz % 1024) == 0)
1030  snprintf(result, 14, "%dGB", sz / 1024);
1031  else
1032  snprintf(result, 14, "%dMB", sz);
1033 
1034  return result;
1035 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static int wal_segment_size_mb
Definition: initdb.c:149
#define snprintf
Definition: port.h:215

◆ readfile()

static char ** readfile ( const char *  path)
static

Definition at line 450 of file initdb.c.

References StringInfoData::data, infile(), initStringInfo(), pfree(), pg_get_line_buf(), pg_log_error, pg_malloc(), pg_realloc(), and pg_strdup().

Referenced by bootstrap_template1(), setup_config(), setup_dictionary(), setup_schema(), and setup_sysviews().

451 {
452  char **result;
453  FILE *infile;
454  StringInfoData line;
455  int maxlines;
456  int n;
457 
458  if ((infile = fopen(path, "r")) == NULL)
459  {
460  pg_log_error("could not open file \"%s\" for reading: %m", path);
461  exit(1);
462  }
463 
464  initStringInfo(&line);
465 
466  maxlines = 1024;
467  result = (char **) pg_malloc(maxlines * sizeof(char *));
468 
469  n = 0;
470  while (pg_get_line_buf(infile, &line))
471  {
472  /* make sure there will be room for a trailing NULL pointer */
473  if (n >= maxlines - 1)
474  {
475  maxlines *= 2;
476  result = (char **) pg_realloc(result, maxlines * sizeof(char *));
477  }
478 
479  result[n++] = pg_strdup(line.data);
480  }
481  result[n] = NULL;
482 
483  pfree(line.data);
484 
485  fclose(infile);
486 
487  return result;
488 }
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:88
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define pg_log_error(...)
Definition: logging.h:80
void pfree(void *pointer)
Definition: mcxt.c:1057
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
static void infile(const char *filename)
Definition: zic.c:1241

◆ replace_token()

static char ** replace_token ( char **  lines,
const char *  token,
const char *  replacement 
)
static

Definition at line 367 of file initdb.c.

References i, newline(), and pg_malloc().

Referenced by bootstrap_template1(), setup_config(), and setup_privileges().

368 {
369  int numlines = 1;
370  int i;
371  char **result;
372  int toklen,
373  replen,
374  diff;
375 
376  for (i = 0; lines[i]; i++)
377  numlines++;
378 
379  result = (char **) pg_malloc(numlines * sizeof(char *));
380 
381  toklen = strlen(token);
382  replen = strlen(replacement);
383  diff = replen - toklen;
384 
385  for (i = 0; i < numlines; i++)
386  {
387  char *where;
388  char *newline;
389  int pre;
390 
391  /* just copy pointer if NULL or no change needed */
392  if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
393  {
394  result[i] = lines[i];
395  continue;
396  }
397 
398  /* if we get here a change is needed - set up new line */
399 
400  newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
401 
402  pre = where - lines[i];
403 
404  memcpy(newline, lines[i], pre);
405 
406  memcpy(newline + pre, replacement, replen);
407 
408  strcpy(newline + pre + replen, lines[i] + pre + toklen);
409 
410  result[i] = newline;
411  }
412 
413  return result;
414 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static chr newline(void)
Definition: regc_lex.c:1138
int i

◆ select_default_timezone()

const char* select_default_timezone ( const char *  share_path)

Definition at line 1701 of file findtimezone.c.

References identify_system_timezone(), snprintf, tzdirpath, and validate_zone().

Referenced by test_config_settings().

1702 {
1703  const char *tzname;
1704 
1705  /* Initialize timezone directory path, if needed */
1706 #ifndef SYSTEMTZDIR
1707  snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1708 #endif
1709 
1710  /* Check TZ environment variable */
1711  tzname = getenv("TZ");
1712  if (validate_zone(tzname))
1713  return tzname;
1714 
1715  /* Nope, so try to identify the system timezone */
1716  tzname = identify_system_timezone();
1717  if (validate_zone(tzname))
1718  return tzname;
1719 
1720  return NULL;
1721 }
static bool validate_zone(const char *tzname)
static char * share_path
Definition: initdb.c:121
static char tzdirpath[MAXPGPATH]
Definition: findtimezone.c:27
static const char * identify_system_timezone(void)
Definition: findtimezone.c:330
#define snprintf
Definition: port.h:215

◆ set_info_version()

static void set_info_version ( void  )
static

Definition at line 1856 of file initdb.c.

References infoversion, pg_strdup(), and snprintf.

Referenced by main().

1857 {
1858  char *letterversion;
1859  long major = 0,
1860  minor = 0,
1861  micro = 0;
1862  char *endptr;
1863  char *vstr = pg_strdup(PG_VERSION);
1864  char *ptr;
1865 
1866  ptr = vstr + (strlen(vstr) - 1);
1867  while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1868  ptr--;
1869  letterversion = ptr + 1;
1870  major = strtol(vstr, &endptr, 10);
1871  if (*endptr)
1872  minor = strtol(endptr + 1, &endptr, 10);
1873  if (*endptr)
1874  micro = strtol(endptr + 1, &endptr, 10);
1875  snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1876  major, minor, micro, letterversion);
1877 }
static char infoversion[100]
Definition: initdb.c:168
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define snprintf
Definition: port.h:215

◆ set_input()

static void set_input ( char **  dest,
const char *  filename 
)
static

Definition at line 762 of file initdb.c.

References psprintf(), and share_path.

Referenced by setup_data_file_paths().

763 {
764  *dest = psprintf("%s/%s", share_path, filename);
765 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * share_path
Definition: initdb.c:121
static char * filename
Definition: pg_dumpall.c:91

◆ set_null_conf()

static void set_null_conf ( void  )
static

Definition at line 837 of file initdb.c.

References conf_file, free, PG_BINARY_W, pg_data, pg_log_error, and psprintf().

Referenced by initialize_data_directory().

838 {
839  FILE *conf_file;
840  char *path;
841 
842  path = psprintf("%s/postgresql.conf", pg_data);
843  conf_file = fopen(path, PG_BINARY_W);
844  if (conf_file == NULL)
845  {
846  pg_log_error("could not open file \"%s\" for writing: %m", path);
847  exit(1);
848  }
849  if (fclose(conf_file))
850  {
851  pg_log_error("could not write file \"%s\": %m", path);
852  exit(1);
853  }
854  free(path);
855 }
static char * conf_file
Definition: initdb.c:158
#define pg_log_error(...)
Definition: logging.h:80
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_BINARY_W
Definition: c.h:1262
static char * pg_data
Definition: initdb.c:124
#define free(a)
Definition: header.h:65

◆ setlocales()

static void setlocales ( void  )
static

Definition at line 2216 of file initdb.c.

References check_locale_name(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, and locale.

Referenced by setup_locale_encoding().

2217 {
2218  char *canonname;
2219 
2220  /* set empty lc_* values to locale config if set */
2221 
2222  if (locale)
2223  {
2224  if (!lc_ctype)
2225  lc_ctype = locale;
2226  if (!lc_collate)
2227  lc_collate = locale;
2228  if (!lc_numeric)
2229  lc_numeric = locale;
2230  if (!lc_time)
2231  lc_time = locale;
2232  if (!lc_monetary)
2233  lc_monetary = locale;
2234  if (!lc_messages)
2235  lc_messages = locale;
2236  }
2237 
2238  /*
2239  * canonicalize locale names, and obtain any missing values from our
2240  * current environment
2241  */
2242 
2243  check_locale_name(LC_CTYPE, lc_ctype, &canonname);
2244  lc_ctype = canonname;
2245  check_locale_name(LC_COLLATE, lc_collate, &canonname);
2246  lc_collate = canonname;
2247  check_locale_name(LC_NUMERIC, lc_numeric, &canonname);
2248  lc_numeric = canonname;
2249  check_locale_name(LC_TIME, lc_time, &canonname);
2250  lc_time = canonname;
2251  check_locale_name(LC_MONETARY, lc_monetary, &canonname);
2252  lc_monetary = canonname;
2253 #if defined(LC_MESSAGES) && !defined(WIN32)
2254  check_locale_name(LC_MESSAGES, lc_messages, &canonname);
2255  lc_messages = canonname;
2256 #else
2257  /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2258  check_locale_name(LC_CTYPE, lc_messages, &canonname);
2259  lc_messages = canonname;
2260 #endif
2261 }
static char * lc_monetary
Definition: initdb.c:129
static char * lc_collate
Definition: initdb.c:127
static char * lc_time
Definition: initdb.c:131
static char * lc_messages
Definition: initdb.c:132
static char * lc_ctype
Definition: initdb.c:128
static char * lc_numeric
Definition: initdb.c:130
static void check_locale_name(int category, const char *locale, char **canonname)
Definition: initdb.c:2116
static char * locale
Definition: initdb.c:126

◆ setup_auth()

static void setup_auth ( FILE *  cmdfd)
static

Definition at line 1426 of file initdb.c.

References escape_quotes(), PG_CMD_PRINTF, PG_CMD_PUTS, superuser_password, and username.

Referenced by initialize_data_directory().

1427 {
1428  const char *const *line;
1429  static const char *const pg_authid_setup[] = {
1430  /*
1431  * The authid table shouldn't be readable except through views, to
1432  * ensure passwords are not publicly visible.
1433  */
1434  "REVOKE ALL on pg_authid FROM public;\n\n",
1435  NULL
1436  };
1437 
1438  for (line = pg_authid_setup; *line != NULL; line++)
1439  PG_CMD_PUTS(*line);
1440 
1441  if (superuser_password)
1442  PG_CMD_PRINTF("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1444 }
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:310
#define PG_CMD_PUTS(line)
Definition: initdb.c:304
static char * username
Definition: initdb.c:134
static char * superuser_password
Definition: initdb.c:137
static char * escape_quotes(const char *src)
Definition: initdb.c:321

◆ setup_bin_paths()

void setup_bin_paths ( const char *  argv0)

Definition at line 2399 of file initdb.c.

References backend_exec, bin_path, canonicalize_path(), find_my_exec(), find_other_exec(), get_share_path(), is_absolute_path, last_dir_separator(), MAXPGPATH, PG_BACKEND_VERSIONSTR, pg_log_error, pg_malloc(), progname, share_path, and strlcpy().

Referenced by main().

2400 {
2401  int ret;
2402 
2403  if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
2404  backend_exec)) < 0)
2405  {
2406  char full_path[MAXPGPATH];
2407 
2408  if (find_my_exec(argv0, full_path) < 0)
2409  strlcpy(full_path, progname, sizeof(full_path));
2410 
2411  if (ret == -1)
2412  pg_log_error("The program \"%s\" is needed by %s but was not found in the\n"
2413  "same directory as \"%s\".\n"
2414  "Check your installation.",
2415  "postgres", progname, full_path);
2416  else
2417  pg_log_error("The program \"%s\" was found by \"%s\"\n"
2418  "but was not the same version as %s.\n"
2419  "Check your installation.",
2420  "postgres", full_path, progname);
2421  exit(1);
2422  }
2423 
2424  /* store binary directory */
2425  strcpy(bin_path, backend_exec);
2426  *last_dir_separator(bin_path) = '\0';
2428 
2429  if (!share_path)
2430  {
2433  }
2434  else if (!is_absolute_path(share_path))
2435  {
2436  pg_log_error("input file location must be an absolute path");
2437  exit(1);
2438  }
2439 
2441 }
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:323
static char * argv0
Definition: pg_ctl.c:97
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define pg_log_error(...)
Definition: logging.h:80
void canonicalize_path(char *path)
Definition: path.c:254
static char * share_path
Definition: initdb.c:121
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:128
#define is_absolute_path(filename)
Definition: port.h:86
static const char * progname
Definition: initdb.c:153
char * last_dir_separator(const char *filename)
Definition: path.c:138
#define PG_BACKEND_VERSIONSTR
Definition: port.h:134
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static char bin_path[MAXPGPATH]
Definition: initdb.c:230
static char backend_exec[MAXPGPATH]
Definition: initdb.c:231
void get_share_path(const char *my_exec_path, char *ret_path)
Definition: path.c:704

◆ setup_collation()

static void setup_collation ( FILE *  cmdfd)
static

Definition at line 1649 of file initdb.c.

References PG_CMD_PRINTF, PG_CMD_PUTS, and PG_UTF8.

Referenced by initialize_data_directory().

1650 {
1651  /*
1652  * Add an SQL-standard name. We don't want to pin this, so it doesn't go
1653  * in pg_collation.h. But add it before reading system collations, so
1654  * that it wins if libc defines a locale named ucs_basic.
1655  */
1656  PG_CMD_PRINTF("INSERT INTO pg_collation (oid, collname, collnamespace, collowner, collprovider, collisdeterministic, collencoding, collcollate, collctype)"
1657  "VALUES (pg_nextoid('pg_catalog.pg_collation', 'oid', 'pg_catalog.pg_collation_oid_index'), 'ucs_basic', 'pg_catalog'::regnamespace, %u, '%c', true, %d, 'C', 'C');\n\n",
1658  BOOTSTRAP_SUPERUSERID, COLLPROVIDER_LIBC, PG_UTF8);
1659 
1660  /* Now import all collations we can find in the operating system */
1661  PG_CMD_PUTS("SELECT pg_import_system_collations('pg_catalog');\n\n");
1662 }
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:310
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ setup_config()

static void setup_config ( void  )
static

Definition at line 1041 of file initdb.c.

References _, addrinfo::ai_addr, addrinfo::ai_addrlen, addrinfo::ai_canonname, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_NUMERICHOST, addrinfo::ai_protocol, addrinfo::ai_socktype, authmethodhost, authmethodlocal, AUTHTRUST_WARNING, check_ok(), conf_file, DATEORDER_DMY, DATEORDER_MDY, DATEORDER_YMD, DEFAULT_BACKEND_FLUSH_AFTER, DEFAULT_BGWRITER_FLUSH_AFTER, DEFAULT_CHECKPOINT_FLUSH_AFTER, DEFAULT_MAX_WAL_SEGS, DEFAULT_MIN_WAL_SEGS, DEFAULT_PGSOCKET_DIR, default_text_search_config, default_timezone, dynamic_shared_memory_type, escape_quotes(), filter_lines_with_token(), free, getaddrinfo, hba_file, ident_file, lc_messages, lc_monetary, lc_numeric, lc_time, locale_date_order(), MAXPGPATH, n_buffers, n_connections, pg_data, pg_dir_create_mode, PG_DIR_MODE_GROUP, pg_file_create_mode, pg_log_error, pg_strdup(), pretty_wal_size(), readfile(), replace_token(), snprintf, sprintf, generate_unaccent_rules::stdout, and writefile().

Referenced by initialize_data_directory().

1042 {
1043  char **conflines;
1044  char repltok[MAXPGPATH];
1045  char path[MAXPGPATH];
1046  char *autoconflines[3];
1047 
1048  fputs(_("creating configuration files ... "), stdout);
1049  fflush(stdout);
1050 
1051  /* postgresql.conf */
1052 
1053  conflines = readfile(conf_file);
1054 
1055  snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
1056  conflines = replace_token(conflines, "#max_connections = 100", repltok);
1057 
1058  if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1059  snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
1060  (n_buffers * (BLCKSZ / 1024)) / 1024);
1061  else
1062  snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
1063  n_buffers * (BLCKSZ / 1024));
1064  conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
1065 
1066 #ifdef HAVE_UNIX_SOCKETS
1067  snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'",
1069 #else
1070  snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''");
1071 #endif
1072  conflines = replace_token(conflines, "#unix_socket_directories = '/tmp'",
1073  repltok);
1074 
1075 #if DEF_PGPORT != 5432
1076  snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
1077  conflines = replace_token(conflines, "#port = 5432", repltok);
1078 #endif
1079 
1080  /* set default max_wal_size and min_wal_size */
1081  snprintf(repltok, sizeof(repltok), "min_wal_size = %s",
1083  conflines = replace_token(conflines, "#min_wal_size = 80MB", repltok);
1084 
1085  snprintf(repltok, sizeof(repltok), "max_wal_size = %s",
1087  conflines = replace_token(conflines, "#max_wal_size = 1GB", repltok);
1088 
1089  snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
1091  conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
1092 
1093  snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
1095  conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
1096 
1097  snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
1099  conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
1100 
1101  snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
1103  conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
1104 
1105  switch (locale_date_order(lc_time))
1106  {
1107  case DATEORDER_YMD:
1108  strcpy(repltok, "datestyle = 'iso, ymd'");
1109  break;
1110  case DATEORDER_DMY:
1111  strcpy(repltok, "datestyle = 'iso, dmy'");
1112  break;
1113  case DATEORDER_MDY:
1114  default:
1115  strcpy(repltok, "datestyle = 'iso, mdy'");
1116  break;
1117  }
1118  conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
1119 
1120  snprintf(repltok, sizeof(repltok),
1121  "default_text_search_config = 'pg_catalog.%s'",
1123  conflines = replace_token(conflines,
1124  "#default_text_search_config = 'pg_catalog.simple'",
1125  repltok);
1126 
1127  if (default_timezone)
1128  {
1129  snprintf(repltok, sizeof(repltok), "timezone = '%s'",
1131  conflines = replace_token(conflines, "#timezone = 'GMT'", repltok);
1132  snprintf(repltok, sizeof(repltok), "log_timezone = '%s'",
1134  conflines = replace_token(conflines, "#log_timezone = 'GMT'", repltok);
1135  }
1136 
1137  snprintf(repltok, sizeof(repltok), "dynamic_shared_memory_type = %s",
1139  conflines = replace_token(conflines, "#dynamic_shared_memory_type = posix",
1140  repltok);
1141 
1142 #if DEFAULT_BACKEND_FLUSH_AFTER > 0
1143  snprintf(repltok, sizeof(repltok), "#backend_flush_after = %dkB",
1144  DEFAULT_BACKEND_FLUSH_AFTER * (BLCKSZ / 1024));
1145  conflines = replace_token(conflines, "#backend_flush_after = 0",
1146  repltok);
1147 #endif
1148 
1149 #if DEFAULT_BGWRITER_FLUSH_AFTER > 0
1150  snprintf(repltok, sizeof(repltok), "#bgwriter_flush_after = %dkB",
1151  DEFAULT_BGWRITER_FLUSH_AFTER * (BLCKSZ / 1024));
1152  conflines = replace_token(conflines, "#bgwriter_flush_after = 0",
1153  repltok);
1154 #endif
1155 
1156 #if DEFAULT_CHECKPOINT_FLUSH_AFTER > 0
1157  snprintf(repltok, sizeof(repltok), "#checkpoint_flush_after = %dkB",
1158  DEFAULT_CHECKPOINT_FLUSH_AFTER * (BLCKSZ / 1024));
1159  conflines = replace_token(conflines, "#checkpoint_flush_after = 0",
1160  repltok);
1161 #endif
1162 
1163 #ifndef USE_PREFETCH
1164  conflines = replace_token(conflines,
1165  "#effective_io_concurrency = 1",
1166  "#effective_io_concurrency = 0");
1167 #endif
1168 
1169 #ifdef WIN32
1170  conflines = replace_token(conflines,
1171  "#update_process_title = on",
1172  "#update_process_title = off");
1173 #endif
1174 
1175  /*
1176  * Change password_encryption setting to md5 if md5 was chosen as an
1177  * authentication method, unless scram-sha-256 was also chosen.
1178  */
1179  if ((strcmp(authmethodlocal, "md5") == 0 &&
1180  strcmp(authmethodhost, "scram-sha-256") != 0) ||
1181  (strcmp(authmethodhost, "md5") == 0 &&
1182  strcmp(authmethodlocal, "scram-sha-256") != 0))
1183  {
1184  conflines = replace_token(conflines,
1185  "#password_encryption = scram-sha-256",
1186  "password_encryption = md5");
1187  }
1188 
1189  /*
1190  * If group access has been enabled for the cluster then it makes sense to
1191  * ensure that the log files also allow group access. Otherwise a backup
1192  * from a user in the group would fail if the log files were not
1193  * relocated.
1194  */
1196  {
1197  conflines = replace_token(conflines,
1198  "#log_file_mode = 0600",
1199  "log_file_mode = 0640");
1200  }
1201 
1202  snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1203 
1204  writefile(path, conflines);
1205  if (chmod(path, pg_file_create_mode) != 0)
1206  {
1207  pg_log_error("could not change permissions of \"%s\": %m", path);
1208  exit(1);
1209  }
1210 
1211  /*
1212  * create the automatic configuration file to store the configuration
1213  * parameters set by ALTER SYSTEM command. The parameters present in this
1214  * file will override the value of parameters that exists before parse of
1215  * this file.
1216  */
1217  autoconflines[0] = pg_strdup("# Do not edit this file manually!\n");
1218  autoconflines[1] = pg_strdup("# It will be overwritten by the ALTER SYSTEM command.\n");
1219  autoconflines[2] = NULL;
1220 
1221  sprintf(path, "%s/postgresql.auto.conf", pg_data);
1222 
1223  writefile(path, autoconflines);
1224  if (chmod(path, pg_file_create_mode) != 0)
1225  {
1226  pg_log_error("could not change permissions of \"%s\": %m", path);
1227  exit(1);
1228  }
1229 
1230  free(conflines);
1231 
1232 
1233  /* pg_hba.conf */
1234 
1235  conflines = readfile(hba_file);
1236 
1237 #ifndef HAVE_UNIX_SOCKETS
1238  conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
1239 #else
1240  conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
1241 #endif
1242 
1243 #ifdef HAVE_IPV6
1244 
1245  /*
1246  * Probe to see if there is really any platform support for IPv6, and
1247  * comment out the relevant pg_hba line if not. This avoids runtime
1248  * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1249  * useful on Windows, where executables built on a machine with IPv6 may
1250  * have to run on a machine without.
1251  */
1252  {
1253  struct addrinfo *gai_result;
1254  struct addrinfo hints;
1255  int err = 0;
1256 
1257 #ifdef WIN32
1258  /* need to call WSAStartup before calling getaddrinfo */
1259  WSADATA wsaData;
1260 
1261  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1262 #endif
1263 
1264  /* for best results, this code should match parse_hba_line() */
1265  hints.ai_flags = AI_NUMERICHOST;
1266  hints.ai_family = AF_UNSPEC;
1267  hints.ai_socktype = 0;
1268  hints.ai_protocol = 0;
1269  hints.ai_addrlen = 0;
1270  hints.ai_canonname = NULL;
1271  hints.ai_addr = NULL;
1272  hints.ai_next = NULL;
1273 
1274  if (err != 0 ||
1275  getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1276  {
1277  conflines = replace_token(conflines,
1278  "host all all ::1",
1279  "#host all all ::1");
1280  conflines = replace_token(conflines,
1281  "host replication all ::1",
1282  "#host replication all ::1");
1283  }
1284  }
1285 #else /* !HAVE_IPV6 */
1286  /* If we didn't compile IPV6 support at all, always comment it out */
1287  conflines = replace_token(conflines,
1288  "host all all ::1",
1289  "#host all all ::1");
1290  conflines = replace_token(conflines,
1291  "host replication all ::1",
1292  "#host replication all ::1");
1293 #endif /* HAVE_IPV6 */
1294 
1295  /* Replace default authentication methods */
1296  conflines = replace_token(conflines,
1297  "@authmethodhost@",
1298  authmethodhost);
1299  conflines = replace_token(conflines,
1300  "@authmethodlocal@",
1301  authmethodlocal);
1302 
1303  conflines = replace_token(conflines,
1304  "@authcomment@",
1305  (strcmp(authmethodlocal, "trust") == 0 || strcmp(authmethodhost, "trust") == 0) ? AUTHTRUST_WARNING : "");
1306 
1307  snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1308 
1309  writefile(path, conflines);
1310  if (chmod(path, pg_file_create_mode) != 0)
1311  {
1312  pg_log_error("could not change permissions of \"%s\": %m", path);
1313  exit(1);
1314  }
1315 
1316  free(conflines);
1317 
1318  /* pg_ident.conf */
1319 
1320  conflines = readfile(ident_file);
1321 
1322  snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1323 
1324  writefile(path, conflines);
1325  if (chmod(path, pg_file_create_mode) != 0)
1326  {
1327  pg_log_error("could not change permissions of \"%s\": %m", path);
1328  exit(1);
1329  }
1330 
1331  free(conflines);
1332 
1333  check_ok();
1334 }
static const char * default_timezone
Definition: initdb.c:178
static char * lc_monetary
Definition: initdb.c:129
#define getaddrinfo
Definition: getaddrinfo.h:136
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:367
int pg_file_create_mode
Definition: file_perm.c:19
#define DEFAULT_CHECKPOINT_FLUSH_AFTER
static char * conf_file
Definition: initdb.c:158
#define pg_log_error(...)
Definition: logging.h:80
#define DEFAULT_BGWRITER_FLUSH_AFTER
#define AUTHTRUST_WARNING
Definition: initdb.c:183
#define AI_NUMERICHOST
Definition: getaddrinfo.h:73
static const char * authmethodlocal
Definition: initdb.c:139
static char * lc_time
Definition: initdb.c:131
#define sprintf
Definition: port.h:217
static int n_connections
Definition: initdb.c:175
static char * lc_messages
Definition: initdb.c:132
#define MAXPGPATH
static const char * default_text_search_config
Definition: initdb.c:133
static char * lc_numeric
Definition: initdb.c:130
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * pg_data
Definition: initdb.c:124
static void check_ok(void)
Definition: initdb.c:2019
#define DEFAULT_MAX_WAL_SEGS
Definition: xlog_internal.h:90
#define DATEORDER_YMD
Definition: miscadmin.h:221
int pg_dir_create_mode
Definition: file_perm.c:18
#define PG_DIR_MODE_GROUP
Definition: file_perm.h:35
static int locale_date_order(const char *locale)
Definition: initdb.c:2053
#define DATEORDER_DMY
Definition: miscadmin.h:222
static char * pretty_wal_size(int segment_count)
Definition: initdb.c:1024
#define DATEORDER_MDY
Definition: miscadmin.h:223
#define free(a)
Definition: header.h:65
static char ** filter_lines_with_token(char **lines, const char *token)
Definition: initdb.c:423
static int n_buffers
Definition: initdb.c:176
static char * escape_quotes(const char *src)
Definition: initdb.c:321
static const char * dynamic_shared_memory_type
Definition: initdb.c:177
static void writefile(char *path, char **lines)
Definition: initdb.c:497
static char * ident_file
Definition: initdb.c:157
static char ** readfile(const char *path)
Definition: initdb.c:450
static const char * authmethodhost
Definition: initdb.c:138
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:89
#define DEFAULT_MIN_WAL_SEGS
Definition: xlog_internal.h:89
static char * hba_file
Definition: initdb.c:156
#define DEFAULT_BACKEND_FLUSH_AFTER
#define DEFAULT_PGSOCKET_DIR

◆ setup_data_file_paths()

void setup_data_file_paths ( void  )

Definition at line 2528 of file initdb.c.

References bin_path, bki_file, check_input(), conf_file, debug, dictionary_file, features_file, fprintf, hba_file, ident_file, info_schema_file, pg_data, set_input(), share_path, show_setting, system_views_file, and username.

Referenced by main().

2529 {
2530  set_input(&bki_file, "postgres.bki");
2531  set_input(&hba_file, "pg_hba.conf.sample");
2532  set_input(&ident_file, "pg_ident.conf.sample");
2533  set_input(&conf_file, "postgresql.conf.sample");
2534  set_input(&dictionary_file, "snowball_create.sql");
2535  set_input(&info_schema_file, "information_schema.sql");
2536  set_input(&features_file, "sql_features.txt");
2537  set_input(&system_views_file, "system_views.sql");
2538 
2539  if (show_setting || debug)
2540  {
2541  fprintf(stderr,
2542  "VERSION=%s\n"
2543  "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2544  "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2545  "POSTGRESQL_CONF_SAMPLE=%s\n"
2546  "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2547  PG_VERSION,
2549  username, bki_file,
2550  conf_file,
2551  hba_file, ident_file);
2552  if (show_setting)
2553  exit(0);
2554  }
2555 
2564 }
static bool debug
Definition: initdb.c:140
static char * system_views_file
Definition: initdb.c:162
static char * conf_file
Definition: initdb.c:158
static char * features_file
Definition: initdb.c:161
#define fprintf
Definition: port.h:219
static char * share_path
Definition: initdb.c:121
static void check_input(char *path)
Definition: initdb.c:771
static char * pg_data
Definition: initdb.c:124
static char * dictionary_file
Definition: initdb.c:159
static char * info_schema_file
Definition: initdb.c:160
static char * username
Definition: initdb.c:134
static char bin_path[MAXPGPATH]
Definition: initdb.c:230
static char * ident_file
Definition: initdb.c:157
static char * bki_file
Definition: initdb.c:155
static char * hba_file
Definition: initdb.c:156
static bool show_setting
Definition: initdb.c:145
static void set_input(char **dest, const char *filename)
Definition: initdb.c:762

◆ setup_depend()

static void setup_depend ( FILE *  cmdfd)
static

Definition at line 1513 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1514 {
1515  const char *const *line;
1516  static const char *const pg_depend_setup[] = {
1517  /*
1518  * Make PIN entries in pg_depend for all objects made so far in the
1519  * tables that the dependency code handles. This is overkill (the
1520  * system doesn't really depend on having every last weird datatype,
1521  * for instance) but generating only the minimum required set of
1522  * dependencies seems hard.
1523  *
1524  * Catalogs that are intentionally not scanned here are:
1525  *
1526  * pg_database: it's a feature, not a bug, that template1 is not
1527  * pinned.
1528  *
1529  * pg_extension: a pinned extension isn't really an extension, hmm?
1530  *
1531  * pg_tablespace: tablespaces don't participate in the dependency
1532  * code, and DropTableSpace() explicitly protects the built-in
1533  * tablespaces.
1534  *
1535  * First delete any already-made entries; PINs override all else, and
1536  * must be the only entries for their objects.
1537  */
1538  "DELETE FROM pg_depend;\n\n",
1539  "VACUUM pg_depend;\n\n",
1540  "DELETE FROM pg_shdepend;\n\n",
1541  "VACUUM pg_shdepend;\n\n",
1542 
1543  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1544  " FROM pg_class;\n\n",
1545  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1546  " FROM pg_proc;\n\n",
1547  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1548  " FROM pg_type;\n\n",
1549  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1550  " FROM pg_cast;\n\n",
1551  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1552  " FROM pg_constraint;\n\n",
1553  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1554  " FROM pg_conversion;\n\n",
1555  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1556  " FROM pg_attrdef;\n\n",
1557  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1558  " FROM pg_language;\n\n",
1559  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1560  " FROM pg_operator;\n\n",
1561  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1562  " FROM pg_opclass;\n\n",
1563  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1564  " FROM pg_opfamily;\n\n",
1565  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1566  " FROM pg_am;\n\n",
1567  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1568  " FROM pg_amop;\n\n",
1569  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1570  " FROM pg_amproc;\n\n",
1571  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1572  " FROM pg_rewrite;\n\n",
1573  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1574  " FROM pg_trigger;\n\n",
1575 
1576  /*
1577  * restriction here to avoid pinning the public namespace
1578  */
1579  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1580  " FROM pg_namespace "
1581  " WHERE nspname LIKE 'pg%';\n\n",
1582 
1583  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1584  " FROM pg_ts_parser;\n\n",
1585  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1586  " FROM pg_ts_dict;\n\n",
1587  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1588  " FROM pg_ts_template;\n\n",
1589  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1590  " FROM pg_ts_config;\n\n",
1591  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1592  " FROM pg_collation;\n\n",
1593  "INSERT INTO pg_shdepend SELECT 0,0,0,0, tableoid,oid, 'p' "
1594  " FROM pg_authid;\n\n",
1595  NULL
1596  };
1597 
1598  for (line = pg_depend_setup; *line != NULL; line++)
1599  PG_CMD_PUTS(*line);
1600 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ setup_description()

static void setup_description ( FILE *  cmdfd)
static

Definition at line 1628 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1629 {
1630  /* Create default descriptions for operator implementation functions */
1631  PG_CMD_PUTS("WITH funcdescs AS ( "
1632  "SELECT p.oid as p_oid, o.oid as o_oid, oprname "
1633  "FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
1634  "INSERT INTO pg_description "
1635  " SELECT p_oid, 'pg_proc'::regclass, 0, "
1636  " 'implementation of ' || oprname || ' operator' "
1637  " FROM funcdescs "
1638  " WHERE NOT EXISTS (SELECT 1 FROM pg_description "
1639  " WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass) "
1640  " AND NOT EXISTS (SELECT 1 FROM pg_description "
1641  " WHERE objoid = o_oid AND classoid = 'pg_operator'::regclass"
1642  " AND description LIKE 'deprecated%');\n\n");
1643 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ setup_dictionary()

static void setup_dictionary ( FILE *  cmdfd)
static

Definition at line 1668 of file initdb.c.

References dictionary_file, free, PG_CMD_PUTS, and readfile().

Referenced by initialize_data_directory().

1669 {
1670  char **line;
1671  char **conv_lines;
1672 
1673  conv_lines = readfile(dictionary_file);
1674  for (line = conv_lines; *line != NULL; line++)
1675  {
1676  PG_CMD_PUTS(*line);
1677  free(*line);
1678  }
1679 
1680  PG_CMD_PUTS("\n\n");
1681 
1682  free(conv_lines);
1683 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304
static char * dictionary_file
Definition: initdb.c:159
#define free(a)
Definition: header.h:65
static char ** readfile(const char *path)
Definition: initdb.c:450

◆ setup_locale_encoding()

void setup_locale_encoding ( void  )

Definition at line 2444 of file initdb.c.

References _, check_locale_encoding(), encoding, encodingid, fprintf, get_encoding_id(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, pg_encoding_to_char(), pg_get_encoding_from_locale(), pg_log_error, PG_UTF8, pg_valid_server_encoding_id(), printf, progname, and setlocales().

Referenced by main().

2445 {
2446  setlocales();
2447 
2448  if (strcmp(lc_ctype, lc_collate) == 0 &&
2449  strcmp(lc_ctype, lc_time) == 0 &&
2450  strcmp(lc_ctype, lc_numeric) == 0 &&
2451  strcmp(lc_ctype, lc_monetary) == 0 &&
2452  strcmp(lc_ctype, lc_messages) == 0)
2453  printf(_("The database cluster will be initialized with locale \"%s\".\n"), lc_ctype);
2454  else
2455  {
2456  printf(_("The database cluster will be initialized with locales\n"
2457  " COLLATE: %s\n"
2458  " CTYPE: %s\n"
2459  " MESSAGES: %s\n"
2460  " MONETARY: %s\n"
2461  " NUMERIC: %s\n"
2462  " TIME: %s\n"),
2463  lc_collate,
2464  lc_ctype,
2465  lc_messages,
2466  lc_monetary,
2467  lc_numeric,
2468  lc_time);
2469  }
2470 
2471  if (!encoding)
2472  {
2473  int ctype_enc;
2474 
2475  ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);
2476 
2477  if (ctype_enc == -1)
2478  {
2479  /* Couldn't recognize the locale's codeset */
2480  pg_log_error("could not find suitable encoding for locale \"%s\"",
2481  lc_ctype);
2482  fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
2483  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2484  progname);
2485  exit(1);
2486  }
2487  else if (!pg_valid_server_encoding_id(ctype_enc))
2488  {
2489  /*
2490  * We recognized it, but it's not a legal server encoding. On
2491  * Windows, UTF-8 works with any locale, so we can fall back to
2492  * UTF-8.
2493  */
2494 #ifdef WIN32
2495  encodingid = PG_UTF8;
2496  printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
2497  "The default database encoding will be set to \"%s\" instead.\n"),
2498  pg_encoding_to_char(ctype_enc),
2500 #else
2501  pg_log_error("locale \"%s\" requires unsupported encoding \"%s\"",
2502  lc_ctype, pg_encoding_to_char(ctype_enc));
2503  fprintf(stderr,
2504  _("Encoding \"%s\" is not allowed as a server-side encoding.\n"
2505  "Rerun %s with a different locale selection.\n"),
2506  pg_encoding_to_char(ctype_enc), progname);
2507  exit(1);
2508 #endif
2509  }
2510  else
2511  {
2512  encodingid = ctype_enc;
2513  printf(_("The default database encoding has accordingly been set to \"%s\".\n"),
2515  }
2516  }
2517  else
2519 
2522  exit(1); /* check_locale_encoding printed the error */
2523 
2524 }
static int encodingid
Definition: initdb.c:154
static char * lc_monetary
Definition: initdb.c:129
static char * lc_collate
Definition: initdb.c:127
#define pg_log_error(...)
Definition: logging.h:80
int pg_valid_server_encoding_id(int encoding)
Definition: encnames.c:514
#define printf(...)
Definition: port.h:221
#define fprintf
Definition: port.h:219
static int get_encoding_id(const char *encoding_name)
Definition: initdb.c:631
static char * lc_time
Definition: initdb.c:131
static char * lc_messages
Definition: initdb.c:132
static bool check_locale_encoding(const char *locale, int encoding)
Definition: initdb.c:2180
static char * lc_ctype
Definition: initdb.c:128
static char * lc_numeric
Definition: initdb.c:130
static const char * progname
Definition: initdb.c:153
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:452
static char * encoding
Definition: initdb.c:125
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
static void setlocales(void)
Definition: initdb.c:2216
#define _(x)
Definition: elog.c:89

◆ setup_pgdata()

void setup_pgdata ( void  )

Definition at line 2358 of file initdb.c.

References _, canonicalize_path(), fprintf, pg_data, pg_log_error, pg_strdup(), pgdata_native, and setenv.

Referenced by main().

2359 {
2360  char *pgdata_get_env;
2361 
2362  if (!pg_data)
2363  {
2364  pgdata_get_env = getenv("PGDATA");
2365  if (pgdata_get_env && strlen(pgdata_get_env))
2366  {
2367  /* PGDATA found */
2368  pg_data = pg_strdup(pgdata_get_env);
2369  }
2370  else
2371  {
2372  pg_log_error("no data directory specified");
2373  fprintf(stderr,
2374  _("You must identify the directory where the data for this database system\n"
2375  "will reside. Do this with either the invocation option -D or the\n"
2376  "environment variable PGDATA.\n"));
2377  exit(1);
2378  }
2379  }
2380 
2383 
2384  /*
2385  * we have to set PGDATA for postgres rather than pass it on the command
2386  * line to avoid dumb quoting problems on Windows, and we would especially
2387  * need quotes otherwise on Windows because paths there are most likely to
2388  * have embedded spaces.
2389  */
2390  if (setenv("PGDATA", pg_data, 1) != 0)
2391  {
2392  pg_log_error("could not set environment");
2393  exit(1);
2394  }
2395 }
#define pg_log_error(...)
Definition: logging.h:80
void canonicalize_path(char *path)
Definition: path.c:254
static char * pgdata_native
Definition: initdb.c:172
#define fprintf
Definition: port.h:219
#define setenv(x, y, z)
Definition: win32_port.h:497
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * pg_data
Definition: initdb.c:124
#define _(x)
Definition: elog.c:89

◆ setup_privileges()

static void setup_privileges ( FILE *  cmdfd)
static

Definition at line 1705 of file initdb.c.

References CppAsString2, escape_quotes(), PG_CMD_PUTS, replace_token(), and username.

Referenced by initialize_data_directory().

1706 {
1707  char **line;
1708  char **priv_lines;
1709  static char *privileges_setup[] = {
1710  "UPDATE pg_class "
1711  " SET relacl = (SELECT array_agg(a.acl) FROM "
1712  " (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl "
1713  " UNION SELECT unnest(pg_catalog.acldefault("
1714  " CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) " THEN 's' "
1715  " ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
1716  " ) as a) "
1717  " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1718  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1719  CppAsString2(RELKIND_SEQUENCE) ")"
1720  " AND relacl IS NULL;\n\n",
1721  "GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n\n",
1722  "GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n\n",
1723  "REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n",
1724  "INSERT INTO pg_init_privs "
1725  " (objoid, classoid, objsubid, initprivs, privtype)"
1726  " SELECT"
1727  " oid,"
1728  " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1729  " 0,"
1730  " relacl,"
1731  " 'i'"
1732  " FROM"
1733  " pg_class"
1734  " WHERE"
1735  " relacl IS NOT NULL"
1736  " AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1737  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1738  CppAsString2(RELKIND_SEQUENCE) ");\n\n",
1739  "INSERT INTO pg_init_privs "
1740  " (objoid, classoid, objsubid, initprivs, privtype)"
1741  " SELECT"
1742  " pg_class.oid,"
1743  " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1744  " pg_attribute.attnum,"
1745  " pg_attribute.attacl,"
1746  " 'i'"
1747  " FROM"
1748  " pg_class"
1749  " JOIN pg_attribute ON (pg_class.oid = pg_attribute.attrelid)"
1750  " WHERE"
1751  " pg_attribute.attacl IS NOT NULL"
1752  " AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1753  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1754  CppAsString2(RELKIND_SEQUENCE) ");\n\n",
1755  "INSERT INTO pg_init_privs "
1756  " (objoid, classoid, objsubid, initprivs, privtype)"
1757  " SELECT"
1758  " oid,"
1759  " (SELECT oid FROM pg_class WHERE relname = 'pg_proc'),"
1760  " 0,"
1761  " proacl,"
1762  " 'i'"
1763  " FROM"
1764  " pg_proc"
1765  " WHERE"
1766  " proacl IS NOT NULL;\n\n",
1767  "INSERT INTO pg_init_privs "
1768  " (objoid, classoid, objsubid, initprivs, privtype)"
1769  " SELECT"
1770  " oid,"
1771  " (SELECT oid FROM pg_class WHERE relname = 'pg_type'),"
1772  " 0,"
1773  " typacl,"
1774  " 'i'"
1775  " FROM"
1776  " pg_type"
1777  " WHERE"
1778  " typacl IS NOT NULL;\n\n",
1779  "INSERT INTO pg_init_privs "
1780  " (objoid, classoid, objsubid, initprivs, privtype)"
1781  " SELECT"
1782  " oid,"
1783  " (SELECT oid FROM pg_class WHERE relname = 'pg_language'),"
1784  " 0,"
1785  " lanacl,"
1786  " 'i'"
1787  " FROM"
1788  " pg_language"
1789  " WHERE"
1790  " lanacl IS NOT NULL;\n\n",
1791  "INSERT INTO pg_init_privs "
1792  " (objoid, classoid, objsubid, initprivs, privtype)"
1793  " SELECT"
1794  " oid,"
1795  " (SELECT oid FROM pg_class WHERE "
1796  " relname = 'pg_largeobject_metadata'),"
1797  " 0,"
1798  " lomacl,"
1799  " 'i'"
1800  " FROM"
1801  " pg_largeobject_metadata"
1802  " WHERE"
1803  " lomacl IS NOT NULL;\n\n",
1804  "INSERT INTO pg_init_privs "
1805  " (objoid, classoid, objsubid, initprivs, privtype)"
1806  " SELECT"
1807  " oid,"
1808  " (SELECT oid FROM pg_class WHERE relname = 'pg_namespace'),"
1809  " 0,"
1810  " nspacl,"
1811  " 'i'"
1812  " FROM"
1813  " pg_namespace"
1814  " WHERE"
1815  " nspacl IS NOT NULL;\n\n",
1816  "INSERT INTO pg_init_privs "
1817  " (objoid, classoid, objsubid, initprivs, privtype)"
1818  " SELECT"
1819  " oid,"
1820  " (SELECT oid FROM pg_class WHERE "
1821  " relname = 'pg_foreign_data_wrapper'),"
1822  " 0,"
1823  " fdwacl,"
1824  " 'i'"
1825  " FROM"
1826  " pg_foreign_data_wrapper"
1827  " WHERE"
1828  " fdwacl IS NOT NULL;\n\n",
1829  "INSERT INTO pg_init_privs "
1830  " (objoid, classoid, objsubid, initprivs, privtype)"
1831  " SELECT"
1832  " oid,"
1833  " (SELECT oid FROM pg_class "
1834  " WHERE relname = 'pg_foreign_server'),"
1835  " 0,"
1836  " srvacl,"
1837  " 'i'"
1838  " FROM"
1839  " pg_foreign_server"
1840  " WHERE"
1841  " srvacl IS NOT NULL;\n\n",
1842  NULL
1843  };
1844 
1845  priv_lines = replace_token(privileges_setup, "$POSTGRES_SUPERUSERNAME",
1847  for (line = priv_lines; *line != NULL; line++)
1848  PG_CMD_PUTS(*line);
1849 }
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:367
#define PG_CMD_PUTS(line)
Definition: initdb.c:304
#define CppAsString2(x)
Definition: c.h:277
static char * username
Definition: initdb.c:134
static char * escape_quotes(const char *src)
Definition: initdb.c:321

◆ setup_schema()

static void setup_schema ( FILE *  cmdfd)
static

Definition at line 1883 of file initdb.c.

References escape_quotes(), features_file, free, info_schema_file, infoversion, PG_CMD_PRINTF, PG_CMD_PUTS, and readfile().

Referenced by initialize_data_directory().

1884 {
1885  char **line;
1886  char **lines;
1887 
1888  lines = readfile(info_schema_file);
1889 
1890  for (line = lines; *line != NULL; line++)
1891  {
1892  PG_CMD_PUTS(*line);
1893  free(*line);
1894  }
1895 
1896  PG_CMD_PUTS("\n\n");
1897 
1898  free(lines);
1899 
1900  PG_CMD_PRINTF("UPDATE information_schema.sql_implementation_info "
1901  " SET character_value = '%s' "
1902  " WHERE implementation_info_name = 'DBMS VERSION';\n\n",
1903  infoversion);
1904 
1905  PG_CMD_PRINTF("COPY information_schema.sql_features "
1906  " (feature_id, feature_name, sub_feature_id, "
1907  " sub_feature_name, is_supported, comments) "
1908  " FROM E'%s';\n\n",
1910 }
static char infoversion[100]
Definition: initdb.c:168
static char * features_file
Definition: initdb.c:161
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:310
#define PG_CMD_PUTS(line)
Definition: initdb.c:304
static char * info_schema_file
Definition: initdb.c:160
#define free(a)
Definition: header.h:65
static char * escape_quotes(const char *src)
Definition: initdb.c:321
static char ** readfile(const char *path)
Definition: initdb.c:450

◆ setup_signals()

void setup_signals ( void  )

Definition at line 2603 of file initdb.c.

References pqsignal(), SIG_IGN, SIGHUP, SIGPIPE, SIGQUIT, and trapsig().

Referenced by initialize_data_directory().

2604 {
2605  /* some of these are not valid on Windows */
2606 #ifdef SIGHUP
2608 #endif
2609 #ifdef SIGINT
2610  pqsignal(SIGINT, trapsig);
2611 #endif
2612 #ifdef SIGQUIT
2614 #endif
2615 #ifdef SIGTERM
2616  pqsignal(SIGTERM, trapsig);
2617 #endif
2618 
2619  /* Ignore SIGPIPE when writing to backend, so we can clean up */
2620 #ifdef SIGPIPE
2622 #endif
2623 
2624  /* Prevent SIGSYS so we can probe for kernel calls that might not work */
2625 #ifdef SIGSYS
2626  pqsignal(SIGSYS, SIG_IGN);
2627 #endif
2628 }
#define SIGQUIT
Definition: win32_port.h:160
#define SIGPIPE
Definition: win32_port.h:164
#define SIGHUP
Definition: win32_port.h:159
#define SIG_IGN
Definition: win32_port.h:156
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static void trapsig(int signum)
Definition: initdb.c:2008

◆ setup_sysviews()

static void setup_sysviews ( FILE *  cmdfd)
static

Definition at line 1606 of file initdb.c.

References free, PG_CMD_PUTS, readfile(), and system_views_file.

Referenced by initialize_data_directory().

1607 {
1608  char **line;
1609  char **sysviews_setup;
1610 
1611  sysviews_setup = readfile(system_views_file);
1612 
1613  for (line = sysviews_setup; *line != NULL; line++)
1614  {
1615  PG_CMD_PUTS(*line);
1616  free(*line);
1617  }
1618 
1619  PG_CMD_PUTS("\n\n");
1620 
1621  free(sysviews_setup);
1622 }
static char * system_views_file
Definition: initdb.c:162
#define PG_CMD_PUTS(line)
Definition: initdb.c:304
#define free(a)
Definition: header.h:65
static char ** readfile(const char *path)
Definition: initdb.c:450

◆ setup_text_search()

void setup_text_search ( void  )

Definition at line 2568 of file initdb.c.

References _, default_text_search_config, find_matching_ts_config(), lc_ctype, pg_log_info, pg_log_warning, and printf.

Referenced by main().

2569 {
2571  {
2574  {
2575  pg_log_info("could not find suitable text search configuration for locale \"%s\"",
2576  lc_ctype);
2577  default_text_search_config = "simple";
2578  }
2579  }
2580  else
2581  {
2582  const char *checkmatch = find_matching_ts_config(lc_ctype);
2583 
2584  if (checkmatch == NULL)
2585  {
2586  pg_log_warning("suitable text search configuration for locale \"%s\" is unknown",
2587  lc_ctype);
2588  }
2589  else if (strcmp(checkmatch, default_text_search_config) != 0)
2590  {
2591  pg_log_warning("specified text search configuration \"%s\" might not match locale \"%s\"",
2593  }
2594  }
2595 
2596  printf(_("The default text search configuration will be set to \"%s\".\n"),
2598 
2599 }
static const char * find_matching_ts_config(const char *lc_type)
Definition: initdb.c:716
#define printf(...)
Definition: port.h:221
static const char * default_text_search_config
Definition: initdb.c:133
static char * lc_ctype
Definition: initdb.c:128
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define _(x)
Definition: elog.c:89
#define pg_log_info(...)
Definition: logging.h:88

◆ test_config_settings()

static void test_config_settings ( void  )
static

Definition at line 910 of file initdb.c.

References _, backend_exec, boot_options, choose_dsm_implementation(), default_timezone, DEVNULL, dynamic_shared_memory_type, i, MAXPGPATH, MIN_BUFS_FOR_CONNS, n_buffers, n_connections, printf, select_default_timezone(), share_path, snprintf, status(), and generate_unaccent_rules::stdout.

Referenced by initialize_data_directory().

911 {
912  /*
913  * This macro defines the minimum shared_buffers we want for a given
914  * max_connections value. The arrays show the settings to try.
915  */
916 #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
917 
918  static const int trial_conns[] = {
919  100, 50, 40, 30, 20
920  };
921  static const int trial_bufs[] = {
922  16384, 8192, 4096, 3584, 3072, 2560, 2048, 1536,
923  1000, 900, 800, 700, 600, 500,
924  400, 300, 200, 100, 50
925  };
926 
927  char cmd[MAXPGPATH];
928  const int connslen = sizeof(trial_conns) / sizeof(int);
929  const int bufslen = sizeof(trial_bufs) / sizeof(int);
930  int i,
931  status,
932  test_conns,
933  test_buffs,
934  ok_buffers = 0;
935 
936  /*
937  * Need to determine working DSM implementation first so that subsequent
938  * tests don't fail because DSM setting doesn't work.
939  */
940  printf(_("selecting dynamic shared memory implementation ... "));
941  fflush(stdout);
944 
945  /*
946  * Probe for max_connections before shared_buffers, since it is subject to
947  * more constraints than shared_buffers.
948  */
949  printf(_("selecting default max_connections ... "));
950  fflush(stdout);
951 
952  for (i = 0; i < connslen; i++)
953  {
954  test_conns = trial_conns[i];
955  test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
956 
957  snprintf(cmd, sizeof(cmd),
958  "\"%s\" --boot -x0 %s "
959  "-c max_connections=%d "
960  "-c shared_buffers=%d "
961  "-c dynamic_shared_memory_type=%s "
962  "< \"%s\" > \"%s\" 2>&1",
964  test_conns, test_buffs,
966  DEVNULL, DEVNULL);
967  status = system(cmd);
968  if (status == 0)
969  {
970  ok_buffers = test_buffs;
971  break;
972  }
973  }
974  if (i >= connslen)
975  i = connslen - 1;
976  n_connections = trial_conns[i];
977 
978  printf("%d\n", n_connections);
979 
980  printf(_("selecting default shared_buffers ... "));
981  fflush(stdout);
982 
983  for (i = 0; i < bufslen; i++)
984  {
985  /* Use same amount of memory, independent of BLCKSZ */
986  test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
987  if (test_buffs <= ok_buffers)
988  {
989  test_buffs = ok_buffers;
990  break;
991  }
992 
993  snprintf(cmd, sizeof(cmd),
994  "\"%s\" --boot -x0 %s "
995  "-c max_connections=%d "
996  "-c shared_buffers=%d "
997  "-c dynamic_shared_memory_type=%s "
998  "< \"%s\" > \"%s\" 2>&1",
1000  n_connections, test_buffs,
1002  DEVNULL, DEVNULL);
1003  status = system(cmd);
1004  if (status == 0)
1005  break;
1006  }
1007  n_buffers = test_buffs;
1008 
1009  if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1010  printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
1011  else
1012  printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
1013 
1014  printf(_("selecting default time zone ... "));
1015  fflush(stdout);
1017  printf("%s\n", default_timezone ? default_timezone : "GMT");
1018 }
static const char * default_timezone
Definition: initdb.c:178
#define MIN_BUFS_FOR_CONNS(nconns)
#define printf(...)
Definition: port.h:221
static const char * choose_dsm_implementation(void)
Definition: initdb.c:869
static char * share_path
Definition: initdb.c:121
static int n_connections
Definition: initdb.c:175
#define MAXPGPATH
#define DEVNULL
Definition: port.h:146
const char * select_default_timezone(const char *share_path)
static const char * boot_options
Definition: initdb.c:200
static int n_buffers
Definition: initdb.c:176
static const char * dynamic_shared_memory_type
Definition: initdb.c:177
int i
static char backend_exec[MAXPGPATH]
Definition: initdb.c:231
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define snprintf
Definition: port.h:215
#define _(x)
Definition: elog.c:89

◆ trapsig()

static void trapsig ( int  signum)
static

Definition at line 2008 of file initdb.c.

References caught_signal, and pqsignal().

Referenced by setup_signals().

2009 {
2010  /* handle systems that reset the handler, like Windows (grr) */
2011  pqsignal(signum, trapsig);
2012  caught_signal = true;
2013 }
static bool caught_signal
Definition: initdb.c:169
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static void trapsig(int signum)
Definition: initdb.c:2008

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 2267 of file initdb.c.

References _, and printf.

Referenced by main().

2268 {
2269  printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2270  printf(_("Usage:\n"));
2271  printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2272  printf(_("\nOptions:\n"));
2273  printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2274  printf(_(" --auth-host=METHOD default authentication method for local TCP/IP connections\n"));
2275  printf(_(" --auth-local=METHOD default authentication method for local-socket connections\n"));
2276  printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2277  printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2278  printf(_(" -g, --allow-group-access allow group read/execute on data directory\n"));
2279  printf(_(" -k, --data-checksums use data page checksums\n"));
2280  printf(_(" --locale=LOCALE set default locale for new databases\n"));
2281  printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2282  " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2283  " set default locale in the respective category for\n"
2284  " new databases (default taken from environment)\n"));
2285  printf(_(" --no-locale equivalent to --locale=C\n"));
2286  printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2287  printf(_(" -T, --text-search-config=CFG\n"
2288  " default text search configuration\n"));
2289  printf(_(" -U, --username=NAME database superuser name\n"));
2290  printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2291  printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
2292  printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
2293  printf(_("\nLess commonly used options:\n"));
2294  printf(_(" -d, --debug generate lots of debugging output\n"));
2295  printf(_(" -L DIRECTORY where to find the input files\n"));
2296  printf(_(" -n, --no-clean do not clean up after errors\n"));
2297  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
2298  printf(_(" --no-instructions do not print instructions for next steps\n"));
2299  printf(_(" -s, --show show internal settings\n"));
2300  printf(_(" -S, --sync-only only sync data directory\n"));
2301  printf(_("\nOther options:\n"));
2302  printf(_(" -V, --version output version information, then exit\n"));
2303  printf(_(" -?, --help show this help, then exit\n"));
2304  printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2305  "is used.\n"));
2306  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2307  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2308 }
#define printf(...)
Definition: port.h:221
static const char * progname
Definition: initdb.c:153
#define _(x)
Definition: elog.c:89

◆ vacuum_db()

static void vacuum_db ( FILE *  cmdfd)
static

Definition at line 1925 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1926 {
1927  /* Run analyze before VACUUM so the statistics are frozen. */
1928  PG_CMD_PUTS("ANALYZE;\n\nVACUUM FREEZE;\n\n");
1929 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:304

◆ warn_on_mount_point()

void warn_on_mount_point ( int  error)

Definition at line 2804 of file initdb.c.

References _, and fprintf.

Referenced by create_data_directory(), and create_xlog_or_symlink().

2805 {
2806  if (error == 2)
2807  fprintf(stderr,
2808  _("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.\n"));
2809  else if (error == 3)
2810  fprintf(stderr,
2811  _("It contains a lost+found directory, perhaps due to it being a mount point.\n"));
2812 
2813  fprintf(stderr,
2814  _("Using a mount point directly as the data directory is not recommended.\n"
2815  "Create a subdirectory under the mount point.\n"));
2816 }
static void error(void)
Definition: sql-dyntest.c:147
#define fprintf
Definition: port.h:219
#define _(x)
Definition: elog.c:89

◆ write_version_file()

static void write_version_file ( const char *  extrapath)
static

Definition at line 808 of file initdb.c.

References fprintf, free, PG_BINARY_W, pg_data, pg_log_error, psprintf(), and version_file.

Referenced by initialize_data_directory().

809 {
810  FILE *version_file;
811  char *path;
812 
813  if (extrapath == NULL)
814  path = psprintf("%s/PG_VERSION", pg_data);
815  else
816  path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
817 
818  if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
819  {
820  pg_log_error("could not open file \"%s\" for writing: %m", path);
821  exit(1);
822  }
823  if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
824  fclose(version_file))
825  {
826  pg_log_error("could not write file \"%s\": %m", path);
827  exit(1);
828  }
829  free(path);
830 }
#define pg_log_error(...)
Definition: logging.h:80
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_BINARY_W
Definition: c.h:1262
#define fprintf
Definition: port.h:219
static char * pg_data
Definition: initdb.c:124
#define free(a)
Definition: header.h:65
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:102

◆ writefile()

static void writefile ( char *  path,
char **  lines 
)
static

Definition at line 497 of file initdb.c.

References free, and pg_log_error.

Referenced by setup_config().

498 {
499  FILE *out_file;
500  char **line;
501 
502  if ((out_file = fopen(path, "w")) == NULL)
503  {
504  pg_log_error("could not open file \"%s\" for writing: %m", path);
505  exit(1);
506  }
507  for (line = lines; *line != NULL; line++)
508  {
509  if (fputs(*line, out_file) < 0)
510  {
511  pg_log_error("could not write file \"%s\": %m", path);
512  exit(1);
513  }
514  free(*line);
515  }
516  if (fclose(out_file))
517  {
518  pg_log_error("could not write file \"%s\": %m", path);
519  exit(1);
520  }
521 }
#define pg_log_error(...)
Definition: logging.h:80
#define free(a)
Definition: header.h:65

Variable Documentation

◆ auth_methods_host

const char* const auth_methods_host[]
static
Initial value:
= {
"trust", "reject", "scram-sha-256", "md5", "password", "ident", "radius",
NULL
}

Definition at line 82 of file initdb.c.

Referenced by main().

◆ auth_methods_local

const char* const auth_methods_local[]
static
Initial value:
= {
"trust", "reject", "scram-sha-256", "md5", "password", "peer", "radius",
NULL
}

Definition at line 104 of file initdb.c.

Referenced by main().

◆ authmethodhost

const char* authmethodhost = NULL
static

Definition at line 138 of file initdb.c.

Referenced by main(), and setup_config().

◆ authmethodlocal

const char* authmethodlocal = NULL
static

Definition at line 139 of file initdb.c.

Referenced by main(), and setup_config().

◆ authwarning

bool authwarning = false
static

Definition at line 188 of file initdb.c.

Referenced by check_authmethod_unspecified(), and main().

◆ backend_exec

char backend_exec[MAXPGPATH]
static

◆ backend_options

const char* backend_options = "--single -F -O -j -c search_path=pg_catalog -c exit_on_error=true"
static

Definition at line 201 of file initdb.c.

Referenced by initialize_data_directory().

◆ bin_path

char bin_path[MAXPGPATH]
static

Definition at line 230 of file initdb.c.

Referenced by setup_bin_paths(), and setup_data_file_paths().

◆ bki_file

char* bki_file
static

Definition at line 155 of file initdb.c.

Referenced by bootstrap_template1(), and setup_data_file_paths().

◆ boot_options

const char* boot_options = "-F"
static

Definition at line 200 of file initdb.c.

Referenced by bootstrap_template1(), and test_config_settings().

◆ caught_signal

bool caught_signal = false
static

Definition at line 169 of file initdb.c.

Referenced by check_ok(), and trapsig().

◆ conf_file

char* conf_file
static

Definition at line 158 of file initdb.c.

Referenced by set_null_conf(), setup_config(), and setup_data_file_paths().

◆ data_checksums

bool data_checksums = false
static

Definition at line 146 of file initdb.c.

Referenced by bootstrap_template1(), and main().

◆ debug

bool debug = false
static

Definition at line 140 of file initdb.c.

Referenced by bootstrap_template1(), main(), and setup_data_file_paths().

◆ default_text_search_config

const char* default_text_search_config = NULL
static

Definition at line 133 of file initdb.c.

Referenced by main(), setup_config(), and setup_text_search().

◆ default_timezone

const char* default_timezone = NULL
static

Definition at line 178 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ dictionary_file

char* dictionary_file
static

Definition at line 159 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_dictionary().

◆ do_sync

bool do_sync = true
static

Definition at line 143 of file initdb.c.

Referenced by main(), and sync_target_dir().

◆ dynamic_shared_memory_type

const char* dynamic_shared_memory_type = NULL
static

Definition at line 177 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ encoding

char* encoding = NULL
static

Definition at line 125 of file initdb.c.

Referenced by main(), and setup_locale_encoding().

◆ encodingid

int encodingid
static

Definition at line 154 of file initdb.c.

Referenced by bootstrap_template1(), and setup_locale_encoding().

◆ features_file

char* features_file
static

Definition at line 161 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_schema().

◆ found_existing_pgdata

bool found_existing_pgdata = false
static

Definition at line 165 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_data_directory().

◆ found_existing_xlogdir

bool found_existing_xlogdir = false
static

Definition at line 167 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_xlog_or_symlink().

◆ hba_file

char* hba_file
static

Definition at line 156 of file initdb.c.

Referenced by setup_config(), and setup_data_file_paths().

◆ ident_file

char* ident_file
static

Definition at line 157 of file initdb.c.

Referenced by setup_config(), and setup_data_file_paths().

◆ info_schema_file

char* info_schema_file
static

Definition at line 160 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_schema().

◆ infoversion

char infoversion[100]
static

Definition at line 168 of file initdb.c.

Referenced by set_info_version(), and setup_schema().

◆ lc_collate

char* lc_collate = NULL
static

◆ lc_ctype

char* lc_ctype = NULL
static

◆ lc_messages

char* lc_messages = NULL
static

Definition at line 132 of file initdb.c.

Referenced by get_control_data(), main(), setlocales(), setup_config(), and setup_locale_encoding().

◆ lc_monetary

char* lc_monetary = NULL
static

Definition at line 129 of file initdb.c.

Referenced by get_control_data(), main(), setlocales(), setup_config(), and setup_locale_encoding().

◆ lc_numeric

char* lc_numeric = NULL
static

Definition at line 130 of file initdb.c.

Referenced by get_control_data(), main(), setlocales(), setup_config(), and setup_locale_encoding().

◆ lc_time

char* lc_time = NULL
static

Definition at line 131 of file initdb.c.

Referenced by get_control_data(), main(), setlocales(), setup_config(), and setup_locale_encoding().

◆ locale

◆ made_new_pgdata

bool made_new_pgdata = false
static

Definition at line 164 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_data_directory().

◆ made_new_xlogdir

bool made_new_xlogdir = false
static

Definition at line 166 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_xlog_or_symlink().

◆ n_buffers

int n_buffers = 50
static

Definition at line 176 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ n_connections

int n_connections = 10
static

Definition at line 175 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ noclean

bool noclean = false
static

Definition at line 141 of file initdb.c.

Referenced by cleanup_directories_atexit(), and main().

◆ noinstructions

bool noinstructions = false
static

Definition at line 142 of file initdb.c.

Referenced by main().

◆ output_errno

int output_errno = 0
static

Definition at line 171 of file initdb.c.

Referenced by check_ok().

◆ output_failed

bool output_failed = false
static

Definition at line 170 of file initdb.c.

Referenced by check_ok().

◆ pg_data

◆ pgdata_native

char* pgdata_native
static

Definition at line 172 of file initdb.c.

Referenced by main(), and setup_pgdata().

◆ progname

const char* progname
static

◆ pwfilename

char* pwfilename = NULL
static

Definition at line 136 of file initdb.c.

Referenced by check_need_password(), get_su_pwd(), and main().

◆ pwprompt

bool pwprompt = false
static

Definition at line 135 of file initdb.c.

Referenced by check_need_password(), get_su_pwd(), and main().

◆ share_path

char* share_path = NULL
static

◆ show_setting

bool show_setting = false
static

Definition at line 145 of file initdb.c.

Referenced by main(), and setup_data_file_paths().

◆ str_wal_segment_size_mb

char* str_wal_segment_size_mb = NULL
static

Definition at line 148 of file initdb.c.

Referenced by main().

◆ subdirs

const char* const subdirs[]
static
Initial value:
= {
"global",
"pg_wal/archive_status",
"pg_commit_ts",
"pg_dynshmem",
"pg_notify",
"pg_serial",
"pg_snapshots",
"pg_subtrans",
"pg_twophase",
"pg_multixact",
"pg_multixact/members",
"pg_multixact/offsets",
"base",
"base/1",
"pg_replslot",
"pg_tblspc",
"pg_stat",
"pg_stat_tmp",
"pg_xact",
"pg_logical",
"pg_logical/snapshots",
"pg_logical/mappings"
}

Definition at line 203 of file initdb.c.

Referenced by initialize_data_directory().

◆ success

◆ superuser_password

char* superuser_password = NULL
static

Definition at line 137 of file initdb.c.

Referenced by get_su_pwd(), and setup_auth().

◆ sync_only

bool sync_only = false
static

Definition at line 144 of file initdb.c.

Referenced by main().

◆ system_views_file

char* system_views_file
static

Definition at line 162 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_sysviews().

◆ tsearch_config_languages

const struct tsearch_config_match tsearch_config_languages[]
static

Definition at line 655 of file initdb.c.

◆ username

◆ wal_segment_size_mb

int wal_segment_size_mb
static

Definition at line 149 of file initdb.c.

Referenced by bootstrap_template1(), main(), and pretty_wal_size().

◆ xlog_dir

char* xlog_dir = NULL
static

Definition at line 147 of file initdb.c.

Referenced by cleanup_directories_atexit(), create_xlog_or_symlink(), and main().