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_run_file (FILE *cmdfd, const char *filename)
 
static void setup_description (FILE *cmdfd)
 
static void setup_collation (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_constraints_file
 
static char * system_functions_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 185 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:395

Definition at line 299 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 290 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:528

Definition at line 292 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:220
static int output_errno
Definition: initdb.c:173

Definition at line 311 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:173

Definition at line 305 of file initdb.c.

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

Function Documentation

◆ bootstrap_template1()

static void bootstrap_template1 ( void  )
static

Definition at line 1348 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().

1349 {
1350  PG_CMD_DECL;
1351  char **line;
1352  char **bki_lines;
1353  char headerline[MAXPGPATH];
1354  char buf[64];
1355 
1356  printf(_("running bootstrap script ... "));
1357  fflush(stdout);
1358 
1359  bki_lines = readfile(bki_file);
1360 
1361  /* Check that bki file appears to be of the right version */
1362 
1363  snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1364  PG_MAJORVERSION);
1365 
1366  if (strcmp(headerline, *bki_lines) != 0)
1367  {
1368  pg_log_error("input file \"%s\" does not belong to PostgreSQL %s",
1369  bki_file, PG_VERSION);
1370  fprintf(stderr,
1371  _("Check your installation or specify the correct path "
1372  "using the option -L.\n"));
1373  exit(1);
1374  }
1375 
1376  /* Substitute for various symbols used in the BKI file */
1377 
1378  sprintf(buf, "%d", NAMEDATALEN);
1379  bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1380 
1381  sprintf(buf, "%d", (int) sizeof(Pointer));
1382  bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
1383 
1384  bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
1385  (sizeof(Pointer) == 4) ? "i" : "d");
1386 
1387  bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1388  FLOAT8PASSBYVAL ? "true" : "false");
1389 
1390  bki_lines = replace_token(bki_lines, "POSTGRES",
1392 
1393  bki_lines = replace_token(bki_lines, "ENCODING",
1395 
1396  bki_lines = replace_token(bki_lines, "LC_COLLATE",
1398 
1399  bki_lines = replace_token(bki_lines, "LC_CTYPE",
1401 
1402  /* Also ensure backend isn't confused by this environment var: */
1403  unsetenv("PGCLIENTENCODING");
1404 
1405  snprintf(cmd, sizeof(cmd),
1406  "\"%s\" --boot -x1 -X %u %s %s %s",
1407  backend_exec,
1408  wal_segment_size_mb * (1024 * 1024),
1409  data_checksums ? "-k" : "",
1410  boot_options,
1411  debug ? "-d 5" : "");
1412 
1413 
1414  PG_CMD_OPEN;
1415 
1416  for (line = bki_lines; *line != NULL; line++)
1417  {
1418  PG_CMD_PUTS(*line);
1419  free(*line);
1420  }
1421 
1422  PG_CMD_CLOSE;
1423 
1424  free(bki_lines);
1425 
1426  check_ok();
1427 }
static int encodingid
Definition: initdb.c:154
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:368
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:620
#define pg_log_error(...)
Definition: logging.h:80
#define printf(...)
Definition: port.h:222
#define PG_CMD_PUTS(line)
Definition: initdb.c:305
#define fprintf
Definition: port.h:220
#define NAMEDATALEN
#define sprintf
Definition: port.h:218
char * Pointer
Definition: c.h:418
#define PG_CMD_DECL
Definition: initdb.c:290
#define MAXPGPATH
static char * lc_ctype
Definition: initdb.c:128
#define PG_CMD_OPEN
Definition: initdb.c:292
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:1991
#define PG_CMD_CLOSE
Definition: initdb.c:299
static const char * boot_options
Definition: initdb.c:202
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:570
static char backend_exec[MAXPGPATH]
Definition: initdb.c:233
static char ** readfile(const char *path)
Definition: initdb.c:451
static char * bki_file
Definition: initdb.c:155
#define snprintf
Definition: port.h:216
#define _(x)
Definition: elog.c:89
static char * escape_quotes_bki(const char *src)
Definition: initdb.c:341
#define unsetenv(x)
Definition: win32_port.h:498

◆ check_authmethod_unspecified()

static void check_authmethod_unspecified ( const char **  authmethod)
static

Definition at line 2283 of file initdb.c.

References authwarning.

Referenced by main().

2284 {
2285  if (*authmethod == NULL)
2286  {
2287  authwarning = true;
2288  *authmethod = "trust";
2289  }
2290 }
static bool authwarning
Definition: initdb.c:190

◆ check_authmethod_valid()

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

Definition at line 2293 of file initdb.c.

References pg_log_error.

Referenced by main().

2294 {
2295  const char *const *p;
2296 
2297  for (p = valid_methods; *p; p++)
2298  {
2299  if (strcmp(authmethod, *p) == 0)
2300  return;
2301  /* with space = param */
2302  if (strchr(authmethod, ' '))
2303  if (strncmp(authmethod, *p, (authmethod - strchr(authmethod, ' '))) == 0)
2304  return;
2305  }
2306 
2307  pg_log_error("invalid authentication method \"%s\" for \"%s\" connections",
2308  authmethod, conntype);
2309  exit(1);
2310 }
#define pg_log_error(...)
Definition: logging.h:80

◆ check_input()

static void check_input ( char *  path)
static

Definition at line 778 of file initdb.c.

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

Referenced by setup_data_file_paths().

779 {
780  struct stat statbuf;
781 
782  if (stat(path, &statbuf) != 0)
783  {
784  if (errno == ENOENT)
785  {
786  pg_log_error("file \"%s\" does not exist", 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  else
792  {
793  pg_log_error("could not access file \"%s\": %m", path);
794  fprintf(stderr,
795  _("This might mean you have a corrupted installation or identified\n"
796  "the wrong directory with the invocation option -L.\n"));
797  }
798  exit(1);
799  }
800  if (!S_ISREG(statbuf.st_mode))
801  {
802  pg_log_error("file \"%s\" is not a regular file", path);
803  fprintf(stderr,
804  _("This might mean you have a corrupted installation or identified\n"
805  "the wrong directory with the invocation option -L.\n"));
806  exit(1);
807  }
808 }
#define pg_log_error(...)
Definition: logging.h:80
#define fprintf
Definition: port.h:220
#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 2152 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().

2153 {
2154  int locale_enc;
2155 
2156  locale_enc = pg_get_encoding_from_locale(locale, true);
2157 
2158  /* See notes in createdb() to understand these tests */
2159  if (!(locale_enc == user_enc ||
2160  locale_enc == PG_SQL_ASCII ||
2161  locale_enc == -1 ||
2162 #ifdef WIN32
2163  user_enc == PG_UTF8 ||
2164 #endif
2165  user_enc == PG_SQL_ASCII))
2166  {
2167  pg_log_error("encoding mismatch");
2168  fprintf(stderr,
2169  _("The encoding you selected (%s) and the encoding that the\n"
2170  "selected locale uses (%s) do not match. This would lead to\n"
2171  "misbehavior in various character string processing functions.\n"
2172  "Rerun %s and either do not specify an encoding explicitly,\n"
2173  "or choose a matching combination.\n"),
2174  pg_encoding_to_char(user_enc),
2175  pg_encoding_to_char(locale_enc),
2176  progname);
2177  return false;
2178  }
2179  return true;
2180 }
#define pg_log_error(...)
Definition: logging.h:80
#define fprintf
Definition: port.h:220
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 2088 of file initdb.c.

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

Referenced by setlocales().

2089 {
2090  char *save;
2091  char *res;
2092 
2093  if (canonname)
2094  *canonname = NULL; /* in case of failure */
2095 
2096  save = setlocale(category, NULL);
2097  if (!save)
2098  {
2099  pg_log_error("setlocale() failed");
2100  exit(1);
2101  }
2102 
2103  /* save may be pointing at a modifiable scratch variable, so copy it. */
2104  save = pg_strdup(save);
2105 
2106  /* for setlocale() call */
2107  if (!locale)
2108  locale = "";
2109 
2110  /* set the locale with setlocale, to see if it accepts it. */
2111  res = setlocale(category, locale);
2112 
2113  /* save canonical name if requested. */
2114  if (res && canonname)
2115  *canonname = pg_strdup(res);
2116 
2117  /* restore old value. */
2118  if (!setlocale(category, save))
2119  {
2120  pg_log_error("failed to restore old locale \"%s\"", save);
2121  exit(1);
2122  }
2123  free(save);
2124 
2125  /* complain if locale wasn't valid */
2126  if (res == NULL)
2127  {
2128  if (*locale)
2129  pg_log_error("invalid locale name \"%s\"", locale);
2130  else
2131  {
2132  /*
2133  * If no relevant switch was given on command line, locale is an
2134  * empty string, which is not too helpful to report. Presumably
2135  * setlocale() found something it did not like in the environment.
2136  * Ideally we'd report the bad environment variable, but since
2137  * setlocale's behavior is implementation-specific, it's hard to
2138  * be sure what it didn't like. Print a safe generic message.
2139  */
2140  pg_log_error("invalid locale settings; check LANG and LC_* environment variables");
2141  }
2142  exit(1);
2143  }
2144 }
#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 2313 of file initdb.c.

References pg_log_error, pwfilename, and pwprompt.

Referenced by main().

2314 {
2315  if ((strcmp(authmethodlocal, "md5") == 0 ||
2316  strcmp(authmethodlocal, "password") == 0 ||
2317  strcmp(authmethodlocal, "scram-sha-256") == 0) &&
2318  (strcmp(authmethodhost, "md5") == 0 ||
2319  strcmp(authmethodhost, "password") == 0 ||
2320  strcmp(authmethodhost, "scram-sha-256") == 0) &&
2321  !(pwprompt || pwfilename))
2322  {
2323  pg_log_error("must specify a password for the superuser to enable password authentication");
2324  exit(1);
2325  }
2326 }
#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 1991 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_encoding_conversions(), 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().

1992 {
1993  if (caught_signal)
1994  {
1995  printf(_("caught signal\n"));
1996  fflush(stdout);
1997  exit(1);
1998  }
1999  else if (output_failed)
2000  {
2001  printf(_("could not write to child process: %s\n"),
2003  fflush(stdout);
2004  exit(1);
2005  }
2006  else
2007  {
2008  /* all seems well */
2009  printf(_("ok\n"));
2010  fflush(stdout);
2011  }
2012 }
static bool caught_signal
Definition: initdb.c:171
#define printf(...)
Definition: port.h:222
#define strerror
Definition: port.h:229
static bool output_failed
Definition: initdb.c:172
static int output_errno
Definition: initdb.c:173
#define _(x)
Definition: elog.c:89

◆ choose_dsm_implementation()

static const char* choose_dsm_implementation ( void  )
static

Definition at line 876 of file initdb.c.

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

Referenced by test_config_settings().

877 {
878 #ifdef HAVE_SHM_OPEN
879  int ntries = 10;
880 
881  /* Initialize random(); this function is its only user in this program. */
882  srandom((unsigned int) (getpid() ^ time(NULL)));
883 
884  while (ntries > 0)
885  {
886  uint32 handle;
887  char name[64];
888  int fd;
889 
890  handle = random();
891  snprintf(name, 64, "/PostgreSQL.%u", handle);
892  if ((fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0600)) != -1)
893  {
894  close(fd);
895  shm_unlink(name);
896  return "posix";
897  }
898  if (errno != EEXIST)
899  break;
900  --ntries;
901  }
902 #endif
903 
904 #ifdef WIN32
905  return "windows";
906 #else
907  return "sysv";
908 #endif
909 }
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:441
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:216

◆ cleanup_directories_atexit()

static void cleanup_directories_atexit ( void  )
static

Definition at line 546 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().

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

2609 {
2610  int ret;
2611 
2612  switch ((ret = pg_check_dir(pg_data)))
2613  {
2614  case 0:
2615  /* PGDATA not there, must create it */
2616  printf(_("creating directory %s ... "),
2617  pg_data);
2618  fflush(stdout);
2619 
2621  {
2622  pg_log_error("could not create directory \"%s\": %m", pg_data);
2623  exit(1);
2624  }
2625  else
2626  check_ok();
2627 
2628  made_new_pgdata = true;
2629  break;
2630 
2631  case 1:
2632  /* Present but empty, fix permissions and use it */
2633  printf(_("fixing permissions on existing directory %s ... "),
2634  pg_data);
2635  fflush(stdout);
2636 
2637  if (chmod(pg_data, pg_dir_create_mode) != 0)
2638  {
2639  pg_log_error("could not change permissions of directory \"%s\": %m",
2640  pg_data);
2641  exit(1);
2642  }
2643  else
2644  check_ok();
2645 
2646  found_existing_pgdata = true;
2647  break;
2648 
2649  case 2:
2650  case 3:
2651  case 4:
2652  /* Present and not empty */
2653  pg_log_error("directory \"%s\" exists but is not empty", pg_data);
2654  if (ret != 4)
2655  warn_on_mount_point(ret);
2656  else
2657  fprintf(stderr,
2658  _("If you want to create a new database system, either remove or empty\n"
2659  "the directory \"%s\" or run %s\n"
2660  "with an argument other than \"%s\".\n"),
2662  exit(1); /* no further message needed */
2663 
2664  default:
2665  /* Trouble accessing directory */
2666  pg_log_error("could not access directory \"%s\": %m", pg_data);
2667  exit(1);
2668  }
2669 }
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
void warn_on_mount_point(int error)
Definition: initdb.c:2780
#define pg_log_error(...)
Definition: logging.h:80
#define printf(...)
Definition: port.h:222
#define fprintf
Definition: port.h:220
static bool found_existing_pgdata
Definition: initdb.c:167
static char * pg_data
Definition: initdb.c:124
static void check_ok(void)
Definition: initdb.c:1991
static bool made_new_pgdata
Definition: initdb.c:166
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 2674 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().

2675 {
2676  char *subdirloc;
2677 
2678  /* form name of the place for the subdirectory or symlink */
2679  subdirloc = psprintf("%s/pg_wal", pg_data);
2680 
2681  if (xlog_dir)
2682  {
2683  int ret;
2684 
2685  /* clean up xlog directory name, check it's absolute */
2687  if (!is_absolute_path(xlog_dir))
2688  {
2689  pg_log_error("WAL directory location must be an absolute path");
2690  exit(1);
2691  }
2692 
2693  /* check if the specified xlog directory exists/is empty */
2694  switch ((ret = pg_check_dir(xlog_dir)))
2695  {
2696  case 0:
2697  /* xlog directory not there, must create it */
2698  printf(_("creating directory %s ... "),
2699  xlog_dir);
2700  fflush(stdout);
2701 
2703  {
2704  pg_log_error("could not create directory \"%s\": %m",
2705  xlog_dir);
2706  exit(1);
2707  }
2708  else
2709  check_ok();
2710 
2711  made_new_xlogdir = true;
2712  break;
2713 
2714  case 1:
2715  /* Present but empty, fix permissions and use it */
2716  printf(_("fixing permissions on existing directory %s ... "),
2717  xlog_dir);
2718  fflush(stdout);
2719 
2720  if (chmod(xlog_dir, pg_dir_create_mode) != 0)
2721  {
2722  pg_log_error("could not change permissions of directory \"%s\": %m",
2723  xlog_dir);
2724  exit(1);
2725  }
2726  else
2727  check_ok();
2728 
2729  found_existing_xlogdir = true;
2730  break;
2731 
2732  case 2:
2733  case 3:
2734  case 4:
2735  /* Present and not empty */
2736  pg_log_error("directory \"%s\" exists but is not empty", xlog_dir);
2737  if (ret != 4)
2738  warn_on_mount_point(ret);
2739  else
2740  fprintf(stderr,
2741  _("If you want to store the WAL there, either remove or empty the directory\n"
2742  "\"%s\".\n"),
2743  xlog_dir);
2744  exit(1);
2745 
2746  default:
2747  /* Trouble accessing directory */
2748  pg_log_error("could not access directory \"%s\": %m", xlog_dir);
2749  exit(1);
2750  }
2751 
2752 #ifdef HAVE_SYMLINK
2753  if (symlink(xlog_dir, subdirloc) != 0)
2754  {
2755  pg_log_error("could not create symbolic link \"%s\": %m",
2756  subdirloc);
2757  exit(1);
2758  }
2759 #else
2760  pg_log_error("symlinks are not supported on this platform");
2761  exit(1);
2762 #endif
2763  }
2764  else
2765  {
2766  /* Without -X option, just make the subdirectory normally */
2767  if (mkdir(subdirloc, pg_dir_create_mode) < 0)
2768  {
2769  pg_log_error("could not create directory \"%s\": %m",
2770  subdirloc);
2771  exit(1);
2772  }
2773  }
2774 
2775  free(subdirloc);
2776 }
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
void warn_on_mount_point(int error)
Definition: initdb.c:2780
#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:168
void canonicalize_path(char *path)
Definition: path.c:254
#define printf(...)
Definition: port.h:222
#define fprintf
Definition: port.h:220
#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:1991
static bool found_existing_xlogdir
Definition: initdb.c:169
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 620 of file initdb.c.

References pg_strdup(), and sprintf.

Referenced by bootstrap_template1().

621 {
622  char result[20];
623 
624  sprintf(result, "%d", enc);
625  return pg_strdup(result);
626 }
#define sprintf
Definition: port.h:218
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 322 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().

323 {
324  char *result = escape_single_quotes_ascii(src);
325 
326  if (!result)
327  {
328  pg_log_error("out of memory");
329  exit(1);
330  }
331  return result;
332 }
#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 341 of file initdb.c.

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

Referenced by bootstrap_template1().

342 {
343  char *result;
344  char *data = escape_quotes(src);
345  char *resultp;
346  char *datap;
347 
348  result = (char *) pg_malloc(strlen(data) + 3);
349  resultp = result;
350  *resultp++ = '\'';
351  for (datap = data; *datap; datap++)
352  *resultp++ = *datap;
353  *resultp++ = '\'';
354  *resultp = '\0';
355 
356  free(data);
357  return result;
358 }
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:322

◆ filter_lines_with_token()

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

Definition at line 424 of file initdb.c.

References i, and pg_malloc().

Referenced by setup_config().

425 {
426  int numlines = 1;
427  int i,
428  src,
429  dst;
430  char **result;
431 
432  for (i = 0; lines[i]; i++)
433  numlines++;
434 
435  result = (char **) pg_malloc(numlines * sizeof(char *));
436 
437  for (src = 0, dst = 0; src < numlines; src++)
438  {
439  if (lines[src] == NULL || strstr(lines[src], token) == NULL)
440  result[dst++] = lines[src];
441  }
442 
443  return result;
444 }
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 723 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().

724 {
725  int i;
726  char *langname,
727  *ptr;
728 
729  /*
730  * Convert lc_ctype to a language name by stripping everything after an
731  * underscore (usual case) or a hyphen (Windows "locale name"; see
732  * comments at IsoLocaleName()).
733  *
734  * XXX Should ' ' be a stop character? This would select "norwegian" for
735  * the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
736  * should also accept the "nn" and "nb" Unix locales.
737  *
738  * Just for paranoia, we also stop at '.' or '@'.
739  */
740  if (lc_type == NULL)
741  langname = pg_strdup("");
742  else
743  {
744  ptr = langname = pg_strdup(lc_type);
745  while (*ptr &&
746  *ptr != '_' && *ptr != '-' && *ptr != '.' && *ptr != '@')
747  ptr++;
748  *ptr = '\0';
749  }
750 
751  for (i = 0; tsearch_config_languages[i].tsconfname; i++)
752  {
753  if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
754  {
755  free(langname);
757  }
758  }
759 
760  free(langname);
761  return NULL;
762 }
static const struct tsearch_config_match tsearch_config_languages[]
Definition: initdb.c:656
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * tsconfname
Definition: initdb.c:652
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 632 of file initdb.c.

References enc, pg_log_error, and pg_valid_server_encoding().

Referenced by setup_locale_encoding().

633 {
634  int enc;
635 
636  if (encoding_name && *encoding_name)
637  {
638  if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
639  return enc;
640  }
641  pg_log_error("\"%s\" is not a valid server encoding name",
642  encoding_name ? encoding_name : "(null)");
643  exit(1);
644 }
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 599 of file initdb.c.

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

Referenced by main().

600 {
601  const char *username;
602 
603 #ifndef WIN32
604  if (geteuid() == 0) /* 0 is root's uid */
605  {
606  pg_log_error("cannot be run as root");
607  fprintf(stderr,
608  _("Please log in (using, e.g., \"su\") as the (unprivileged) user that will\n"
609  "own the server process.\n"));
610  exit(1);
611  }
612 #endif
613 
614  username = get_user_name_or_exit(progname);
615 
616  return pg_strdup(username);
617 }
#define pg_log_error(...)
Definition: logging.h:80
#define fprintf
Definition: port.h:220
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 1457 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().

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

References _, backend_exec, backend_options, bootstrap_template1(), check_ok(), create_data_directory(), create_xlog_or_symlink(), DEVNULL, dictionary_file, 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_privileges(), setup_run_file(), setup_schema(), setup_signals(), snprintf, generate_unaccent_rules::stdout, subdirs, system_constraints_file, system_functions_file, system_views_file, test_config_settings(), vacuum_db(), and write_version_file().

Referenced by main().

2797 {
2798  PG_CMD_DECL;
2799  int i;
2800 
2801  setup_signals();
2802 
2803  /*
2804  * Set mask based on requested PGDATA permissions. pg_mode_mask, and
2805  * friends like pg_dir_create_mode, are set to owner-only by default and
2806  * then updated if -g is passed in by calling SetDataDirectoryCreatePerm()
2807  * when parsing our options (see above).
2808  */
2809  umask(pg_mode_mask);
2810 
2812 
2814 
2815  /* Create required subdirectories (other than pg_wal) */
2816  printf(_("creating subdirectories ... "));
2817  fflush(stdout);
2818 
2819  for (i = 0; i < lengthof(subdirs); i++)
2820  {
2821  char *path;
2822 
2823  path = psprintf("%s/%s", pg_data, subdirs[i]);
2824 
2825  /*
2826  * The parent directory already exists, so we only need mkdir() not
2827  * pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
2828  */
2829  if (mkdir(path, pg_dir_create_mode) < 0)
2830  {
2831  pg_log_error("could not create directory \"%s\": %m", path);
2832  exit(1);
2833  }
2834 
2835  free(path);
2836  }
2837 
2838  check_ok();
2839 
2840  /* Top level PG_VERSION is checked by bootstrapper, so make it first */
2841  write_version_file(NULL);
2842 
2843  /* Select suitable configuration settings */
2844  set_null_conf();
2846 
2847  /* Now create all the text config files */
2848  setup_config();
2849 
2850  /* Bootstrap template1 */
2852 
2853  /*
2854  * Make the per-database PG_VERSION for template1 only after init'ing it
2855  */
2856  write_version_file("base/1");
2857 
2858  /*
2859  * Create the stuff we don't need to use bootstrap mode for, using a
2860  * backend running in simple standalone mode.
2861  */
2862  fputs(_("performing post-bootstrap initialization ... "), stdout);
2863  fflush(stdout);
2864 
2865  snprintf(cmd, sizeof(cmd),
2866  "\"%s\" %s template1 >%s",
2868  DEVNULL);
2869 
2870  PG_CMD_OPEN;
2871 
2872  setup_auth(cmdfd);
2873 
2875 
2877 
2878  setup_depend(cmdfd);
2879 
2880  /*
2881  * Note that no objects created after setup_depend() will be "pinned".
2882  * They are all droppable at the whim of the DBA.
2883  */
2884 
2886 
2887  setup_description(cmdfd);
2888 
2889  setup_collation(cmdfd);
2890 
2892 
2893  setup_privileges(cmdfd);
2894 
2895  setup_schema(cmdfd);
2896 
2897  load_plpgsql(cmdfd);
2898 
2899  vacuum_db(cmdfd);
2900 
2901  make_template0(cmdfd);
2902 
2903  make_postgres(cmdfd);
2904 
2905  PG_CMD_CLOSE;
2906 
2907  check_ok();
2908 }
static void setup_depend(FILE *cmdfd)
Definition: initdb.c:1520
static char * system_constraints_file
Definition: initdb.c:162
static void vacuum_db(FILE *cmdfd)
Definition: initdb.c:1897
static char * system_views_file
Definition: initdb.c:164
static const char * backend_options
Definition: initdb.c:203
#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:1433
void create_xlog_or_symlink(void)
Definition: initdb.c:2674
static void bootstrap_template1(void)
Definition: initdb.c:1348
static void setup_run_file(FILE *cmdfd, const char *filename)
Definition: initdb.c:1613
#define printf(...)
Definition: port.h:222
void setup_signals(void)
Definition: initdb.c:2579
#define lengthof(array)
Definition: c.h:734
static char * system_functions_file
Definition: initdb.c:163
static void set_null_conf(void)
Definition: initdb.c:844
#define PG_CMD_DECL
Definition: initdb.c:290
static const char *const subdirs[]
Definition: initdb.c:205
#define PG_CMD_OPEN
Definition: initdb.c:292
static void setup_config(void)
Definition: initdb.c:1048
static void load_plpgsql(FILE *cmdfd)
Definition: initdb.c:1888
static void write_version_file(const char *extrapath)
Definition: initdb.c:815
static void setup_description(FILE *cmdfd)
Definition: initdb.c:1634
static char * pg_data
Definition: initdb.c:124
#define DEVNULL
Definition: port.h:147
static void check_ok(void)
Definition: initdb.c:1991
static char * dictionary_file
Definition: initdb.c:159
static void test_config_settings(void)
Definition: initdb.c:917
void create_data_directory(void)
Definition: initdb.c:2608
int pg_dir_create_mode
Definition: file_perm.c:18
#define PG_CMD_CLOSE
Definition: initdb.c:299
static void make_template0(FILE *cmdfd)
Definition: initdb.c:1907
#define free(a)
Definition: header.h:65
static void setup_collation(FILE *cmdfd)
Definition: initdb.c:1655
static void setup_schema(FILE *cmdfd)
Definition: initdb.c:1868
static void setup_privileges(FILE *cmdfd)
Definition: initdb.c:1690
static void make_postgres(FILE *cmdfd)
Definition: initdb.c:1945
int i
static char backend_exec[MAXPGPATH]
Definition: initdb.c:233
#define mkdir(a, b)
Definition: win32_port.h:63
#define snprintf
Definition: port.h:216
#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 1888 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1889 {
1890  PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n\n");
1891 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:305

◆ locale_date_order()

static int locale_date_order ( const char *  locale)
static

Definition at line 2025 of file initdb.c.

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

Referenced by setup_config().

2026 {
2027  struct tm testtime;
2028  char buf[128];
2029  char *posD;
2030  char *posM;
2031  char *posY;
2032  char *save;
2033  size_t res;
2034  int result;
2035 
2036  result = DATEORDER_MDY; /* default */
2037 
2038  save = setlocale(LC_TIME, NULL);
2039  if (!save)
2040  return result;
2041  save = pg_strdup(save);
2042 
2043  setlocale(LC_TIME, locale);
2044 
2045  memset(&testtime, 0, sizeof(testtime));
2046  testtime.tm_mday = 22;
2047  testtime.tm_mon = 10; /* November, should come out as "11" */
2048  testtime.tm_year = 133; /* 2033 */
2049 
2050  res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2051 
2052  setlocale(LC_TIME, save);
2053  free(save);
2054 
2055  if (res == 0)
2056  return result;
2057 
2058  posM = strstr(buf, "11");
2059  posD = strstr(buf, "22");
2060  posY = strstr(buf, "33");
2061 
2062  if (!posM || !posD || !posY)
2063  return result;
2064 
2065  if (posY < posM && posM < posD)
2066  result = DATEORDER_YMD;
2067  else if (posD < posM)
2068  result = DATEORDER_DMY;
2069  else
2070  result = DATEORDER_MDY;
2071 
2072  return result;
2073 }
#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:223
static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
Definition: initdb.c:2016
#define DATEORDER_DMY
Definition: miscadmin.h:224
#define DATEORDER_MDY
Definition: miscadmin.h:225
#define free(a)
Definition: header.h:65
static char * locale
Definition: initdb.c:126

◆ main()

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

Definition at line 2912 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, make_native_path(), 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.

2913 {
2914  static struct option long_options[] = {
2915  {"pgdata", required_argument, NULL, 'D'},
2916  {"encoding", required_argument, NULL, 'E'},
2917  {"locale", required_argument, NULL, 1},
2918  {"lc-collate", required_argument, NULL, 2},
2919  {"lc-ctype", required_argument, NULL, 3},
2920  {"lc-monetary", required_argument, NULL, 4},
2921  {"lc-numeric", required_argument, NULL, 5},
2922  {"lc-time", required_argument, NULL, 6},
2923  {"lc-messages", required_argument, NULL, 7},
2924  {"no-locale", no_argument, NULL, 8},
2925  {"text-search-config", required_argument, NULL, 'T'},
2926  {"auth", required_argument, NULL, 'A'},
2927  {"auth-local", required_argument, NULL, 10},
2928  {"auth-host", required_argument, NULL, 11},
2929  {"pwprompt", no_argument, NULL, 'W'},
2930  {"pwfile", required_argument, NULL, 9},
2931  {"username", required_argument, NULL, 'U'},
2932  {"help", no_argument, NULL, '?'},
2933  {"version", no_argument, NULL, 'V'},
2934  {"debug", no_argument, NULL, 'd'},
2935  {"show", no_argument, NULL, 's'},
2936  {"noclean", no_argument, NULL, 'n'}, /* for backwards compatibility */
2937  {"no-clean", no_argument, NULL, 'n'},
2938  {"nosync", no_argument, NULL, 'N'}, /* for backwards compatibility */
2939  {"no-sync", no_argument, NULL, 'N'},
2940  {"no-instructions", no_argument, NULL, 13},
2941  {"sync-only", no_argument, NULL, 'S'},
2942  {"waldir", required_argument, NULL, 'X'},
2943  {"wal-segsize", required_argument, NULL, 12},
2944  {"data-checksums", no_argument, NULL, 'k'},
2945  {"allow-group-access", no_argument, NULL, 'g'},
2946  {NULL, 0, NULL, 0}
2947  };
2948 
2949  /*
2950  * options with no short version return a low integer, the rest return
2951  * their short version value
2952  */
2953  int c;
2954  int option_index;
2955  char *effective_user;
2956  PQExpBuffer start_db_cmd;
2957  char pg_ctl_path[MAXPGPATH];
2958 
2959  /*
2960  * Ensure that buffering behavior of stdout matches what it is in
2961  * interactive usage (at least on most platforms). This prevents
2962  * unexpected output ordering when, eg, output is redirected to a file.
2963  * POSIX says we must do this before any other usage of these files.
2964  */
2965  setvbuf(stdout, NULL, PG_IOLBF, 0);
2966 
2967  pg_logging_init(argv[0]);
2968  progname = get_progname(argv[0]);
2969  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
2970 
2971  if (argc > 1)
2972  {
2973  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2974  {
2975  usage(progname);
2976  exit(0);
2977  }
2978  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
2979  {
2980  puts("initdb (PostgreSQL) " PG_VERSION);
2981  exit(0);
2982  }
2983  }
2984 
2985  /* process command-line options */
2986 
2987  while ((c = getopt_long(argc, argv, "A:dD:E:gkL:nNsST:U:WX:", long_options, &option_index)) != -1)
2988  {
2989  switch (c)
2990  {
2991  case 'A':
2993 
2994  /*
2995  * When ident is specified, use peer for local connections.
2996  * Mirrored, when peer is specified, use ident for TCP/IP
2997  * connections.
2998  */
2999  if (strcmp(authmethodhost, "ident") == 0)
3000  authmethodlocal = "peer";
3001  else if (strcmp(authmethodlocal, "peer") == 0)
3002  authmethodhost = "ident";
3003  break;
3004  case 10:
3006  break;
3007  case 11:
3009  break;
3010  case 'D':
3012  break;
3013  case 'E':
3015  break;
3016  case 'W':
3017  pwprompt = true;
3018  break;
3019  case 'U':
3021  break;
3022  case 'd':
3023  debug = true;
3024  printf(_("Running in debug mode.\n"));
3025  break;
3026  case 'n':
3027  noclean = true;
3028  printf(_("Running in no-clean mode. Mistakes will not be cleaned up.\n"));
3029  break;
3030  case 'N':
3031  do_sync = false;
3032  break;
3033  case 'S':
3034  sync_only = true;
3035  break;
3036  case 'k':
3037  data_checksums = true;
3038  break;
3039  case 'L':
3041  break;
3042  case 1:
3043  locale = pg_strdup(optarg);
3044  break;
3045  case 2:
3047  break;
3048  case 3:
3050  break;
3051  case 4:
3053  break;
3054  case 5:
3056  break;
3057  case 6:
3059  break;
3060  case 7:
3062  break;
3063  case 8:
3064  locale = "C";
3065  break;
3066  case 9:
3068  break;
3069  case 's':
3070  show_setting = true;
3071  break;
3072  case 'T':
3074  break;
3075  case 'X':
3077  break;
3078  case 12:
3080  break;
3081  case 13:
3082  noinstructions = true;
3083  break;
3084  case 'g':
3086  break;
3087  default:
3088  /* getopt_long already emitted a complaint */
3089  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
3090  progname);
3091  exit(1);
3092  }
3093  }
3094 
3095 
3096  /*
3097  * Non-option argument specifies data directory as long as it wasn't
3098  * already specified with -D / --pgdata
3099  */
3100  if (optind < argc && !pg_data)
3101  {
3102  pg_data = pg_strdup(argv[optind]);
3103  optind++;
3104  }
3105 
3106  if (optind < argc)
3107  {
3108  pg_log_error("too many command-line arguments (first is \"%s\")",
3109  argv[optind]);
3110  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
3111  progname);
3112  exit(1);
3113  }
3114 
3116 
3117  /* If we only need to fsync, just do it and exit */
3118  if (sync_only)
3119  {
3120  setup_pgdata();
3121 
3122  /* must check that directory is readable */
3123  if (pg_check_dir(pg_data) <= 0)
3124  {
3125  pg_log_error("could not access directory \"%s\": %m", pg_data);
3126  exit(1);
3127  }
3128 
3129  fputs(_("syncing data to disk ... "), stdout);
3130  fflush(stdout);
3131  fsync_pgdata(pg_data, PG_VERSION_NUM);
3132  check_ok();
3133  return 0;
3134  }
3135 
3136  if (pwprompt && pwfilename)
3137  {
3138  pg_log_error("password prompt and password file cannot be specified together");
3139  exit(1);
3140  }
3141 
3144 
3147 
3149 
3150  /* set wal segment size */
3151  if (str_wal_segment_size_mb == NULL)
3152  wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024);
3153  else
3154  {
3155  char *endptr;
3156 
3157  /* check that the argument is a number */
3158  wal_segment_size_mb = strtol(str_wal_segment_size_mb, &endptr, 10);
3159 
3160  /* verify that wal segment size is valid */
3161  if (endptr == str_wal_segment_size_mb || *endptr != '\0')
3162  {
3163  pg_log_error("argument of --wal-segsize must be a number");
3164  exit(1);
3165  }
3166  if (!IsValidWalSegSize(wal_segment_size_mb * 1024 * 1024))
3167  {
3168  pg_log_error("argument of --wal-segsize must be a power of 2 between 1 and 1024");
3169  exit(1);
3170  }
3171  }
3172 
3174 
3175  setup_pgdata();
3176 
3177  setup_bin_paths(argv[0]);
3178 
3179  effective_user = get_id();
3180  if (!username)
3181  username = effective_user;
3182 
3183  if (strncmp(username, "pg_", 3) == 0)
3184  {
3185  pg_log_error("superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"", username);
3186  exit(1);
3187  }
3188 
3189  printf(_("The files belonging to this database system will be owned "
3190  "by user \"%s\".\n"
3191  "This user must also own the server process.\n\n"),
3192  effective_user);
3193 
3194  set_info_version();
3195 
3197 
3199 
3201 
3202  printf("\n");
3203 
3204  if (data_checksums)
3205  printf(_("Data page checksums are enabled.\n"));
3206  else
3207  printf(_("Data page checksums are disabled.\n"));
3208 
3209  if (pwprompt || pwfilename)
3210  get_su_pwd();
3211 
3212  printf("\n");
3213 
3215 
3216  if (do_sync)
3217  {
3218  fputs(_("syncing data to disk ... "), stdout);
3219  fflush(stdout);
3220  fsync_pgdata(pg_data, PG_VERSION_NUM);
3221  check_ok();
3222  }
3223  else
3224  printf(_("\nSync to disk skipped.\nThe data directory might become corrupt if the operating system crashes.\n"));
3225 
3226  if (authwarning)
3227  {
3228  printf("\n");
3229  pg_log_warning("enabling \"trust\" authentication for local connections");
3230  fprintf(stderr, _("You can change this by editing pg_hba.conf or using the option -A, or\n"
3231  "--auth-local and --auth-host, the next time you run initdb.\n"));
3232  }
3233 
3234  if (!noinstructions)
3235  {
3236  /*
3237  * Build up a shell command to tell the user how to start the server
3238  */
3239  start_db_cmd = createPQExpBuffer();
3240 
3241  /* Get directory specification used to start initdb ... */
3242  strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
3243  canonicalize_path(pg_ctl_path);
3244  get_parent_directory(pg_ctl_path);
3245  /* ... and tag on pg_ctl instead */
3246  join_path_components(pg_ctl_path, pg_ctl_path, "pg_ctl");
3247 
3248  /* Convert the path to use native separators */
3249  make_native_path(pg_ctl_path);
3250 
3251  /* path to pg_ctl, properly quoted */
3252  appendShellString(start_db_cmd, pg_ctl_path);
3253 
3254  /* add -D switch, with properly quoted data directory */
3255  appendPQExpBufferStr(start_db_cmd, " -D ");
3256  appendShellString(start_db_cmd, pgdata_native);
3257 
3258  /* add suggested -l switch and "start" command */
3259  /* translator: This is a placeholder in a shell command. */
3260  appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
3261 
3262  printf(_("\nSuccess. You can now start the database server using:\n\n"
3263  " %s\n\n"),
3264  start_db_cmd->data);
3265 
3266  destroyPQExpBuffer(start_db_cmd);
3267  }
3268 
3269 
3270  success = true;
3271  return 0;
3272 }
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:546
void setup_data_file_paths(void)
Definition: initdb.c:2500
void setup_locale_encoding(void)
Definition: initdb.c:2416
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
void make_native_path(char *path)
Definition: path.c:166
#define DEFAULT_XLOG_SEG_SIZE
void canonicalize_path(char *path)
Definition: path.c:254
#define printf(...)
Definition: port.h:222
static bool pwprompt
Definition: initdb.c:135
static char * pgdata_native
Definition: initdb.c:174
static void get_su_pwd(void)
Definition: initdb.c:1457
#define fprintf
Definition: port.h:220
static char * get_id(void)
Definition: initdb.c:599
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:2283
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
static bool authwarning
Definition: initdb.c:190
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:2796
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:1991
void setup_bin_paths(const char *argv0)
Definition: initdb.c:2371
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:1215
static bool data_checksums
Definition: initdb.c:146
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
#define PG_IOLBF
Definition: port.h:344
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:2544
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:433
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:2239
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
static void check_need_password(const char *authmethodlocal, const char *authmethodhost)
Definition: initdb.c:2313
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:165
static void check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
Definition: initdb.c:2293
void setup_pgdata(void)
Definition: initdb.c:2330
#define _(x)
Definition: elog.c:89
static void set_info_version(void)
Definition: initdb.c:1841
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 1945 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1946 {
1947  const char *const *line;
1948  static const char *const postgres_setup[] = {
1949  "CREATE DATABASE postgres;\n\n",
1950  "COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
1951  NULL
1952  };
1953 
1954  for (line = postgres_setup; *line; line++)
1955  PG_CMD_PUTS(*line);
1956 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:305

◆ make_template0()

static void make_template0 ( FILE *  cmdfd)
static

Definition at line 1907 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1908 {
1909  const char *const *line;
1910  static const char *const template0_setup[] = {
1911  "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
1912 
1913  /*
1914  * We use the OID of template0 to determine datlastsysoid
1915  */
1916  "UPDATE pg_database SET datlastsysoid = "
1917  " (SELECT oid FROM pg_database "
1918  " WHERE datname = 'template0');\n\n",
1919 
1920  /*
1921  * Explicitly revoke public create-schema and create-temp-table
1922  * privileges in template1 and template0; else the latter would be on
1923  * by default
1924  */
1925  "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n",
1926  "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n",
1927 
1928  "COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n",
1929 
1930  /*
1931  * Finally vacuum to clean up dead rows in pg_database
1932  */
1933  "VACUUM pg_database;\n\n",
1934  NULL
1935  };
1936 
1937  for (line = template0_setup; *line; line++)
1938  PG_CMD_PUTS(*line);
1939 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:305

◆ my_strftime()

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

Definition at line 2016 of file initdb.c.

Referenced by locale_date_order().

2017 {
2018  return strftime(s, max, fmt, tm);
2019 }
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 528 of file initdb.c.

References pg_log_error, and generate_unaccent_rules::stdout.

529 {
530  FILE *cmdfd;
531 
532  fflush(stdout);
533  fflush(stderr);
534  errno = 0;
535  cmdfd = popen(command, mode);
536  if (cmdfd == NULL)
537  pg_log_error("could not execute command \"%s\": %m", command);
538  return cmdfd;
539 }
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 1031 of file initdb.c.

References pg_malloc(), snprintf, and wal_segment_size_mb.

Referenced by setup_config().

1032 {
1033  int sz = wal_segment_size_mb * segment_count;
1034  char *result = pg_malloc(14);
1035 
1036  if ((sz % 1024) == 0)
1037  snprintf(result, 14, "%dGB", sz / 1024);
1038  else
1039  snprintf(result, 14, "%dMB", sz);
1040 
1041  return result;
1042 }
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:216

◆ readfile()

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

Definition at line 451 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(), and setup_run_file().

452 {
453  char **result;
454  FILE *infile;
455  StringInfoData line;
456  int maxlines;
457  int n;
458 
459  if ((infile = fopen(path, "r")) == NULL)
460  {
461  pg_log_error("could not open file \"%s\" for reading: %m", path);
462  exit(1);
463  }
464 
465  initStringInfo(&line);
466 
467  maxlines = 1024;
468  result = (char **) pg_malloc(maxlines * sizeof(char *));
469 
470  n = 0;
471  while (pg_get_line_buf(infile, &line))
472  {
473  /* make sure there will be room for a trailing NULL pointer */
474  if (n >= maxlines - 1)
475  {
476  maxlines *= 2;
477  result = (char **) pg_realloc(result, maxlines * sizeof(char *));
478  }
479 
480  result[n++] = pg_strdup(line.data);
481  }
482  result[n] = NULL;
483 
484  pfree(line.data);
485 
486  fclose(infile);
487 
488  return result;
489 }
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:1169
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 368 of file initdb.c.

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

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

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

◆ set_info_version()

static void set_info_version ( void  )
static

Definition at line 1841 of file initdb.c.

References infoversion, pg_strdup(), and snprintf.

Referenced by main().

1842 {
1843  char *letterversion;
1844  long major = 0,
1845  minor = 0,
1846  micro = 0;
1847  char *endptr;
1848  char *vstr = pg_strdup(PG_VERSION);
1849  char *ptr;
1850 
1851  ptr = vstr + (strlen(vstr) - 1);
1852  while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1853  ptr--;
1854  letterversion = ptr + 1;
1855  major = strtol(vstr, &endptr, 10);
1856  if (*endptr)
1857  minor = strtol(endptr + 1, &endptr, 10);
1858  if (*endptr)
1859  micro = strtol(endptr + 1, &endptr, 10);
1860  snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1861  major, minor, micro, letterversion);
1862 }
static char infoversion[100]
Definition: initdb.c:170
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define snprintf
Definition: port.h:216

◆ set_input()

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

Definition at line 769 of file initdb.c.

References psprintf(), and share_path.

Referenced by setup_data_file_paths().

770 {
771  *dest = psprintf("%s/%s", share_path, filename);
772 }
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 844 of file initdb.c.

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

Referenced by initialize_data_directory().

845 {
846  FILE *conf_file;
847  char *path;
848 
849  path = psprintf("%s/postgresql.conf", pg_data);
850  conf_file = fopen(path, PG_BINARY_W);
851  if (conf_file == NULL)
852  {
853  pg_log_error("could not open file \"%s\" for writing: %m", path);
854  exit(1);
855  }
856  if (fclose(conf_file))
857  {
858  pg_log_error("could not write file \"%s\": %m", path);
859  exit(1);
860  }
861  free(path);
862 }
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:1274
static char * pg_data
Definition: initdb.c:124
#define free(a)
Definition: header.h:65

◆ setlocales()

static void setlocales ( void  )
static

Definition at line 2188 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().

2189 {
2190  char *canonname;
2191 
2192  /* set empty lc_* values to locale config if set */
2193 
2194  if (locale)
2195  {
2196  if (!lc_ctype)
2197  lc_ctype = locale;
2198  if (!lc_collate)
2199  lc_collate = locale;
2200  if (!lc_numeric)
2201  lc_numeric = locale;
2202  if (!lc_time)
2203  lc_time = locale;
2204  if (!lc_monetary)
2205  lc_monetary = locale;
2206  if (!lc_messages)
2207  lc_messages = locale;
2208  }
2209 
2210  /*
2211  * canonicalize locale names, and obtain any missing values from our
2212  * current environment
2213  */
2214 
2215  check_locale_name(LC_CTYPE, lc_ctype, &canonname);
2216  lc_ctype = canonname;
2217  check_locale_name(LC_COLLATE, lc_collate, &canonname);
2218  lc_collate = canonname;
2219  check_locale_name(LC_NUMERIC, lc_numeric, &canonname);
2220  lc_numeric = canonname;
2221  check_locale_name(LC_TIME, lc_time, &canonname);
2222  lc_time = canonname;
2223  check_locale_name(LC_MONETARY, lc_monetary, &canonname);
2224  lc_monetary = canonname;
2225 #if defined(LC_MESSAGES) && !defined(WIN32)
2226  check_locale_name(LC_MESSAGES, lc_messages, &canonname);
2227  lc_messages = canonname;
2228 #else
2229  /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2230  check_locale_name(LC_CTYPE, lc_messages, &canonname);
2231  lc_messages = canonname;
2232 #endif
2233 }
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:2088
static char * locale
Definition: initdb.c:126

◆ setup_auth()

static void setup_auth ( FILE *  cmdfd)
static

Definition at line 1433 of file initdb.c.

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

Referenced by initialize_data_directory().

1434 {
1435  const char *const *line;
1436  static const char *const pg_authid_setup[] = {
1437  /*
1438  * The authid table shouldn't be readable except through views, to
1439  * ensure passwords are not publicly visible.
1440  */
1441  "REVOKE ALL ON pg_authid FROM public;\n\n",
1442  NULL
1443  };
1444 
1445  for (line = pg_authid_setup; *line != NULL; line++)
1446  PG_CMD_PUTS(*line);
1447 
1448  if (superuser_password)
1449  PG_CMD_PRINTF("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1451 }
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:311
#define PG_CMD_PUTS(line)
Definition: initdb.c:305
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:322

◆ setup_bin_paths()

void setup_bin_paths ( const char *  argv0)

Definition at line 2371 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().

2372 {
2373  int ret;
2374 
2375  if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
2376  backend_exec)) < 0)
2377  {
2378  char full_path[MAXPGPATH];
2379 
2380  if (find_my_exec(argv0, full_path) < 0)
2381  strlcpy(full_path, progname, sizeof(full_path));
2382 
2383  if (ret == -1)
2384  pg_log_error("The program \"%s\" is needed by %s but was not found in the\n"
2385  "same directory as \"%s\".\n"
2386  "Check your installation.",
2387  "postgres", progname, full_path);
2388  else
2389  pg_log_error("The program \"%s\" was found by \"%s\"\n"
2390  "but was not the same version as %s.\n"
2391  "Check your installation.",
2392  "postgres", full_path, progname);
2393  exit(1);
2394  }
2395 
2396  /* store binary directory */
2397  strcpy(bin_path, backend_exec);
2398  *last_dir_separator(bin_path) = '\0';
2400 
2401  if (!share_path)
2402  {
2405  }
2406  else if (!is_absolute_path(share_path))
2407  {
2408  pg_log_error("input file location must be an absolute path");
2409  exit(1);
2410  }
2411 
2413 }
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:322
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:127
#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:135
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static char bin_path[MAXPGPATH]
Definition: initdb.c:232
static char backend_exec[MAXPGPATH]
Definition: initdb.c:233
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 1655 of file initdb.c.

References PG_CMD_PRINTF, PG_CMD_PUTS, and PG_UTF8.

Referenced by initialize_data_directory().

1656 {
1657  /*
1658  * Add an SQL-standard name. We don't want to pin this, so it doesn't go
1659  * in pg_collation.h. But add it before reading system collations, so
1660  * that it wins if libc defines a locale named ucs_basic.
1661  */
1662  PG_CMD_PRINTF("INSERT INTO pg_collation (oid, collname, collnamespace, collowner, collprovider, collisdeterministic, collencoding, collcollate, collctype)"
1663  "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",
1664  BOOTSTRAP_SUPERUSERID, COLLPROVIDER_LIBC, PG_UTF8);
1665 
1666  /* Now import all collations we can find in the operating system */
1667  PG_CMD_PUTS("SELECT pg_import_system_collations('pg_catalog');\n\n");
1668 }
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:311
#define PG_CMD_PUTS(line)
Definition: initdb.c:305

◆ setup_config()

static void setup_config ( void  )
static

Definition at line 1048 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().

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

Referenced by main().

2501 {
2502  set_input(&bki_file, "postgres.bki");
2503  set_input(&hba_file, "pg_hba.conf.sample");
2504  set_input(&ident_file, "pg_ident.conf.sample");
2505  set_input(&conf_file, "postgresql.conf.sample");
2506  set_input(&dictionary_file, "snowball_create.sql");
2507  set_input(&info_schema_file, "information_schema.sql");
2508  set_input(&features_file, "sql_features.txt");
2509  set_input(&system_constraints_file, "system_constraints.sql");
2510  set_input(&system_functions_file, "system_functions.sql");
2511  set_input(&system_views_file, "system_views.sql");
2512 
2513  if (show_setting || debug)
2514  {
2515  fprintf(stderr,
2516  "VERSION=%s\n"
2517  "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2518  "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2519  "POSTGRESQL_CONF_SAMPLE=%s\n"
2520  "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2521  PG_VERSION,
2523  username, bki_file,
2524  conf_file,
2525  hba_file, ident_file);
2526  if (show_setting)
2527  exit(0);
2528  }
2529 
2540 }
static char * system_constraints_file
Definition: initdb.c:162
static bool debug
Definition: initdb.c:140
static char * system_views_file
Definition: initdb.c:164
static char * conf_file
Definition: initdb.c:158
static char * features_file
Definition: initdb.c:161
static char * system_functions_file
Definition: initdb.c:163
#define fprintf
Definition: port.h:220
static char * share_path
Definition: initdb.c:121
static void check_input(char *path)
Definition: initdb.c:778
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:232
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:769

◆ setup_depend()

static void setup_depend ( FILE *  cmdfd)
static

Definition at line 1520 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

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

◆ setup_description()

static void setup_description ( FILE *  cmdfd)
static

Definition at line 1634 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

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

◆ setup_locale_encoding()

void setup_locale_encoding ( void  )

Definition at line 2416 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().

2417 {
2418  setlocales();
2419 
2420  if (strcmp(lc_ctype, lc_collate) == 0 &&
2421  strcmp(lc_ctype, lc_time) == 0 &&
2422  strcmp(lc_ctype, lc_numeric) == 0 &&
2423  strcmp(lc_ctype, lc_monetary) == 0 &&
2424  strcmp(lc_ctype, lc_messages) == 0)
2425  printf(_("The database cluster will be initialized with locale \"%s\".\n"), lc_ctype);
2426  else
2427  {
2428  printf(_("The database cluster will be initialized with locales\n"
2429  " COLLATE: %s\n"
2430  " CTYPE: %s\n"
2431  " MESSAGES: %s\n"
2432  " MONETARY: %s\n"
2433  " NUMERIC: %s\n"
2434  " TIME: %s\n"),
2435  lc_collate,
2436  lc_ctype,
2437  lc_messages,
2438  lc_monetary,
2439  lc_numeric,
2440  lc_time);
2441  }
2442 
2443  if (!encoding)
2444  {
2445  int ctype_enc;
2446 
2447  ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);
2448 
2449  if (ctype_enc == -1)
2450  {
2451  /* Couldn't recognize the locale's codeset */
2452  pg_log_error("could not find suitable encoding for locale \"%s\"",
2453  lc_ctype);
2454  fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
2455  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2456  progname);
2457  exit(1);
2458  }
2459  else if (!pg_valid_server_encoding_id(ctype_enc))
2460  {
2461  /*
2462  * We recognized it, but it's not a legal server encoding. On
2463  * Windows, UTF-8 works with any locale, so we can fall back to
2464  * UTF-8.
2465  */
2466 #ifdef WIN32
2467  encodingid = PG_UTF8;
2468  printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
2469  "The default database encoding will be set to \"%s\" instead.\n"),
2470  pg_encoding_to_char(ctype_enc),
2472 #else
2473  pg_log_error("locale \"%s\" requires unsupported encoding \"%s\"",
2474  lc_ctype, pg_encoding_to_char(ctype_enc));
2475  fprintf(stderr,
2476  _("Encoding \"%s\" is not allowed as a server-side encoding.\n"
2477  "Rerun %s with a different locale selection.\n"),
2478  pg_encoding_to_char(ctype_enc), progname);
2479  exit(1);
2480 #endif
2481  }
2482  else
2483  {
2484  encodingid = ctype_enc;
2485  printf(_("The default database encoding has accordingly been set to \"%s\".\n"),
2487  }
2488  }
2489  else
2491 
2494  exit(1); /* check_locale_encoding printed the error */
2495 
2496 }
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:222
#define fprintf
Definition: port.h:220
static int get_encoding_id(const char *encoding_name)
Definition: initdb.c:632
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:2152
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:2188
#define _(x)
Definition: elog.c:89

◆ setup_pgdata()

void setup_pgdata ( void  )

Definition at line 2330 of file initdb.c.

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

Referenced by main().

2331 {
2332  char *pgdata_get_env;
2333 
2334  if (!pg_data)
2335  {
2336  pgdata_get_env = getenv("PGDATA");
2337  if (pgdata_get_env && strlen(pgdata_get_env))
2338  {
2339  /* PGDATA found */
2340  pg_data = pg_strdup(pgdata_get_env);
2341  }
2342  else
2343  {
2344  pg_log_error("no data directory specified");
2345  fprintf(stderr,
2346  _("You must identify the directory where the data for this database system\n"
2347  "will reside. Do this with either the invocation option -D or the\n"
2348  "environment variable PGDATA.\n"));
2349  exit(1);
2350  }
2351  }
2352 
2355 
2356  /*
2357  * we have to set PGDATA for postgres rather than pass it on the command
2358  * line to avoid dumb quoting problems on Windows, and we would especially
2359  * need quotes otherwise on Windows because paths there are most likely to
2360  * have embedded spaces.
2361  */
2362  if (setenv("PGDATA", pg_data, 1) != 0)
2363  {
2364  pg_log_error("could not set environment");
2365  exit(1);
2366  }
2367 }
#define pg_log_error(...)
Definition: logging.h:80
void canonicalize_path(char *path)
Definition: path.c:254
static char * pgdata_native
Definition: initdb.c:174
#define fprintf
Definition: port.h:220
#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 1690 of file initdb.c.

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

Referenced by initialize_data_directory().

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

◆ setup_run_file()

static void setup_run_file ( FILE *  cmdfd,
const char *  filename 
)
static

Definition at line 1613 of file initdb.c.

References free, PG_CMD_PUTS, and readfile().

Referenced by initialize_data_directory(), and setup_schema().

1614 {
1615  char **lines;
1616 
1617  lines = readfile(filename);
1618 
1619  for (char **line = lines; *line != NULL; line++)
1620  {
1621  PG_CMD_PUTS(*line);
1622  free(*line);
1623  }
1624 
1625  PG_CMD_PUTS("\n\n");
1626 
1627  free(lines);
1628 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:305
#define free(a)
Definition: header.h:65
static char * filename
Definition: pg_dumpall.c:91
static char ** readfile(const char *path)
Definition: initdb.c:451

◆ setup_schema()

static void setup_schema ( FILE *  cmdfd)
static

Definition at line 1868 of file initdb.c.

References escape_quotes(), features_file, info_schema_file, infoversion, PG_CMD_PRINTF, and setup_run_file().

Referenced by initialize_data_directory().

1869 {
1871 
1872  PG_CMD_PRINTF("UPDATE information_schema.sql_implementation_info "
1873  " SET character_value = '%s' "
1874  " WHERE implementation_info_name = 'DBMS VERSION';\n\n",
1875  infoversion);
1876 
1877  PG_CMD_PRINTF("COPY information_schema.sql_features "
1878  " (feature_id, feature_name, sub_feature_id, "
1879  " sub_feature_name, is_supported, comments) "
1880  " FROM E'%s';\n\n",
1882 }
static char infoversion[100]
Definition: initdb.c:170
static char * features_file
Definition: initdb.c:161
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:311
static void setup_run_file(FILE *cmdfd, const char *filename)
Definition: initdb.c:1613
static char * info_schema_file
Definition: initdb.c:160
static char * escape_quotes(const char *src)
Definition: initdb.c:322

◆ setup_signals()

void setup_signals ( void  )

Definition at line 2579 of file initdb.c.

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

Referenced by initialize_data_directory().

2580 {
2581  /* some of these are not valid on Windows */
2582 #ifdef SIGHUP
2584 #endif
2585 #ifdef SIGINT
2586  pqsignal(SIGINT, trapsig);
2587 #endif
2588 #ifdef SIGQUIT
2590 #endif
2591 #ifdef SIGTERM
2592  pqsignal(SIGTERM, trapsig);
2593 #endif
2594 
2595  /* Ignore SIGPIPE when writing to backend, so we can clean up */
2596 #ifdef SIGPIPE
2598 #endif
2599 
2600  /* Prevent SIGSYS so we can probe for kernel calls that might not work */
2601 #ifdef SIGSYS
2602  pqsignal(SIGSYS, SIG_IGN);
2603 #endif
2604 }
#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:1980

◆ setup_text_search()

void setup_text_search ( void  )

Definition at line 2544 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().

2545 {
2547  {
2550  {
2551  pg_log_info("could not find suitable text search configuration for locale \"%s\"",
2552  lc_ctype);
2553  default_text_search_config = "simple";
2554  }
2555  }
2556  else
2557  {
2558  const char *checkmatch = find_matching_ts_config(lc_ctype);
2559 
2560  if (checkmatch == NULL)
2561  {
2562  pg_log_warning("suitable text search configuration for locale \"%s\" is unknown",
2563  lc_ctype);
2564  }
2565  else if (strcmp(checkmatch, default_text_search_config) != 0)
2566  {
2567  pg_log_warning("specified text search configuration \"%s\" might not match locale \"%s\"",
2569  }
2570  }
2571 
2572  printf(_("The default text search configuration will be set to \"%s\".\n"),
2574 
2575 }
static const char * find_matching_ts_config(const char *lc_type)
Definition: initdb.c:723
#define printf(...)
Definition: port.h:222
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 917 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().

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

◆ trapsig()

static void trapsig ( int  signum)
static

Definition at line 1980 of file initdb.c.

References caught_signal, and pqsignal().

Referenced by setup_signals().

1981 {
1982  /* handle systems that reset the handler, like Windows (grr) */
1983  pqsignal(signum, trapsig);
1984  caught_signal = true;
1985 }
static bool caught_signal
Definition: initdb.c:171
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static void trapsig(int signum)
Definition: initdb.c:1980

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 2239 of file initdb.c.

References _, and printf.

Referenced by main().

2240 {
2241  printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2242  printf(_("Usage:\n"));
2243  printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2244  printf(_("\nOptions:\n"));
2245  printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2246  printf(_(" --auth-host=METHOD default authentication method for local TCP/IP connections\n"));
2247  printf(_(" --auth-local=METHOD default authentication method for local-socket connections\n"));
2248  printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2249  printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2250  printf(_(" -g, --allow-group-access allow group read/execute on data directory\n"));
2251  printf(_(" -k, --data-checksums use data page checksums\n"));
2252  printf(_(" --locale=LOCALE set default locale for new databases\n"));
2253  printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2254  " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2255  " set default locale in the respective category for\n"
2256  " new databases (default taken from environment)\n"));
2257  printf(_(" --no-locale equivalent to --locale=C\n"));
2258  printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2259  printf(_(" -T, --text-search-config=CFG\n"
2260  " default text search configuration\n"));
2261  printf(_(" -U, --username=NAME database superuser name\n"));
2262  printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2263  printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
2264  printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
2265  printf(_("\nLess commonly used options:\n"));
2266  printf(_(" -d, --debug generate lots of debugging output\n"));
2267  printf(_(" -L DIRECTORY where to find the input files\n"));
2268  printf(_(" -n, --no-clean do not clean up after errors\n"));
2269  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
2270  printf(_(" --no-instructions do not print instructions for next steps\n"));
2271  printf(_(" -s, --show show internal settings\n"));
2272  printf(_(" -S, --sync-only only sync data directory\n"));
2273  printf(_("\nOther options:\n"));
2274  printf(_(" -V, --version output version information, then exit\n"));
2275  printf(_(" -?, --help show this help, then exit\n"));
2276  printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2277  "is used.\n"));
2278  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2279  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2280 }
#define printf(...)
Definition: port.h:222
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 1897 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1898 {
1899  /* Run analyze before VACUUM so the statistics are frozen. */
1900  PG_CMD_PUTS("ANALYZE;\n\nVACUUM FREEZE;\n\n");
1901 }
#define PG_CMD_PUTS(line)
Definition: initdb.c:305

◆ warn_on_mount_point()

void warn_on_mount_point ( int  error)

Definition at line 2780 of file initdb.c.

References _, and fprintf.

Referenced by create_data_directory(), and create_xlog_or_symlink().

2781 {
2782  if (error == 2)
2783  fprintf(stderr,
2784  _("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.\n"));
2785  else if (error == 3)
2786  fprintf(stderr,
2787  _("It contains a lost+found directory, perhaps due to it being a mount point.\n"));
2788 
2789  fprintf(stderr,
2790  _("Using a mount point directly as the data directory is not recommended.\n"
2791  "Create a subdirectory under the mount point.\n"));
2792 }
static void error(void)
Definition: sql-dyntest.c:147
#define fprintf
Definition: port.h:220
#define _(x)
Definition: elog.c:89

◆ write_version_file()

static void write_version_file ( const char *  extrapath)
static

Definition at line 815 of file initdb.c.

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

Referenced by initialize_data_directory().

816 {
817  FILE *version_file;
818  char *path;
819 
820  if (extrapath == NULL)
821  path = psprintf("%s/PG_VERSION", pg_data);
822  else
823  path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
824 
825  if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
826  {
827  pg_log_error("could not open file \"%s\" for writing: %m", path);
828  exit(1);
829  }
830  if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
831  fclose(version_file))
832  {
833  pg_log_error("could not write file \"%s\": %m", path);
834  exit(1);
835  }
836  free(path);
837 }
#define pg_log_error(...)
Definition: logging.h:80
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_BINARY_W
Definition: c.h:1274
#define fprintf
Definition: port.h:220
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 498 of file initdb.c.

References free, and pg_log_error.

Referenced by setup_config().

499 {
500  FILE *out_file;
501  char **line;
502 
503  if ((out_file = fopen(path, "w")) == NULL)
504  {
505  pg_log_error("could not open file \"%s\" for writing: %m", path);
506  exit(1);
507  }
508  for (line = lines; *line != NULL; line++)
509  {
510  if (fputs(*line, out_file) < 0)
511  {
512  pg_log_error("could not write file \"%s\": %m", path);
513  exit(1);
514  }
515  free(*line);
516  }
517  if (fclose(out_file))
518  {
519  pg_log_error("could not write file \"%s\": %m", path);
520  exit(1);
521  }
522 }
#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 190 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 203 of file initdb.c.

Referenced by initialize_data_directory().

◆ bin_path

char bin_path[MAXPGPATH]
static

Definition at line 232 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 202 of file initdb.c.

Referenced by bootstrap_template1(), and test_config_settings().

◆ caught_signal

bool caught_signal = false
static

Definition at line 171 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

◆ 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 180 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 initialize_data_directory(), and setup_data_file_paths().

◆ 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 179 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 167 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 169 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 170 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 166 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 168 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_xlog_or_symlink().

◆ n_buffers

int n_buffers = 50
static

Definition at line 178 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ n_connections

int n_connections = 10
static

Definition at line 177 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 173 of file initdb.c.

Referenced by check_ok().

◆ output_failed

bool output_failed = false
static

Definition at line 172 of file initdb.c.

Referenced by check_ok().

◆ pg_data

◆ pgdata_native

char* pgdata_native
static

Definition at line 174 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 205 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_constraints_file

char* system_constraints_file
static

Definition at line 162 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ system_functions_file

char* system_functions_file
static

Definition at line 163 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ system_views_file

char* system_views_file
static

Definition at line 164 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ tsearch_config_languages

const struct tsearch_config_match tsearch_config_languages[]
static

Definition at line 656 of file initdb.c.

◆ username

char* username = NULL
static

◆ 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().