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/restricted_token.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_PRINTF1(fmt, arg1)
 
#define PG_CMD_PRINTF2(fmt, arg1, arg2)
 
#define PG_CMD_PRINTF3(fmt, arg1, arg2, arg3)
 
#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 void exit_nicely (void)
 
static char * escape_quotes_bki (const char *src)
 
static char * get_id (void)
 
static char * encodingid_to_string (int enc)
 
static int get_encoding_id (const char *encoding_name)
 
static const char * find_matching_ts_config (const char *lc_type)
 
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 char * choose_dsm_implementation (void)
 
static void test_config_settings (void)
 
static char * pretty_wal_size (int segment_count)
 
static void setup_config (void)
 
static void bootstrap_template1 (void)
 
static void setup_auth (FILE *cmdfd)
 
static void get_su_pwd (void)
 
static void setup_depend (FILE *cmdfd)
 
static void setup_sysviews (FILE *cmdfd)
 
static void setup_description (FILE *cmdfd)
 
static void setup_collation (FILE *cmdfd)
 
static void setup_conversion (FILE *cmdfd)
 
static void setup_dictionary (FILE *cmdfd)
 
static void setup_privileges (FILE *cmdfd)
 
static void set_info_version (void)
 
static void setup_schema (FILE *cmdfd)
 
static void load_plpgsql (FILE *cmdfd)
 
static void vacuum_db (FILE *cmdfd)
 
static void make_template0 (FILE *cmdfd)
 
static void make_postgres (FILE *cmdfd)
 
static void trapsig (int signum)
 
static void check_ok (void)
 
static size_t my_strftime (char *s, size_t max, const char *fmt, const struct tm *tm)
 
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 user_enc)
 
static void setlocales (void)
 
static void usage (const char *progname)
 
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)
 
void setup_pgdata (void)
 
void setup_bin_paths (const char *argv0)
 
void setup_locale_encoding (void)
 
void setup_data_file_paths (void)
 
void setup_text_search (void)
 
void setup_signals (void)
 
void create_data_directory (void)
 
void create_xlog_or_symlink (void)
 
void warn_on_mount_point (int error)
 
void initialize_data_directory (void)
 
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 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 * desc_file
 
static char * shdesc_file
 
static char * hba_file
 
static char * ident_file
 
static char * conf_file
 
static char * conversion_file
 
static char * dictionary_file
 
static char * info_schema_file
 
static char * features_file
 
static char * system_views_file
 
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 char * dynamic_shared_memory_type = NULL
 
static char * authwarning = NULL
 
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 181 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_nicely(); /* message already printed by pclose_check */ \
} while (0)
int pclose_check(FILE *stream)
Definition: exec.c:510

Referenced by bootstrap_template1(), and initialize_data_directory().

◆ PG_CMD_DECL

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

◆ PG_CMD_OPEN

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

Referenced by bootstrap_template1(), and initialize_data_directory().

◆ PG_CMD_PRINTF1

#define PG_CMD_PRINTF1 (   fmt,
  arg1 
)
Value:
do { \
if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)
static int output_errno
Definition: initdb.c:170

Referenced by setup_description(), and setup_schema().

◆ PG_CMD_PRINTF2

#define PG_CMD_PRINTF2 (   fmt,
  arg1,
  arg2 
)
Value:
do { \
if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)
static int output_errno
Definition: initdb.c:170

Referenced by setup_auth().

◆ PG_CMD_PRINTF3

#define PG_CMD_PRINTF3 (   fmt,
  arg1,
  arg2,
  arg3 
)
Value:
do { \
if (fprintf(cmdfd, fmt, arg1, arg2, arg3) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)
static int output_errno
Definition: initdb.c:170

Referenced by setup_collation().

◆ 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:170

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

Function Documentation

◆ bootstrap_template1()

static void bootstrap_template1 ( void  )
static

Definition at line 1377 of file initdb.c.

References _, backend_exec, bki_file, boot_options, buf, check_ok(), data_checksums, debug, encodingid, encodingid_to_string(), escape_quotes_bki(), exit_nicely(), free, lc_collate, lc_ctype, MAXPGPATH, NAMEDATALEN, PG_CMD_CLOSE, PG_CMD_DECL, PG_CMD_OPEN, PG_CMD_PUTS, pg_strdup(), progname, putenv, readfile(), replace_token(), snprintf(), unsetenv, username, and wal_segment_size_mb.

Referenced by initialize_data_directory().

1378 {
1379  PG_CMD_DECL;
1380  char **line;
1381  char **bki_lines;
1382  char headerline[MAXPGPATH];
1383  char buf[64];
1384 
1385  printf(_("running bootstrap script ... "));
1386  fflush(stdout);
1387 
1388  bki_lines = readfile(bki_file);
1389 
1390  /* Check that bki file appears to be of the right version */
1391 
1392  snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1393  PG_MAJORVERSION);
1394 
1395  if (strcmp(headerline, *bki_lines) != 0)
1396  {
1397  fprintf(stderr,
1398  _("%s: input file \"%s\" does not belong to PostgreSQL %s\n"
1399  "Check your installation or specify the correct path "
1400  "using the option -L.\n"),
1401  progname, bki_file, PG_VERSION);
1402  exit_nicely();
1403  }
1404 
1405  /* Substitute for various symbols used in the BKI file */
1406 
1407  sprintf(buf, "%d", NAMEDATALEN);
1408  bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1409 
1410  sprintf(buf, "%d", (int) sizeof(Pointer));
1411  bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
1412 
1413  bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
1414  (sizeof(Pointer) == 4) ? "i" : "d");
1415 
1416  bki_lines = replace_token(bki_lines, "FLOAT4PASSBYVAL",
1417  FLOAT4PASSBYVAL ? "true" : "false");
1418 
1419  bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1420  FLOAT8PASSBYVAL ? "true" : "false");
1421 
1422  bki_lines = replace_token(bki_lines, "POSTGRES",
1424 
1425  bki_lines = replace_token(bki_lines, "ENCODING",
1427 
1428  bki_lines = replace_token(bki_lines, "LC_COLLATE",
1430 
1431  bki_lines = replace_token(bki_lines, "LC_CTYPE",
1433 
1434  /*
1435  * Pass correct LC_xxx environment to bootstrap.
1436  *
1437  * The shell script arranged to restore the LC settings afterwards, but
1438  * there doesn't seem to be any compelling reason to do that.
1439  */
1440  snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate);
1441  putenv(pg_strdup(cmd));
1442 
1443  snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
1444  putenv(pg_strdup(cmd));
1445 
1446  unsetenv("LC_ALL");
1447 
1448  /* Also ensure backend isn't confused by this environment var: */
1449  unsetenv("PGCLIENTENCODING");
1450 
1451  snprintf(cmd, sizeof(cmd),
1452  "\"%s\" --boot -x1 -X %u %s %s %s",
1453  backend_exec,
1454  wal_segment_size_mb * (1024 * 1024),
1455  data_checksums ? "-k" : "",
1456  boot_options,
1457  debug ? "-d 5" : "");
1458 
1459 
1460  PG_CMD_OPEN;
1461 
1462  for (line = bki_lines; *line != NULL; line++)
1463  {
1464  PG_CMD_PUTS(*line);
1465  free(*line);
1466  }
1467 
1468  PG_CMD_CLOSE;
1469 
1470  free(bki_lines);
1471 
1472  check_ok();
1473 }
static int encodingid
Definition: initdb.c:151
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:399
static bool debug
Definition: initdb.c:138
static char * lc_collate
Definition: initdb.c:125
static char * encodingid_to_string(int enc)
Definition: initdb.c:682
static void exit_nicely(void)
Definition: initdb.c:240
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_CMD_PUTS(line)
#define NAMEDATALEN
char * Pointer
Definition: c.h:302
#define PG_CMD_DECL
#define MAXPGPATH
static char * lc_ctype
Definition: initdb.c:126
#define PG_CMD_OPEN
static char * buf
Definition: pg_test_fsync.c:67
static int wal_segment_size_mb
Definition: initdb.c:146
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void check_ok(void)
Definition: initdb.c:2126
static const char * progname
Definition: initdb.c:150
#define PG_CMD_CLOSE
static const char * boot_options
Definition: initdb.c:198
static bool data_checksums
Definition: initdb.c:143
static char * username
Definition: initdb.c:132
#define free(a)
Definition: header.h:65
static char * escape_quotes_bki(const char *src)
Definition: initdb.c:355
#define putenv(x)
Definition: win32_port.h:487
static char backend_exec[MAXPGPATH]
Definition: initdb.c:229
static char ** readfile(const char *path)
Definition: initdb.c:482
static char * bki_file
Definition: initdb.c:152
#define _(x)
Definition: elog.c:84
#define unsetenv(x)
Definition: win32_port.h:488

◆ check_authmethod_unspecified()

static void check_authmethod_unspecified ( const char **  authmethod)
static

Definition at line 2420 of file initdb.c.

References _, and authwarning.

Referenced by main().

2421 {
2422  if (*authmethod == NULL)
2423  {
2424  authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n"
2425  "You can change this by editing pg_hba.conf or using the option -A, or\n"
2426  "--auth-local and --auth-host, the next time you run initdb.\n");
2427  *authmethod = "trust";
2428  }
2429 }
static char * authwarning
Definition: initdb.c:186
#define _(x)
Definition: elog.c:84

◆ check_authmethod_valid()

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

Definition at line 2432 of file initdb.c.

References _, and progname.

Referenced by main().

2433 {
2434  const char *const *p;
2435 
2436  for (p = valid_methods; *p; p++)
2437  {
2438  if (strcmp(authmethod, *p) == 0)
2439  return;
2440  /* with space = param */
2441  if (strchr(authmethod, ' '))
2442  if (strncmp(authmethod, *p, (authmethod - strchr(authmethod, ' '))) == 0)
2443  return;
2444  }
2445 
2446  fprintf(stderr, _("%s: invalid authentication method \"%s\" for \"%s\" connections\n"),
2447  progname, authmethod, conntype);
2448  exit(1);
2449 }
static const char * progname
Definition: initdb.c:150
#define _(x)
Definition: elog.c:84

◆ check_input()

static void check_input ( char *  path)
static

Definition at line 814 of file initdb.c.

References _, progname, S_ISREG, stat, and strerror().

Referenced by setup_data_file_paths().

815 {
816  struct stat statbuf;
817 
818  if (stat(path, &statbuf) != 0)
819  {
820  if (errno == ENOENT)
821  {
822  fprintf(stderr,
823  _("%s: file \"%s\" does not exist\n"), progname, path);
824  fprintf(stderr,
825  _("This might mean you have a corrupted installation or identified\n"
826  "the wrong directory with the invocation option -L.\n"));
827  }
828  else
829  {
830  fprintf(stderr,
831  _("%s: could not access file \"%s\": %s\n"), progname, path,
832  strerror(errno));
833  fprintf(stderr,
834  _("This might mean you have a corrupted installation or identified\n"
835  "the wrong directory with the invocation option -L.\n"));
836  }
837  exit(1);
838  }
839  if (!S_ISREG(statbuf.st_mode))
840  {
841  fprintf(stderr,
842  _("%s: file \"%s\" is not a regular file\n"), progname, path);
843  fprintf(stderr,
844  _("This might mean you have a corrupted installation or identified\n"
845  "the wrong directory with the invocation option -L.\n"));
846  exit(1);
847  }
848 }
#define S_ISREG(m)
Definition: win32_port.h:310
#define stat(a, b)
Definition: win32_port.h:266
static const char * progname
Definition: initdb.c:150
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ check_locale_encoding()

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

Definition at line 2291 of file initdb.c.

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

Referenced by setup_locale_encoding().

2292 {
2293  int locale_enc;
2294 
2295  locale_enc = pg_get_encoding_from_locale(locale, true);
2296 
2297  /* See notes in createdb() to understand these tests */
2298  if (!(locale_enc == user_enc ||
2299  locale_enc == PG_SQL_ASCII ||
2300  locale_enc == -1 ||
2301 #ifdef WIN32
2302  user_enc == PG_UTF8 ||
2303 #endif
2304  user_enc == PG_SQL_ASCII))
2305  {
2306  fprintf(stderr, _("%s: encoding mismatch\n"), progname);
2307  fprintf(stderr,
2308  _("The encoding you selected (%s) and the encoding that the\n"
2309  "selected locale uses (%s) do not match. This would lead to\n"
2310  "misbehavior in various character string processing functions.\n"
2311  "Rerun %s and either do not specify an encoding explicitly,\n"
2312  "or choose a matching combination.\n"),
2313  pg_encoding_to_char(user_enc),
2314  pg_encoding_to_char(locale_enc),
2315  progname);
2316  return false;
2317  }
2318  return true;
2319 }
static const char * progname
Definition: initdb.c:150
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:433
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
static char * locale
Definition: initdb.c:124
#define _(x)
Definition: elog.c:84

◆ check_locale_name()

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

Definition at line 2223 of file initdb.c.

References _, free, pg_strdup(), progname, and setlocale.

Referenced by setlocales().

2224 {
2225  char *save;
2226  char *res;
2227 
2228  if (canonname)
2229  *canonname = NULL; /* in case of failure */
2230 
2231  save = setlocale(category, NULL);
2232  if (!save)
2233  {
2234  fprintf(stderr, _("%s: setlocale() failed\n"),
2235  progname);
2236  exit(1);
2237  }
2238 
2239  /* save may be pointing at a modifiable scratch variable, so copy it. */
2240  save = pg_strdup(save);
2241 
2242  /* for setlocale() call */
2243  if (!locale)
2244  locale = "";
2245 
2246  /* set the locale with setlocale, to see if it accepts it. */
2247  res = setlocale(category, locale);
2248 
2249  /* save canonical name if requested. */
2250  if (res && canonname)
2251  *canonname = pg_strdup(res);
2252 
2253  /* restore old value. */
2254  if (!setlocale(category, save))
2255  {
2256  fprintf(stderr, _("%s: failed to restore old locale \"%s\"\n"),
2257  progname, save);
2258  exit(1);
2259  }
2260  free(save);
2261 
2262  /* complain if locale wasn't valid */
2263  if (res == NULL)
2264  {
2265  if (*locale)
2266  fprintf(stderr, _("%s: invalid locale name \"%s\"\n"),
2267  progname, locale);
2268  else
2269  {
2270  /*
2271  * If no relevant switch was given on command line, locale is an
2272  * empty string, which is not too helpful to report. Presumably
2273  * setlocale() found something it did not like in the environment.
2274  * Ideally we'd report the bad environment variable, but since
2275  * setlocale's behavior is implementation-specific, it's hard to
2276  * be sure what it didn't like. Print a safe generic message.
2277  */
2278  fprintf(stderr, _("%s: invalid locale settings; check LANG and LC_* environment variables\n"),
2279  progname);
2280  }
2281  exit(1);
2282  }
2283 }
#define setlocale(a, b)
Definition: win32_port.h:419
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static const char * progname
Definition: initdb.c:150
#define free(a)
Definition: header.h:65
static char * locale
Definition: initdb.c:124
#define _(x)
Definition: elog.c:84

◆ check_need_password()

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

Definition at line 2452 of file initdb.c.

References _, progname, pwfilename, and pwprompt.

Referenced by main().

2453 {
2454  if ((strcmp(authmethodlocal, "md5") == 0 ||
2455  strcmp(authmethodlocal, "password") == 0 ||
2456  strcmp(authmethodlocal, "scram-sha-256") == 0) &&
2457  (strcmp(authmethodhost, "md5") == 0 ||
2458  strcmp(authmethodhost, "password") == 0 ||
2459  strcmp(authmethodhost, "scram-sha-256") == 0) &&
2460  !(pwprompt || pwfilename))
2461  {
2462  fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname,
2463  (strcmp(authmethodlocal, "md5") == 0 ||
2464  strcmp(authmethodlocal, "password") == 0 ||
2465  strcmp(authmethodlocal, "scram-sha-256") == 0)
2466  ? authmethodlocal
2467  : authmethodhost);
2468  exit(1);
2469  }
2470 }
static bool pwprompt
Definition: initdb.c:133
static const char * authmethodlocal
Definition: initdb.c:137
static char * pwfilename
Definition: initdb.c:134
static const char * progname
Definition: initdb.c:150
static const char * authmethodhost
Definition: initdb.c:136
#define _(x)
Definition: elog.c:84

◆ check_ok()

static void check_ok ( void  )
static

Definition at line 2126 of file initdb.c.

References _, caught_signal, exit_nicely(), output_errno, output_failed, 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_pg_role_prefix(), check_for_prepared_transactions(), check_for_reg_data_type_usage(), 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_cluster_analyze(), 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_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().

2127 {
2128  if (caught_signal)
2129  {
2130  printf(_("caught signal\n"));
2131  fflush(stdout);
2132  exit_nicely();
2133  }
2134  else if (output_failed)
2135  {
2136  printf(_("could not write to child process: %s\n"),
2138  fflush(stdout);
2139  exit_nicely();
2140  }
2141  else
2142  {
2143  /* all seems well */
2144  printf(_("ok\n"));
2145  fflush(stdout);
2146  }
2147 }
static bool caught_signal
Definition: initdb.c:168
static void exit_nicely(void)
Definition: initdb.c:240
static bool output_failed
Definition: initdb.c:169
static int output_errno
Definition: initdb.c:170
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ choose_dsm_implementation()

static char* choose_dsm_implementation ( void  )
static

Definition at line 920 of file initdb.c.

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

Referenced by test_config_settings().

921 {
922 #ifdef HAVE_SHM_OPEN
923  int ntries = 10;
924 
925  while (ntries > 0)
926  {
927  uint32 handle;
928  char name[64];
929  int fd;
930 
931  handle = random();
932  snprintf(name, 64, "/PostgreSQL.%u", handle);
933  if ((fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0600)) != -1)
934  {
935  close(fd);
936  shm_unlink(name);
937  return "posix";
938  }
939  if (errno != EEXIST)
940  break;
941  --ntries;
942  }
943 #endif
944 
945 #ifdef WIN32
946  return "windows";
947 #else
948  return "sysv";
949 #endif
950 }
long random(void)
Definition: random.c:22
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int fd(const char *x, int i)
Definition: preproc-init.c:105
unsigned int uint32
Definition: c.h:325
const char * name
Definition: encode.c:521
#define close(a)
Definition: win32.h:12

◆ create_data_directory()

void create_data_directory ( void  )

Definition at line 2759 of file initdb.c.

References _, check_ok(), exit_nicely(), found_existing_pgdata, made_new_pgdata, pg_check_dir(), pg_data, pg_dir_create_mode, pg_mkdir_p(), progname, strerror(), and warn_on_mount_point().

Referenced by initialize_data_directory().

2760 {
2761  int ret;
2762 
2763  switch ((ret = pg_check_dir(pg_data)))
2764  {
2765  case 0:
2766  /* PGDATA not there, must create it */
2767  printf(_("creating directory %s ... "),
2768  pg_data);
2769  fflush(stdout);
2770 
2772  {
2773  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
2774  progname, pg_data, strerror(errno));
2775  exit_nicely();
2776  }
2777  else
2778  check_ok();
2779 
2780  made_new_pgdata = true;
2781  break;
2782 
2783  case 1:
2784  /* Present but empty, fix permissions and use it */
2785  printf(_("fixing permissions on existing directory %s ... "),
2786  pg_data);
2787  fflush(stdout);
2788 
2789  if (chmod(pg_data, pg_dir_create_mode) != 0)
2790  {
2791  fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
2792  progname, pg_data, strerror(errno));
2793  exit_nicely();
2794  }
2795  else
2796  check_ok();
2797 
2798  found_existing_pgdata = true;
2799  break;
2800 
2801  case 2:
2802  case 3:
2803  case 4:
2804  /* Present and not empty */
2805  fprintf(stderr,
2806  _("%s: directory \"%s\" exists but is not empty\n"),
2807  progname, pg_data);
2808  if (ret != 4)
2809  warn_on_mount_point(ret);
2810  else
2811  fprintf(stderr,
2812  _("If you want to create a new database system, either remove or empty\n"
2813  "the directory \"%s\" or run %s\n"
2814  "with an argument other than \"%s\".\n"),
2816  exit(1); /* no further message needed */
2817 
2818  default:
2819  /* Trouble accessing directory */
2820  fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
2821  progname, pg_data, strerror(errno));
2822  exit_nicely();
2823  }
2824 }
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
void warn_on_mount_point(int error)
Definition: initdb.c:2938
static void exit_nicely(void)
Definition: initdb.c:240
static bool found_existing_pgdata
Definition: initdb.c:164
static char * pg_data
Definition: initdb.c:122
static void check_ok(void)
Definition: initdb.c:2126
static bool made_new_pgdata
Definition: initdb.c:163
static const char * progname
Definition: initdb.c:150
int pg_dir_create_mode
Definition: file_perm.c:18
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:31
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ create_xlog_or_symlink()

void create_xlog_or_symlink ( void  )

Definition at line 2829 of file initdb.c.

References _, canonicalize_path(), check_ok(), exit_nicely(), found_existing_xlogdir, free, is_absolute_path, made_new_xlogdir, mkdir, pg_check_dir(), pg_data, pg_dir_create_mode, pg_mkdir_p(), progname, psprintf(), strerror(), symlink, warn_on_mount_point(), and xlog_dir.

Referenced by initialize_data_directory().

2830 {
2831  char *subdirloc;
2832 
2833  /* form name of the place for the subdirectory or symlink */
2834  subdirloc = psprintf("%s/pg_wal", pg_data);
2835 
2836  if (xlog_dir)
2837  {
2838  int ret;
2839 
2840  /* clean up xlog directory name, check it's absolute */
2842  if (!is_absolute_path(xlog_dir))
2843  {
2844  fprintf(stderr, _("%s: WAL directory location must be an absolute path\n"), progname);
2845  exit_nicely();
2846  }
2847 
2848  /* check if the specified xlog directory exists/is empty */
2849  switch ((ret = pg_check_dir(xlog_dir)))
2850  {
2851  case 0:
2852  /* xlog directory not there, must create it */
2853  printf(_("creating directory %s ... "),
2854  xlog_dir);
2855  fflush(stdout);
2856 
2858  {
2859  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
2860  progname, xlog_dir, strerror(errno));
2861  exit_nicely();
2862  }
2863  else
2864  check_ok();
2865 
2866  made_new_xlogdir = true;
2867  break;
2868 
2869  case 1:
2870  /* Present but empty, fix permissions and use it */
2871  printf(_("fixing permissions on existing directory %s ... "),
2872  xlog_dir);
2873  fflush(stdout);
2874 
2875  if (chmod(xlog_dir, pg_dir_create_mode) != 0)
2876  {
2877  fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
2878  progname, xlog_dir, strerror(errno));
2879  exit_nicely();
2880  }
2881  else
2882  check_ok();
2883 
2884  found_existing_xlogdir = true;
2885  break;
2886 
2887  case 2:
2888  case 3:
2889  case 4:
2890  /* Present and not empty */
2891  fprintf(stderr,
2892  _("%s: directory \"%s\" exists but is not empty\n"),
2893  progname, xlog_dir);
2894  if (ret != 4)
2895  warn_on_mount_point(ret);
2896  else
2897  fprintf(stderr,
2898  _("If you want to store the WAL there, either remove or empty the directory\n"
2899  "\"%s\".\n"),
2900  xlog_dir);
2901  exit_nicely();
2902 
2903  default:
2904  /* Trouble accessing directory */
2905  fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
2906  progname, xlog_dir, strerror(errno));
2907  exit_nicely();
2908  }
2909 
2910 #ifdef HAVE_SYMLINK
2911  if (symlink(xlog_dir, subdirloc) != 0)
2912  {
2913  fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),
2914  progname, subdirloc, strerror(errno));
2915  exit_nicely();
2916  }
2917 #else
2918  fprintf(stderr, _("%s: symlinks are not supported on this platform"));
2919  exit_nicely();
2920 #endif
2921  }
2922  else
2923  {
2924  /* Without -X option, just make the subdirectory normally */
2925  if (mkdir(subdirloc, pg_dir_create_mode) < 0)
2926  {
2927  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
2928  progname, subdirloc, strerror(errno));
2929  exit_nicely();
2930  }
2931  }
2932 
2933  free(subdirloc);
2934 }
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
void warn_on_mount_point(int error)
Definition: initdb.c:2938
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static bool made_new_xlogdir
Definition: initdb.c:165
void canonicalize_path(char *path)
Definition: path.c:254
static void exit_nicely(void)
Definition: initdb.c:240
#define symlink(oldpath, newpath)
Definition: win32_port.h:232
#define is_absolute_path(filename)
Definition: port.h:86
static char * pg_data
Definition: initdb.c:122
static void check_ok(void)
Definition: initdb.c:2126
static bool found_existing_xlogdir
Definition: initdb.c:166
static const char * progname
Definition: initdb.c:150
int pg_dir_create_mode
Definition: file_perm.c:18
static char * xlog_dir
Definition: initdb.c:144
#define free(a)
Definition: header.h:65
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:31
const char * strerror(int errnum)
Definition: strerror.c:19
#define mkdir(a, b)
Definition: win32_port.h:58
#define _(x)
Definition: elog.c:84

◆ encodingid_to_string()

static char* encodingid_to_string ( int  enc)
static

Definition at line 682 of file initdb.c.

References pg_strdup().

Referenced by bootstrap_template1().

683 {
684  char result[20];
685 
686  sprintf(result, "%d", enc);
687  return pg_strdup(result);
688 }
struct pg_encoding enc
Definition: encode.c:522
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85

◆ escape_quotes_bki()

static char* escape_quotes_bki ( const char *  src)
static

Definition at line 355 of file initdb.c.

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

Referenced by bootstrap_template1().

356 {
357  char *result;
358  char *data = escape_quotes(src);
359  char *resultp;
360  char *datap;
361  int nquotes = 0;
362 
363  /* count double quotes in data */
364  datap = data;
365  while ((datap = strchr(datap, '"')) != NULL)
366  {
367  nquotes++;
368  datap++;
369  }
370 
371  result = (char *) pg_malloc(strlen(data) + 3 + nquotes * 3);
372  resultp = result;
373  *resultp++ = '"';
374  for (datap = data; *datap; datap++)
375  {
376  if (*datap == '"')
377  {
378  strcpy(resultp, "\\042");
379  resultp += 4;
380  }
381  else
382  *resultp++ = *datap;
383  }
384  *resultp++ = '"';
385  *resultp = '\0';
386 
387  free(data);
388  return result;
389 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static char * escape_quotes(const char *src)
#define free(a)
Definition: header.h:65

◆ exit_nicely()

static void exit_nicely ( void  )
static

Definition at line 240 of file initdb.c.

Referenced by bootstrap_template1(), check_ok(), create_data_directory(), create_xlog_or_symlink(), get_su_pwd(), initialize_data_directory(), main(), popen_check(), readfile(), set_null_conf(), setup_config(), write_version_file(), and writefile().

292  { \
293  cmdfd = popen_check(cmd, "w"); \
294  if (cmdfd == NULL) \
295  exit_nicely(); /* message already printed by popen_check */ \
296 } while (0)
297 
298 #define PG_CMD_CLOSE \
299 do { \
300  if (pclose_check(cmdfd)) \
301  exit_nicely(); /* message already printed by pclose_check */ \
302 } while (0)
303 
304 #define PG_CMD_PUTS(line) \
305 do { \
306  if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
307  output_failed = true, output_errno = errno; \
308 } while (0)
309 
310 #define PG_CMD_PRINTF1(fmt, arg1) \
311 do { \
312  if (fprintf(cmdfd, fmt, arg1) < 0 || fflush(cmdfd) < 0) \
313  output_failed = true, output_errno = errno; \
314 } while (0)
315 
316 #define PG_CMD_PRINTF2(fmt, arg1, arg2) \
317 do { \
318  if (fprintf(cmdfd, fmt, arg1, arg2) < 0 || fflush(cmdfd) < 0) \
319  output_failed = true, output_errno = errno; \
320 } while (0)
321 
322 #define PG_CMD_PRINTF3(fmt, arg1, arg2, arg3) \
323 do { \
324  if (fprintf(cmdfd, fmt, arg1, arg2, arg3) < 0 || fflush(cmdfd) < 0) \
325  output_failed = true, output_errno = errno; \
326 } while (0)
327 
328 /*
329  * Escape single quotes and backslashes, suitably for insertions into
330  * configuration files or SQL E'' strings.
331  */
332 static char *
333 escape_quotes(const char *src)
334 {
335  char *result = escape_single_quotes_ascii(src);
336 
337  if (!result)
338  {
339  fprintf(stderr, _("%s: out of memory\n"), progname);
340  exit(1);
341  }
342  return result;
343 }
static FILE * popen_check(const char *command, const char *mode)
Definition: initdb.c:577
static char * escape_quotes(const char *src)
char * escape_single_quotes_ascii(const char *src)
Definition: quotes.c:33
static const char * progname
Definition: initdb.c:150
#define _(x)
Definition: elog.c:84

◆ filter_lines_with_token()

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

Definition at line 455 of file initdb.c.

References i, and pg_malloc().

Referenced by setup_config().

456 {
457  int numlines = 1;
458  int i,
459  src,
460  dst;
461  char **result;
462 
463  for (i = 0; lines[i]; i++)
464  numlines++;
465 
466  result = (char **) pg_malloc(numlines * sizeof(char *));
467 
468  for (src = 0, dst = 0; src < numlines; src++)
469  {
470  if (lines[src] == NULL || strstr(lines[src], token) == NULL)
471  result[dst++] = lines[src];
472  }
473 
474  return result;
475 }
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 759 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().

760 {
761  int i;
762  char *langname,
763  *ptr;
764 
765  /*
766  * Convert lc_ctype to a language name by stripping everything after an
767  * underscore (usual case) or a hyphen (Windows "locale name"; see
768  * comments at IsoLocaleName()).
769  *
770  * XXX Should ' ' be a stop character? This would select "norwegian" for
771  * the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
772  * should also accept the "nn" and "nb" Unix locales.
773  *
774  * Just for paranoia, we also stop at '.' or '@'.
775  */
776  if (lc_type == NULL)
777  langname = pg_strdup("");
778  else
779  {
780  ptr = langname = pg_strdup(lc_type);
781  while (*ptr &&
782  *ptr != '_' && *ptr != '-' && *ptr != '.' && *ptr != '@')
783  ptr++;
784  *ptr = '\0';
785  }
786 
787  for (i = 0; tsearch_config_languages[i].tsconfname; i++)
788  {
789  if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
790  {
791  free(langname);
793  }
794  }
795 
796  free(langname);
797  return NULL;
798 }
static const struct tsearch_config_match tsearch_config_languages[]
Definition: initdb.c:718
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * tsconfname
Definition: initdb.c:714
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 694 of file initdb.c.

References _, enc, pg_valid_server_encoding(), and progname.

Referenced by setup_locale_encoding().

695 {
696  int enc;
697 
698  if (encoding_name && *encoding_name)
699  {
700  if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
701  return enc;
702  }
703  fprintf(stderr, _("%s: \"%s\" is not a valid server encoding name\n"),
704  progname, encoding_name ? encoding_name : "(null)");
705  exit(1);
706 }
int pg_valid_server_encoding(const char *name)
Definition: encnames.c:501
struct pg_encoding enc
Definition: encode.c:522
static const char * progname
Definition: initdb.c:150
#define _(x)
Definition: elog.c:84

◆ get_id()

static char* get_id ( void  )
static

Definition at line 659 of file initdb.c.

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

Referenced by main().

660 {
661  const char *username;
662 
663 #ifndef WIN32
664  if (geteuid() == 0) /* 0 is root's uid */
665  {
666  fprintf(stderr,
667  _("%s: cannot be run as root\n"
668  "Please log in (using, e.g., \"su\") as the "
669  "(unprivileged) user that will\n"
670  "own the server process.\n"),
671  progname);
672  exit(1);
673  }
674 #endif
675 
676  username = get_user_name_or_exit(progname);
677 
678  return pg_strdup(username);
679 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static const char * progname
Definition: initdb.c:150
static char * username
Definition: initdb.c:132
#define _(x)
Definition: elog.c:84
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 1503 of file initdb.c.

References _, exit_nicely(), i, pg_strdup(), progname, pwfilename, pwprompt, simple_prompt(), strerror(), and superuser_password.

Referenced by main().

1504 {
1505  char pwd1[100];
1506  char pwd2[100];
1507 
1508  if (pwprompt)
1509  {
1510  /*
1511  * Read password from terminal
1512  */
1513  printf("\n");
1514  fflush(stdout);
1515  simple_prompt("Enter new superuser password: ", pwd1, sizeof(pwd1), false);
1516  simple_prompt("Enter it again: ", pwd2, sizeof(pwd2), false);
1517  if (strcmp(pwd1, pwd2) != 0)
1518  {
1519  fprintf(stderr, _("Passwords didn't match.\n"));
1520  exit_nicely();
1521  }
1522  }
1523  else
1524  {
1525  /*
1526  * Read password from file
1527  *
1528  * Ideally this should insist that the file not be world-readable.
1529  * However, this option is mainly intended for use on Windows where
1530  * file permissions may not exist at all, so we'll skip the paranoia
1531  * for now.
1532  */
1533  FILE *pwf = fopen(pwfilename, "r");
1534  int i;
1535 
1536  if (!pwf)
1537  {
1538  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
1539  progname, pwfilename, strerror(errno));
1540  exit_nicely();
1541  }
1542  if (!fgets(pwd1, sizeof(pwd1), pwf))
1543  {
1544  if (ferror(pwf))
1545  fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
1546  progname, pwfilename, strerror(errno));
1547  else
1548  fprintf(stderr, _("%s: password file \"%s\" is empty\n"),
1549  progname, pwfilename);
1550  exit_nicely();
1551  }
1552  fclose(pwf);
1553 
1554  i = strlen(pwd1);
1555  while (i > 0 && (pwd1[i - 1] == '\r' || pwd1[i - 1] == '\n'))
1556  pwd1[--i] = '\0';
1557  }
1558 
1559  superuser_password = pg_strdup(pwd1);
1560 }
static void exit_nicely(void)
Definition: initdb.c:240
static bool pwprompt
Definition: initdb.c:133
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * pwfilename
Definition: initdb.c:134
static const char * progname
Definition: initdb.c:150
static char * superuser_password
Definition: initdb.c:135
int i
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ initialize_data_directory()

void initialize_data_directory ( void  )

Definition at line 2954 of file initdb.c.

References _, backend_exec, backend_options, bootstrap_template1(), check_ok(), create_data_directory(), create_xlog_or_symlink(), DEVNULL, exit_nicely(), 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_mode_mask, progname, psprintf(), set_null_conf(), setup_auth(), setup_collation(), setup_config(), setup_conversion(), setup_depend(), setup_description(), setup_dictionary(), setup_privileges(), setup_schema(), setup_signals(), setup_sysviews(), snprintf(), strerror(), subdirs, test_config_settings(), vacuum_db(), and write_version_file().

Referenced by main().

2955 {
2956  PG_CMD_DECL;
2957  int i;
2958 
2959  setup_signals();
2960 
2961  /*
2962  * Set mask based on requested PGDATA permissions. pg_mode_mask, and
2963  * friends like pg_dir_create_mode, are set to owner-only by default and
2964  * then updated if -g is passed in by calling SetDataDirectoryCreatePerm()
2965  * when parsing our options (see above).
2966  */
2967  umask(pg_mode_mask);
2968 
2970 
2972 
2973  /* Create required subdirectories (other than pg_wal) */
2974  printf(_("creating subdirectories ... "));
2975  fflush(stdout);
2976 
2977  for (i = 0; i < lengthof(subdirs); i++)
2978  {
2979  char *path;
2980 
2981  path = psprintf("%s/%s", pg_data, subdirs[i]);
2982 
2983  /*
2984  * The parent directory already exists, so we only need mkdir() not
2985  * pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
2986  */
2987  if (mkdir(path, pg_dir_create_mode) < 0)
2988  {
2989  fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"),
2990  progname, path, strerror(errno));
2991  exit_nicely();
2992  }
2993 
2994  free(path);
2995  }
2996 
2997  check_ok();
2998 
2999  /* Top level PG_VERSION is checked by bootstrapper, so make it first */
3000  write_version_file(NULL);
3001 
3002  /* Select suitable configuration settings */
3003  set_null_conf();
3005 
3006  /* Now create all the text config files */
3007  setup_config();
3008 
3009  /* Bootstrap template1 */
3011 
3012  /*
3013  * Make the per-database PG_VERSION for template1 only after init'ing it
3014  */
3015  write_version_file("base/1");
3016 
3017  /*
3018  * Create the stuff we don't need to use bootstrap mode for, using a
3019  * backend running in simple standalone mode.
3020  */
3021  fputs(_("performing post-bootstrap initialization ... "), stdout);
3022  fflush(stdout);
3023 
3024  snprintf(cmd, sizeof(cmd),
3025  "\"%s\" %s template1 >%s",
3027  DEVNULL);
3028 
3029  PG_CMD_OPEN;
3030 
3031  setup_auth(cmdfd);
3032 
3033  setup_depend(cmdfd);
3034 
3035  /*
3036  * Note that no objects created after setup_depend() will be "pinned".
3037  * They are all droppable at the whim of the DBA.
3038  */
3039 
3040  setup_sysviews(cmdfd);
3041 
3042  setup_description(cmdfd);
3043 
3044  setup_collation(cmdfd);
3045 
3046  setup_conversion(cmdfd);
3047 
3048  setup_dictionary(cmdfd);
3049 
3050  setup_privileges(cmdfd);
3051 
3052  setup_schema(cmdfd);
3053 
3054  load_plpgsql(cmdfd);
3055 
3056  vacuum_db(cmdfd);
3057 
3058  make_template0(cmdfd);
3059 
3060  make_postgres(cmdfd);
3061 
3062  PG_CMD_CLOSE;
3063 
3064  check_ok();
3065 }
static void setup_depend(FILE *cmdfd)
Definition: initdb.c:1566
static void vacuum_db(FILE *cmdfd)
Definition: initdb.c:2031
static const char * backend_options
Definition: initdb.c:199
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void setup_auth(FILE *cmdfd)
Definition: initdb.c:1479
static void setup_dictionary(FILE *cmdfd)
Definition: initdb.c:1774
void create_xlog_or_symlink(void)
Definition: initdb.c:2829
static void bootstrap_template1(void)
Definition: initdb.c:1377
static void exit_nicely(void)
Definition: initdb.c:240
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void setup_signals(void)
Definition: initdb.c:2730
#define lengthof(array)
Definition: c.h:629
static void set_null_conf(void)
Definition: initdb.c:886
#define PG_CMD_DECL
static const char *const subdirs[]
Definition: initdb.c:201
#define PG_CMD_OPEN
static void setup_config(void)
Definition: initdb.c:1077
static void load_plpgsql(FILE *cmdfd)
Definition: initdb.c:2022
static void write_version_file(const char *extrapath)
Definition: initdb.c:855
static void setup_description(FILE *cmdfd)
Definition: initdb.c:1681
static char * pg_data
Definition: initdb.c:122
#define DEVNULL
Definition: port.h:123
static void check_ok(void)
Definition: initdb.c:2126
static void test_config_settings(void)
Definition: initdb.c:960
static const char * progname
Definition: initdb.c:150
void create_data_directory(void)
Definition: initdb.c:2759
int pg_dir_create_mode
Definition: file_perm.c:18
#define PG_CMD_CLOSE
static void make_template0(FILE *cmdfd)
Definition: initdb.c:2041
static void setup_conversion(FILE *cmdfd)
Definition: initdb.c:1754
#define free(a)
Definition: header.h:65
static void setup_collation(FILE *cmdfd)
Definition: initdb.c:1736
static void setup_schema(FILE *cmdfd)
Definition: initdb.c:1989
static void setup_privileges(FILE *cmdfd)
Definition: initdb.c:1811
static void make_postgres(FILE *cmdfd)
Definition: initdb.c:2079
int i
const char * strerror(int errnum)
Definition: strerror.c:19
static char backend_exec[MAXPGPATH]
Definition: initdb.c:229
#define mkdir(a, b)
Definition: win32_port.h:58
static void setup_sysviews(FILE *cmdfd)
Definition: initdb.c:1659
#define _(x)
Definition: elog.c:84
int pg_mode_mask
Definition: file_perm.c:25

◆ load_plpgsql()

static void load_plpgsql ( FILE *  cmdfd)
static

Definition at line 2022 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

2023 {
2024  PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n\n");
2025 }
#define PG_CMD_PUTS(line)

◆ locale_date_order()

static int locale_date_order ( const char *  locale)
static

Definition at line 2160 of file initdb.c.

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

Referenced by setup_config().

2161 {
2162  struct tm testtime;
2163  char buf[128];
2164  char *posD;
2165  char *posM;
2166  char *posY;
2167  char *save;
2168  size_t res;
2169  int result;
2170 
2171  result = DATEORDER_MDY; /* default */
2172 
2173  save = setlocale(LC_TIME, NULL);
2174  if (!save)
2175  return result;
2176  save = pg_strdup(save);
2177 
2178  setlocale(LC_TIME, locale);
2179 
2180  memset(&testtime, 0, sizeof(testtime));
2181  testtime.tm_mday = 22;
2182  testtime.tm_mon = 10; /* November, should come out as "11" */
2183  testtime.tm_year = 133; /* 2033 */
2184 
2185  res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2186 
2187  setlocale(LC_TIME, save);
2188  free(save);
2189 
2190  if (res == 0)
2191  return result;
2192 
2193  posM = strstr(buf, "11");
2194  posD = strstr(buf, "22");
2195  posY = strstr(buf, "33");
2196 
2197  if (!posM || !posD || !posY)
2198  return result;
2199 
2200  if (posY < posM && posM < posD)
2201  result = DATEORDER_YMD;
2202  else if (posD < posM)
2203  result = DATEORDER_DMY;
2204  else
2205  result = DATEORDER_MDY;
2206 
2207  return result;
2208 }
#define setlocale(a, b)
Definition: win32_port.h:419
static struct pg_tm tm
Definition: localtime.c:106
static char * buf
Definition: pg_test_fsync.c:67
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define DATEORDER_YMD
Definition: miscadmin.h:218
static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
Definition: initdb.c:2151
#define DATEORDER_DMY
Definition: miscadmin.h:219
#define DATEORDER_MDY
Definition: miscadmin.h:220
#define free(a)
Definition: header.h:65
static char * locale
Definition: initdb.c:124

◆ main()

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

Definition at line 3069 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(), createPQExpBuffer(), PQExpBufferData::data, data_checksums, debug, default_text_search_config, DEFAULT_XLOG_SEG_SIZE, destroyPQExpBuffer(), do_sync, encoding, exit_nicely(), fsync_pgdata(), get_id(), get_parent_directory(), get_progname(), get_restricted_token(), get_su_pwd(), getopt_long(), initialize_data_directory(), IsValidWalSegSize, join_path_components(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, locale, MAXPGPATH, no_argument, noclean, optarg, optind, pg_check_dir(), pg_data, PG_DIR_MODE_GROUP, PG_IOLBF, pg_strdup(), PG_TEXTDOMAIN, pgdata_native, 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, str_wal_segment_size_mb, strerror(), strlcpy(), sync_only, usage(), username, wal_segment_size_mb, and xlog_dir.

3070 {
3071  static struct option long_options[] = {
3072  {"pgdata", required_argument, NULL, 'D'},
3073  {"encoding", required_argument, NULL, 'E'},
3074  {"locale", required_argument, NULL, 1},
3075  {"lc-collate", required_argument, NULL, 2},
3076  {"lc-ctype", required_argument, NULL, 3},
3077  {"lc-monetary", required_argument, NULL, 4},
3078  {"lc-numeric", required_argument, NULL, 5},
3079  {"lc-time", required_argument, NULL, 6},
3080  {"lc-messages", required_argument, NULL, 7},
3081  {"no-locale", no_argument, NULL, 8},
3082  {"text-search-config", required_argument, NULL, 'T'},
3083  {"auth", required_argument, NULL, 'A'},
3084  {"auth-local", required_argument, NULL, 10},
3085  {"auth-host", required_argument, NULL, 11},
3086  {"pwprompt", no_argument, NULL, 'W'},
3087  {"pwfile", required_argument, NULL, 9},
3088  {"username", required_argument, NULL, 'U'},
3089  {"help", no_argument, NULL, '?'},
3090  {"version", no_argument, NULL, 'V'},
3091  {"debug", no_argument, NULL, 'd'},
3092  {"show", no_argument, NULL, 's'},
3093  {"noclean", no_argument, NULL, 'n'}, /* for backwards compatibility */
3094  {"no-clean", no_argument, NULL, 'n'},
3095  {"nosync", no_argument, NULL, 'N'}, /* for backwards compatibility */
3096  {"no-sync", no_argument, NULL, 'N'},
3097  {"sync-only", no_argument, NULL, 'S'},
3098  {"waldir", required_argument, NULL, 'X'},
3099  {"wal-segsize", required_argument, NULL, 12},
3100  {"data-checksums", no_argument, NULL, 'k'},
3101  {"allow-group-access", no_argument, NULL, 'g'},
3102  {NULL, 0, NULL, 0}
3103  };
3104 
3105  /*
3106  * options with no short version return a low integer, the rest return
3107  * their short version value
3108  */
3109  int c;
3110  int option_index;
3111  char *effective_user;
3112  PQExpBuffer start_db_cmd;
3113  char pg_ctl_path[MAXPGPATH];
3114 
3115  /*
3116  * Ensure that buffering behavior of stdout and stderr matches what it is
3117  * in interactive usage (at least on most platforms). This prevents
3118  * unexpected output ordering when, eg, output is redirected to a file.
3119  * POSIX says we must do this before any other usage of these files.
3120  */
3121  setvbuf(stdout, NULL, PG_IOLBF, 0);
3122  setvbuf(stderr, NULL, _IONBF, 0);
3123 
3124  progname = get_progname(argv[0]);
3125  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
3126 
3127  if (argc > 1)
3128  {
3129  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
3130  {
3131  usage(progname);
3132  exit(0);
3133  }
3134  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
3135  {
3136  puts("initdb (PostgreSQL) " PG_VERSION);
3137  exit(0);
3138  }
3139  }
3140 
3141  /* process command-line options */
3142 
3143  while ((c = getopt_long(argc, argv, "dD:E:kL:nNU:WA:sST:X:g", long_options, &option_index)) != -1)
3144  {
3145  switch (c)
3146  {
3147  case 'A':
3149 
3150  /*
3151  * When ident is specified, use peer for local connections.
3152  * Mirrored, when peer is specified, use ident for TCP/IP
3153  * connections.
3154  */
3155  if (strcmp(authmethodhost, "ident") == 0)
3156  authmethodlocal = "peer";
3157  else if (strcmp(authmethodlocal, "peer") == 0)
3158  authmethodhost = "ident";
3159  break;
3160  case 10:
3162  break;
3163  case 11:
3165  break;
3166  case 'D':
3168  break;
3169  case 'E':
3171  break;
3172  case 'W':
3173  pwprompt = true;
3174  break;
3175  case 'U':
3177  break;
3178  case 'd':
3179  debug = true;
3180  printf(_("Running in debug mode.\n"));
3181  break;
3182  case 'n':
3183  noclean = true;
3184  printf(_("Running in no-clean mode. Mistakes will not be cleaned up.\n"));
3185  break;
3186  case 'N':
3187  do_sync = false;
3188  break;
3189  case 'S':
3190  sync_only = true;
3191  break;
3192  case 'k':
3193  data_checksums = true;
3194  break;
3195  case 'L':
3197  break;
3198  case 1:
3199  locale = pg_strdup(optarg);
3200  break;
3201  case 2:
3203  break;
3204  case 3:
3206  break;
3207  case 4:
3209  break;
3210  case 5:
3212  break;
3213  case 6:
3215  break;
3216  case 7:
3218  break;
3219  case 8:
3220  locale = "C";
3221  break;
3222  case 9:
3224  break;
3225  case 's':
3226  show_setting = true;
3227  break;
3228  case 'T':
3230  break;
3231  case 'X':
3233  break;
3234  case 12:
3236  break;
3237  case 'g':
3239  break;
3240  default:
3241  /* getopt_long already emitted a complaint */
3242  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
3243  progname);
3244  exit(1);
3245  }
3246  }
3247 
3248 
3249  /*
3250  * Non-option argument specifies data directory as long as it wasn't
3251  * already specified with -D / --pgdata
3252  */
3253  if (optind < argc && !pg_data)
3254  {
3255  pg_data = pg_strdup(argv[optind]);
3256  optind++;
3257  }
3258 
3259  if (optind < argc)
3260  {
3261  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
3262  progname, argv[optind]);
3263  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
3264  progname);
3265  exit(1);
3266  }
3267 
3268  /* If we only need to fsync, just do it and exit */
3269  if (sync_only)
3270  {
3271  setup_pgdata();
3272 
3273  /* must check that directory is readable */
3274  if (pg_check_dir(pg_data) <= 0)
3275  {
3276  fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
3277  progname, pg_data, strerror(errno));
3278  exit_nicely();
3279  }
3280 
3281  fputs(_("syncing data to disk ... "), stdout);
3282  fflush(stdout);
3283  fsync_pgdata(pg_data, progname, PG_VERSION_NUM);
3284  check_ok();
3285  return 0;
3286  }
3287 
3288  if (pwprompt && pwfilename)
3289  {
3290  fprintf(stderr, _("%s: password prompt and password file cannot be specified together\n"), progname);
3291  exit(1);
3292  }
3293 
3296 
3299 
3301 
3302  /* set wal segment size */
3303  if (str_wal_segment_size_mb == NULL)
3304  wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024);
3305  else
3306  {
3307  char *endptr;
3308 
3309  /* check that the argument is a number */
3310  wal_segment_size_mb = strtol(str_wal_segment_size_mb, &endptr, 10);
3311 
3312  /* verify that wal segment size is valid */
3313  if (endptr == str_wal_segment_size_mb || *endptr != '\0')
3314  {
3315  fprintf(stderr,
3316  _("%s: argument of --wal-segsize must be a number\n"),
3317  progname);
3318  exit(1);
3319  }
3320  if (!IsValidWalSegSize(wal_segment_size_mb * 1024 * 1024))
3321  {
3322  fprintf(stderr,
3323  _("%s: argument of --wal-segsize must be a power of 2 between 1 and 1024\n"),
3324  progname);
3325  exit(1);
3326  }
3327  }
3328 
3330 
3331  setup_pgdata();
3332 
3333  setup_bin_paths(argv[0]);
3334 
3335  effective_user = get_id();
3336  if (!username)
3337  username = effective_user;
3338 
3339  if (strncmp(username, "pg_", 3) == 0)
3340  {
3341  fprintf(stderr, _("%s: superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"\n"), progname, username);
3342  exit(1);
3343  }
3344 
3345  printf(_("The files belonging to this database system will be owned "
3346  "by user \"%s\".\n"
3347  "This user must also own the server process.\n\n"),
3348  effective_user);
3349 
3350  set_info_version();
3351 
3353 
3355 
3357 
3358  printf("\n");
3359 
3360  if (data_checksums)
3361  printf(_("Data page checksums are enabled.\n"));
3362  else
3363  printf(_("Data page checksums are disabled.\n"));
3364 
3365  if (pwprompt || pwfilename)
3366  get_su_pwd();
3367 
3368  printf("\n");
3369 
3371 
3372  if (do_sync)
3373  {
3374  fputs(_("syncing data to disk ... "), stdout);
3375  fflush(stdout);
3376  fsync_pgdata(pg_data, progname, PG_VERSION_NUM);
3377  check_ok();
3378  }
3379  else
3380  printf(_("\nSync to disk skipped.\nThe data directory might become corrupt if the operating system crashes.\n"));
3381 
3382  if (authwarning != NULL)
3383  fprintf(stderr, "%s", authwarning);
3384 
3385  /*
3386  * Build up a shell command to tell the user how to start the server
3387  */
3388  start_db_cmd = createPQExpBuffer();
3389 
3390  /* Get directory specification used to start initdb ... */
3391  strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
3392  canonicalize_path(pg_ctl_path);
3393  get_parent_directory(pg_ctl_path);
3394  /* ... and tag on pg_ctl instead */
3395  join_path_components(pg_ctl_path, pg_ctl_path, "pg_ctl");
3396 
3397  /* path to pg_ctl, properly quoted */
3398  appendShellString(start_db_cmd, pg_ctl_path);
3399 
3400  /* add -D switch, with properly quoted data directory */
3401  appendPQExpBufferStr(start_db_cmd, " -D ");
3402  appendShellString(start_db_cmd, pgdata_native);
3403 
3404  /* add suggested -l switch and "start" command */
3405  /* translator: This is a placeholder in a shell command. */
3406  appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
3407 
3408  printf(_("\nSuccess. You can now start the database server using:\n\n"
3409  " %s\n\n"),
3410  start_db_cmd->data);
3411 
3412  destroyPQExpBuffer(start_db_cmd);
3413 
3414  return 0;
3415 }
static char * lc_monetary
Definition: initdb.c:127
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
void setup_data_file_paths(void)
Definition: initdb.c:2647
void setup_locale_encoding(void)
Definition: initdb.c:2562
void fsync_pgdata(const char *pg_data, const char *progname, int serverVersion)
Definition: file_utils.c:58
static bool debug
Definition: initdb.c:138
static char * lc_collate
Definition: initdb.c:125
const char * get_progname(const char *argv0)
Definition: path.c:453
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:385
#define DEFAULT_XLOG_SEG_SIZE
void canonicalize_path(char *path)
Definition: path.c:254
static void exit_nicely(void)
Definition: initdb.c:240
static bool pwprompt
Definition: initdb.c:133
static char * pgdata_native
Definition: initdb.c:171
static void get_su_pwd(void)
Definition: initdb.c:1503
static char * share_path
Definition: initdb.c:119
void get_restricted_token(const char *progname)
static const char * authmethodlocal
Definition: initdb.c:137
static char * lc_time
Definition: initdb.c:129
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static void check_authmethod_unspecified(const char **authmethod)
Definition: initdb.c:2420
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:51
static char * lc_messages
Definition: initdb.c:130
#define MAXPGPATH
static const char * default_text_search_config
Definition: initdb.c:131
static char * lc_ctype
Definition: initdb.c:126
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * lc_numeric
Definition: initdb.c:128
char * c
void initialize_data_directory(void)
Definition: initdb.c:2954
static int wal_segment_size_mb
Definition: initdb.c:146
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:122
static char * pwfilename
Definition: initdb.c:134
static void check_ok(void)
Definition: initdb.c:2126
void setup_bin_paths(const char *argv0)
Definition: initdb.c:2514
static const char * progname
Definition: initdb.c:150
#define no_argument
Definition: getopt_long.h:24
static char * xlog_dir
Definition: initdb.c:144
#define PG_DIR_MODE_GROUP
Definition: file_perm.h:35
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1054
static bool data_checksums
Definition: initdb.c:143
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define PG_IOLBF
Definition: port.h:304
static char * username
Definition: initdb.c:132
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:434
static char * encoding
Definition: initdb.c:123
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static char * authwarning
Definition: initdb.c:186
void setup_text_search(void)
Definition: initdb.c:2695
static char * str_wal_segment_size_mb
Definition: initdb.c:145
static bool do_sync
Definition: initdb.c:140
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:80
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * optarg
Definition: getopt.c:53
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:31
static void usage(const char *progname)
Definition: initdb.c:2378
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
const char * strerror(int errnum)
Definition: strerror.c:19
static void check_need_password(const char *authmethodlocal, const char *authmethodhost)
Definition: initdb.c:2452
static char * locale
Definition: initdb.c:124
static const char * authmethodhost
Definition: initdb.c:136
static bool sync_only
Definition: initdb.c:141
static char * get_id(void)
Definition: initdb.c:659
static const char *const auth_methods_local[]
Definition: initdb.c:102
static void check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
Definition: initdb.c:2432
void setup_pgdata(void)
Definition: initdb.c:2474
#define _(x)
Definition: elog.c:84
static void set_info_version(void)
Definition: initdb.c:1962
static bool show_setting
Definition: initdb.c:142
static bool noclean
Definition: initdb.c:139

◆ make_postgres()

static void make_postgres ( FILE *  cmdfd)
static

Definition at line 2079 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

2080 {
2081  const char *const *line;
2082  static const char *const postgres_setup[] = {
2083  "CREATE DATABASE postgres;\n\n",
2084  "COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
2085  NULL
2086  };
2087 
2088  for (line = postgres_setup; *line; line++)
2089  PG_CMD_PUTS(*line);
2090 }
#define PG_CMD_PUTS(line)

◆ make_template0()

static void make_template0 ( FILE *  cmdfd)
static

Definition at line 2041 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

2042 {
2043  const char *const *line;
2044  static const char *const template0_setup[] = {
2045  "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
2046 
2047  /*
2048  * We use the OID of template0 to determine lastsysoid
2049  */
2050  "UPDATE pg_database SET datlastsysoid = "
2051  " (SELECT oid FROM pg_database "
2052  " WHERE datname = 'template0');\n\n",
2053 
2054  /*
2055  * Explicitly revoke public create-schema and create-temp-table
2056  * privileges in template1 and template0; else the latter would be on
2057  * by default
2058  */
2059  "REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n",
2060  "REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n",
2061 
2062  "COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n",
2063 
2064  /*
2065  * Finally vacuum to clean up dead rows in pg_database
2066  */
2067  "VACUUM pg_database;\n\n",
2068  NULL
2069  };
2070 
2071  for (line = template0_setup; *line; line++)
2072  PG_CMD_PUTS(*line);
2073 }
#define PG_CMD_PUTS(line)

◆ my_strftime()

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

Definition at line 2151 of file initdb.c.

Referenced by locale_date_order().

2152 {
2153  return strftime(s, max, fmt, tm);
2154 }
static struct pg_tm tm
Definition: localtime.c:106

◆ popen_check()

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

Definition at line 577 of file initdb.c.

References _, exit_nicely(), found_existing_pgdata, found_existing_xlogdir, made_new_pgdata, made_new_xlogdir, noclean, pg_data, progname, rmtree(), strerror(), and xlog_dir.

578 {
579  FILE *cmdfd;
580 
581  fflush(stdout);
582  fflush(stderr);
583  errno = 0;
584  cmdfd = popen(command, mode);
585  if (cmdfd == NULL)
586  fprintf(stderr, _("%s: could not execute command \"%s\": %s\n"),
587  progname, command, strerror(errno));
588  return cmdfd;
589 }
static const char * progname
Definition: initdb.c:150
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ pretty_wal_size()

static char* pretty_wal_size ( int  segment_count)
static

Definition at line 1060 of file initdb.c.

References pg_malloc(), snprintf(), and wal_segment_size_mb.

Referenced by setup_config().

1061 {
1062  int sz = wal_segment_size_mb * segment_count;
1063  char *result = pg_malloc(14);
1064 
1065  if ((sz % 1024) == 0)
1066  snprintf(result, 14, "%dGB", sz / 1024);
1067  else
1068  snprintf(result, 14, "%dMB", sz);
1069 
1070  return result;
1071 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int wal_segment_size_mb
Definition: initdb.c:146

◆ readfile()

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

Definition at line 482 of file initdb.c.

References _, buffer, exit_nicely(), free, infile(), pg_malloc(), pg_strdup(), progname, and strerror().

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

483 {
484  FILE *infile;
485  int maxlength = 1,
486  linelen = 0;
487  int nlines = 0;
488  int n;
489  char **result;
490  char *buffer;
491  int c;
492 
493  if ((infile = fopen(path, "r")) == NULL)
494  {
495  fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
496  progname, path, strerror(errno));
497  exit_nicely();
498  }
499 
500  /* pass over the file twice - the first time to size the result */
501 
502  while ((c = fgetc(infile)) != EOF)
503  {
504  linelen++;
505  if (c == '\n')
506  {
507  nlines++;
508  if (linelen > maxlength)
509  maxlength = linelen;
510  linelen = 0;
511  }
512  }
513 
514  /* handle last line without a terminating newline (yuck) */
515  if (linelen)
516  nlines++;
517  if (linelen > maxlength)
518  maxlength = linelen;
519 
520  /* set up the result and the line buffer */
521  result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
522  buffer = (char *) pg_malloc(maxlength + 1);
523 
524  /* now reprocess the file and store the lines */
525  rewind(infile);
526  n = 0;
527  while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines)
528  result[n++] = pg_strdup(buffer);
529 
530  fclose(infile);
531  free(buffer);
532  result[n] = NULL;
533 
534  return result;
535 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void exit_nicely(void)
Definition: initdb.c:240
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static const char * progname
Definition: initdb.c:150
static void infile(const char *filename)
Definition: zic.c:1182
#define free(a)
Definition: header.h:65
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ replace_token()

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

Definition at line 399 of file initdb.c.

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

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

400 {
401  int numlines = 1;
402  int i;
403  char **result;
404  int toklen,
405  replen,
406  diff;
407 
408  for (i = 0; lines[i]; i++)
409  numlines++;
410 
411  result = (char **) pg_malloc(numlines * sizeof(char *));
412 
413  toklen = strlen(token);
414  replen = strlen(replacement);
415  diff = replen - toklen;
416 
417  for (i = 0; i < numlines; i++)
418  {
419  char *where;
420  char *newline;
421  int pre;
422 
423  /* just copy pointer if NULL or no change needed */
424  if (lines[i] == NULL || (where = strstr(lines[i], token)) == NULL)
425  {
426  result[i] = lines[i];
427  continue;
428  }
429 
430  /* if we get here a change is needed - set up new line */
431 
432  newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
433 
434  pre = where - lines[i];
435 
436  memcpy(newline, lines[i], pre);
437 
438  memcpy(newline + pre, replacement, replen);
439 
440  strcpy(newline + pre + replen, lines[i] + pre + toklen);
441 
442  result[i] = newline;
443  }
444 
445  return result;
446 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static chr newline(void)
Definition: regc_lex.c:1138
int i

◆ select_default_timezone()

const char* select_default_timezone ( const char *  share_path)

Definition at line 1375 of file findtimezone.c.

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

Referenced by setup_config().

1376 {
1377  const char *tzname;
1378 
1379  /* Initialize timezone directory path, if needed */
1380 #ifndef SYSTEMTZDIR
1381  snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1382 #endif
1383 
1384  /* Check TZ environment variable */
1385  tzname = getenv("TZ");
1386  if (validate_zone(tzname))
1387  return tzname;
1388 
1389  /* Nope, so try to identify the system timezone */
1390  tzname = identify_system_timezone();
1391  if (validate_zone(tzname))
1392  return tzname;
1393 
1394  return NULL;
1395 }
static bool validate_zone(const char *tzname)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * share_path
Definition: initdb.c:119
static char tzdirpath[MAXPGPATH]
Definition: findtimezone.c:26
static const char * identify_system_timezone(void)
Definition: findtimezone.c:310

◆ set_info_version()

static void set_info_version ( void  )
static

Definition at line 1962 of file initdb.c.

References infoversion, pg_strdup(), and snprintf().

Referenced by main().

1963 {
1964  char *letterversion;
1965  long major = 0,
1966  minor = 0,
1967  micro = 0;
1968  char *endptr;
1969  char *vstr = pg_strdup(PG_VERSION);
1970  char *ptr;
1971 
1972  ptr = vstr + (strlen(vstr) - 1);
1973  while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1974  ptr--;
1975  letterversion = ptr + 1;
1976  major = strtol(vstr, &endptr, 10);
1977  if (*endptr)
1978  minor = strtol(endptr + 1, &endptr, 10);
1979  if (*endptr)
1980  micro = strtol(endptr + 1, &endptr, 10);
1981  snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1982  major, minor, micro, letterversion);
1983 }
static char infoversion[100]
Definition: initdb.c:167
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85

◆ set_input()

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

Definition at line 805 of file initdb.c.

References psprintf(), and share_path.

Referenced by setup_data_file_paths().

806 {
807  *dest = psprintf("%s/%s", share_path, filename);
808 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static char * share_path
Definition: initdb.c:119
static char * filename
Definition: pg_dumpall.c:87

◆ set_null_conf()

static void set_null_conf ( void  )
static

Definition at line 886 of file initdb.c.

References _, conf_file, exit_nicely(), free, PG_BINARY_W, pg_data, progname, psprintf(), and strerror().

Referenced by initialize_data_directory().

887 {
888  FILE *conf_file;
889  char *path;
890 
891  path = psprintf("%s/postgresql.conf", pg_data);
892  conf_file = fopen(path, PG_BINARY_W);
893  if (conf_file == NULL)
894  {
895  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
896  progname, path, strerror(errno));
897  exit_nicely();
898  }
899  if (fclose(conf_file))
900  {
901  fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
902  progname, path, strerror(errno));
903  exit_nicely();
904  }
905  free(path);
906 }
static char * conf_file
Definition: initdb.c:157
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_BINARY_W
Definition: c.h:1083
static void exit_nicely(void)
Definition: initdb.c:240
static char * pg_data
Definition: initdb.c:122
static const char * progname
Definition: initdb.c:150
#define free(a)
Definition: header.h:65
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

◆ setlocales()

static void setlocales ( void  )
static

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

2328 {
2329  char *canonname;
2330 
2331  /* set empty lc_* values to locale config if set */
2332 
2333  if (locale)
2334  {
2335  if (!lc_ctype)
2336  lc_ctype = locale;
2337  if (!lc_collate)
2338  lc_collate = locale;
2339  if (!lc_numeric)
2340  lc_numeric = locale;
2341  if (!lc_time)
2342  lc_time = locale;
2343  if (!lc_monetary)
2344  lc_monetary = locale;
2345  if (!lc_messages)
2346  lc_messages = locale;
2347  }
2348 
2349  /*
2350  * canonicalize locale names, and obtain any missing values from our
2351  * current environment
2352  */
2353 
2354  check_locale_name(LC_CTYPE, lc_ctype, &canonname);
2355  lc_ctype = canonname;
2356  check_locale_name(LC_COLLATE, lc_collate, &canonname);
2357  lc_collate = canonname;
2358  check_locale_name(LC_NUMERIC, lc_numeric, &canonname);
2359  lc_numeric = canonname;
2360  check_locale_name(LC_TIME, lc_time, &canonname);
2361  lc_time = canonname;
2362  check_locale_name(LC_MONETARY, lc_monetary, &canonname);
2363  lc_monetary = canonname;
2364 #if defined(LC_MESSAGES) && !defined(WIN32)
2365  check_locale_name(LC_MESSAGES, lc_messages, &canonname);
2366  lc_messages = canonname;
2367 #else
2368  /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2369  check_locale_name(LC_CTYPE, lc_messages, &canonname);
2370  lc_messages = canonname;
2371 #endif
2372 }
static char * lc_monetary
Definition: initdb.c:127
static char * lc_collate
Definition: initdb.c:125
static char * lc_time
Definition: initdb.c:129
static char * lc_messages
Definition: initdb.c:130
static char * lc_ctype
Definition: initdb.c:126
static char * lc_numeric
Definition: initdb.c:128
static void check_locale_name(int category, const char *locale, char **canonname)
Definition: initdb.c:2223
static char * locale
Definition: initdb.c:124

◆ setup_auth()

static void setup_auth ( FILE *  cmdfd)
static

Definition at line 1479 of file initdb.c.

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

Referenced by initialize_data_directory().

1480 {
1481  const char *const *line;
1482  static const char *const pg_authid_setup[] = {
1483  /*
1484  * The authid table shouldn't be readable except through views, to
1485  * ensure passwords are not publicly visible.
1486  */
1487  "REVOKE ALL on pg_authid FROM public;\n\n",
1488  NULL
1489  };
1490 
1491  for (line = pg_authid_setup; *line != NULL; line++)
1492  PG_CMD_PUTS(*line);
1493 
1494  if (superuser_password)
1495  PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1497 }
static char * escape_quotes(const char *src)
#define PG_CMD_PUTS(line)
static char * username
Definition: initdb.c:132
static char * superuser_password
Definition: initdb.c:135
#define PG_CMD_PRINTF2(fmt, arg1, arg2)

◆ setup_bin_paths()

void setup_bin_paths ( const char *  argv0)

Definition at line 2514 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_malloc(), progname, share_path, and strlcpy().

Referenced by main().

2515 {
2516  int ret;
2517 
2518  if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
2519  backend_exec)) < 0)
2520  {
2521  char full_path[MAXPGPATH];
2522 
2523  if (find_my_exec(argv0, full_path) < 0)
2524  strlcpy(full_path, progname, sizeof(full_path));
2525 
2526  if (ret == -1)
2527  fprintf(stderr,
2528  _("The program \"postgres\" is needed by %s "
2529  "but was not found in the\n"
2530  "same directory as \"%s\".\n"
2531  "Check your installation.\n"),
2532  progname, full_path);
2533  else
2534  fprintf(stderr,
2535  _("The program \"postgres\" was found by \"%s\"\n"
2536  "but was not the same version as %s.\n"
2537  "Check your installation.\n"),
2538  full_path, progname);
2539  exit(1);
2540  }
2541 
2542  /* store binary directory */
2543  strcpy(bin_path, backend_exec);
2544  *last_dir_separator(bin_path) = '\0';
2546 
2547  if (!share_path)
2548  {
2551  }
2552  else if (!is_absolute_path(share_path))
2553  {
2554  fprintf(stderr, _("%s: input file location must be an absolute path\n"), progname);
2555  exit(1);
2556  }
2557 
2559 }
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:307
static char * argv0
Definition: pg_ctl.c:94
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void canonicalize_path(char *path)
Definition: path.c:254
static char * share_path
Definition: initdb.c:119
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:119
#define is_absolute_path(filename)
Definition: port.h:86
static const char * progname
Definition: initdb.c:150
char * last_dir_separator(const char *filename)
Definition: path.c:138
#define PG_BACKEND_VERSIONSTR
Definition: port.h:111
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static char bin_path[MAXPGPATH]
Definition: initdb.c:228
static char backend_exec[MAXPGPATH]
Definition: initdb.c:229
void get_share_path(const char *my_exec_path, char *ret_path)
Definition: path.c:704
#define _(x)
Definition: elog.c:84

◆ setup_collation()

static void setup_collation ( FILE *  cmdfd)
static

Definition at line 1736 of file initdb.c.

References PG_CMD_PRINTF3, PG_CMD_PUTS, and PG_UTF8.

Referenced by initialize_data_directory().

1737 {
1738  /*
1739  * Add an SQL-standard name. We don't want to pin this, so it doesn't go
1740  * in pg_collation.h. But add it before reading system collations, so
1741  * that it wins if libc defines a locale named ucs_basic.
1742  */
1743  PG_CMD_PRINTF3("INSERT INTO pg_collation (collname, collnamespace, collowner, collprovider, collencoding, collcollate, collctype) VALUES ('ucs_basic', 'pg_catalog'::regnamespace, %u, '%c', %d, 'C', 'C');\n\n",
1744  BOOTSTRAP_SUPERUSERID, COLLPROVIDER_LIBC, PG_UTF8);
1745 
1746  /* Now import all collations we can find in the operating system */
1747  PG_CMD_PUTS("SELECT pg_import_system_collations('pg_catalog');\n\n");
1748 }
#define PG_CMD_PRINTF3(fmt, arg1, arg2, arg3)
#define PG_CMD_PUTS(line)

◆ setup_config()

static void setup_config ( void  )
static

Definition at line 1077 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, dynamic_shared_memory_type, escape_quotes(), exit_nicely(), 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_strdup(), pretty_wal_size(), progname, readfile(), replace_token(), select_default_timezone(), share_path, snprintf(), strerror(), and writefile().

Referenced by initialize_data_directory().

1078 {
1079  char **conflines;
1080  char repltok[MAXPGPATH];
1081  char path[MAXPGPATH];
1082  const char *default_timezone;
1083  char *autoconflines[3];
1084 
1085  fputs(_("creating configuration files ... "), stdout);
1086  fflush(stdout);
1087 
1088  /* postgresql.conf */
1089 
1090  conflines = readfile(conf_file);
1091 
1092  snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
1093  conflines = replace_token(conflines, "#max_connections = 100", repltok);
1094 
1095  if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1096  snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
1097  (n_buffers * (BLCKSZ / 1024)) / 1024);
1098  else
1099  snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
1100  n_buffers * (BLCKSZ / 1024));
1101  conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
1102 
1103 #ifdef HAVE_UNIX_SOCKETS
1104  snprintf(repltok, sizeof(repltok), "#unix_socket_directories = '%s'",
1106 #else
1107  snprintf(repltok, sizeof(repltok), "#unix_socket_directories = ''");
1108 #endif
1109  conflines = replace_token(conflines, "#unix_socket_directories = '/tmp'",
1110  repltok);
1111 
1112 #if DEF_PGPORT != 5432
1113  snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
1114  conflines = replace_token(conflines, "#port = 5432", repltok);
1115 #endif
1116 
1117  /* set default max_wal_size and min_wal_size */
1118  snprintf(repltok, sizeof(repltok), "min_wal_size = %s",
1120  conflines = replace_token(conflines, "#min_wal_size = 80MB", repltok);
1121 
1122  snprintf(repltok, sizeof(repltok), "max_wal_size = %s",
1124  conflines = replace_token(conflines, "#max_wal_size = 1GB", repltok);
1125 
1126  snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
1128  conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
1129 
1130  snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
1132  conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
1133 
1134  snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
1136  conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
1137 
1138  snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
1140  conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
1141 
1142  switch (locale_date_order(lc_time))
1143  {
1144  case DATEORDER_YMD:
1145  strcpy(repltok, "datestyle = 'iso, ymd'");
1146  break;
1147  case DATEORDER_DMY:
1148  strcpy(repltok, "datestyle = 'iso, dmy'");
1149  break;
1150  case DATEORDER_MDY:
1151  default:
1152  strcpy(repltok, "datestyle = 'iso, mdy'");
1153  break;
1154  }
1155  conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);
1156 
1157  snprintf(repltok, sizeof(repltok),
1158  "default_text_search_config = 'pg_catalog.%s'",
1160  conflines = replace_token(conflines,
1161  "#default_text_search_config = 'pg_catalog.simple'",
1162  repltok);
1163 
1164  default_timezone = select_default_timezone(share_path);
1165  if (default_timezone)
1166  {
1167  snprintf(repltok, sizeof(repltok), "timezone = '%s'",
1168  escape_quotes(default_timezone));
1169  conflines = replace_token(conflines, "#timezone = 'GMT'", repltok);
1170  snprintf(repltok, sizeof(repltok), "log_timezone = '%s'",
1171  escape_quotes(default_timezone));
1172  conflines = replace_token(conflines, "#log_timezone = 'GMT'", repltok);
1173  }
1174 
1175  snprintf(repltok, sizeof(repltok), "dynamic_shared_memory_type = %s",
1177  conflines = replace_token(conflines, "#dynamic_shared_memory_type = posix",
1178  repltok);
1179 
1180 #if DEFAULT_BACKEND_FLUSH_AFTER > 0
1181  snprintf(repltok, sizeof(repltok), "#backend_flush_after = %dkB",
1182  DEFAULT_BACKEND_FLUSH_AFTER * (BLCKSZ / 1024));
1183  conflines = replace_token(conflines, "#backend_flush_after = 0",
1184  repltok);
1185 #endif
1186 
1187 #if DEFAULT_BGWRITER_FLUSH_AFTER > 0
1188  snprintf(repltok, sizeof(repltok), "#bgwriter_flush_after = %dkB",
1189  DEFAULT_BGWRITER_FLUSH_AFTER * (BLCKSZ / 1024));
1190  conflines = replace_token(conflines, "#bgwriter_flush_after = 0",
1191  repltok);
1192 #endif
1193 
1194 #if DEFAULT_CHECKPOINT_FLUSH_AFTER > 0
1195  snprintf(repltok, sizeof(repltok), "#checkpoint_flush_after = %dkB",
1196  DEFAULT_CHECKPOINT_FLUSH_AFTER * (BLCKSZ / 1024));
1197  conflines = replace_token(conflines, "#checkpoint_flush_after = 0",
1198  repltok);
1199 #endif
1200 
1201 #ifndef USE_PREFETCH
1202  conflines = replace_token(conflines,
1203  "#effective_io_concurrency = 1",
1204  "#effective_io_concurrency = 0");
1205 #endif
1206 
1207 #ifdef WIN32
1208  conflines = replace_token(conflines,
1209  "#update_process_title = on",
1210  "#update_process_title = off");
1211 #endif
1212 
1213  if (strcmp(authmethodlocal, "scram-sha-256") == 0 ||
1214  strcmp(authmethodhost, "scram-sha-256") == 0)
1215  {
1216  conflines = replace_token(conflines,
1217  "#password_encryption = md5",
1218  "password_encryption = scram-sha-256");
1219  }
1220 
1221  /*
1222  * If group access has been enabled for the cluster then it makes sense to
1223  * ensure that the log files also allow group access. Otherwise a backup
1224  * from a user in the group would fail if the log files were not
1225  * relocated.
1226  */
1228  {
1229  conflines = replace_token(conflines,
1230  "#log_file_mode = 0600",
1231  "log_file_mode = 0640");
1232  }
1233 
1234  snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1235 
1236  writefile(path, conflines);
1237  if (chmod(path, pg_file_create_mode) != 0)
1238  {
1239  fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
1240  progname, path, strerror(errno));
1241  exit_nicely();
1242  }
1243 
1244  /*
1245  * create the automatic configuration file to store the configuration
1246  * parameters set by ALTER SYSTEM command. The parameters present in this
1247  * file will override the value of parameters that exists before parse of
1248  * this file.
1249  */
1250  autoconflines[0] = pg_strdup("# Do not edit this file manually!\n");
1251  autoconflines[1] = pg_strdup("# It will be overwritten by the ALTER SYSTEM command.\n");
1252  autoconflines[2] = NULL;
1253 
1254  sprintf(path, "%s/postgresql.auto.conf", pg_data);
1255 
1256  writefile(path, autoconflines);
1257  if (chmod(path, pg_file_create_mode) != 0)
1258  {
1259  fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
1260  progname, path, strerror(errno));
1261  exit_nicely();
1262  }
1263 
1264  free(conflines);
1265 
1266 
1267  /* pg_hba.conf */
1268 
1269  conflines = readfile(hba_file);
1270 
1271 #ifndef HAVE_UNIX_SOCKETS
1272  conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
1273 #else
1274  conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
1275 #endif
1276 
1277 #ifdef HAVE_IPV6
1278 
1279  /*
1280  * Probe to see if there is really any platform support for IPv6, and
1281  * comment out the relevant pg_hba line if not. This avoids runtime
1282  * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1283  * useful on Windows, where executables built on a machine with IPv6 may
1284  * have to run on a machine without.
1285  */
1286  {
1287  struct addrinfo *gai_result;
1288  struct addrinfo hints;
1289  int err = 0;
1290 
1291 #ifdef WIN32
1292  /* need to call WSAStartup before calling getaddrinfo */
1293  WSADATA wsaData;
1294 
1295  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1296 #endif
1297 
1298  /* for best results, this code should match parse_hba() */
1299  hints.ai_flags = AI_NUMERICHOST;
1300  hints.ai_family = AF_UNSPEC;
1301  hints.ai_socktype = 0;
1302  hints.ai_protocol = 0;
1303  hints.ai_addrlen = 0;
1304  hints.ai_canonname = NULL;
1305  hints.ai_addr = NULL;
1306  hints.ai_next = NULL;
1307 
1308  if (err != 0 ||
1309  getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1310  {
1311  conflines = replace_token(conflines,
1312  "host all all ::1",
1313  "#host all all ::1");
1314  conflines = replace_token(conflines,
1315  "host replication all ::1",
1316  "#host replication all ::1");
1317  }
1318  }
1319 #else /* !HAVE_IPV6 */
1320  /* If we didn't compile IPV6 support at all, always comment it out */
1321  conflines = replace_token(conflines,
1322  "host all all ::1",
1323  "#host all all ::1");
1324  conflines = replace_token(conflines,
1325  "host replication all ::1",
1326  "#host replication all ::1");
1327 #endif /* HAVE_IPV6 */
1328 
1329  /* Replace default authentication methods */
1330  conflines = replace_token(conflines,
1331  "@authmethodhost@",
1332  authmethodhost);
1333  conflines = replace_token(conflines,
1334  "@authmethodlocal@",
1335  authmethodlocal);
1336 
1337  conflines = replace_token(conflines,
1338  "@authcomment@",
1339  (strcmp(authmethodlocal, "trust") == 0 || strcmp(authmethodhost, "trust") == 0) ? AUTHTRUST_WARNING : "");
1340 
1341  snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1342 
1343  writefile(path, conflines);
1344  if (chmod(path, pg_file_create_mode) != 0)
1345  {
1346  fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
1347  progname, path, strerror(errno));
1348  exit_nicely();
1349  }
1350 
1351  free(conflines);
1352 
1353  /* pg_ident.conf */
1354 
1355  conflines = readfile(ident_file);
1356 
1357  snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1358 
1359  writefile(path, conflines);
1360  if (chmod(path, pg_file_create_mode) != 0)
1361  {
1362  fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
1363  progname, path, strerror(errno));
1364  exit_nicely();
1365  }
1366 
1367  free(conflines);
1368 
1369  check_ok();
1370 }
static char * lc_monetary
Definition: initdb.c:127
#define getaddrinfo
Definition: getaddrinfo.h:136
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:399
int pg_file_create_mode
Definition: file_perm.c:19
#define DEFAULT_CHECKPOINT_FLUSH_AFTER
static char * conf_file
Definition: initdb.c:157
#define DEFAULT_BGWRITER_FLUSH_AFTER
#define AUTHTRUST_WARNING
Definition: initdb.c:181
static void exit_nicely(void)
Definition: initdb.c:240
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static char * escape_quotes(const char *src)
#define AI_NUMERICHOST
Definition: getaddrinfo.h:73
static char * share_path
Definition: initdb.c:119
static const char * authmethodlocal
Definition: initdb.c:137
static char * lc_time
Definition: initdb.c:129
static int n_connections
Definition: initdb.c:174
static char * lc_messages
Definition: initdb.c:130
#define MAXPGPATH
static const char * default_text_search_config
Definition: initdb.c:131
static char * lc_numeric
Definition: initdb.c:128
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * pg_data
Definition: initdb.c:122
static void check_ok(void)
Definition: initdb.c:2126
#define DEFAULT_MAX_WAL_SEGS
Definition: xlog_internal.h:93
#define DATEORDER_YMD
Definition: miscadmin.h:218
const char * select_default_timezone(const char *share_path)
static const char * progname
Definition: initdb.c:150
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:2160
#define DATEORDER_DMY
Definition: miscadmin.h:219
static char * pretty_wal_size(int segment_count)
Definition: initdb.c:1060
#define DATEORDER_MDY
Definition: miscadmin.h:220
#define free(a)
Definition: header.h:65
static char ** filter_lines_with_token(char **lines, const char *token)
Definition: initdb.c:455
static int n_buffers
Definition: initdb.c:175
static void writefile(char *path, char **lines)
Definition: initdb.c:544
const char * strerror(int errnum)
Definition: strerror.c:19
static char * ident_file
Definition: initdb.c:156
static char ** readfile(const char *path)
Definition: initdb.c:482
static const char * authmethodhost
Definition: initdb.c:136
static char * dynamic_shared_memory_type
Definition: initdb.c:176
#define _(x)
Definition: elog.c:84
#define DEFAULT_MIN_WAL_SEGS
Definition: xlog_internal.h:92
static char * hba_file
Definition: initdb.c:155
#define DEFAULT_BACKEND_FLUSH_AFTER
#define DEFAULT_PGSOCKET_DIR

◆ setup_conversion()

static void setup_conversion ( FILE *  cmdfd)
static

Definition at line 1754 of file initdb.c.

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

Referenced by initialize_data_directory().

1755 {
1756  char **line;
1757  char **conv_lines;
1758 
1759  conv_lines = readfile(conversion_file);
1760  for (line = conv_lines; *line != NULL; line++)
1761  {
1762  if (strstr(*line, "DROP CONVERSION") != *line)
1763  PG_CMD_PUTS(*line);
1764  free(*line);
1765  }
1766 
1767  free(conv_lines);
1768 }
#define PG_CMD_PUTS(line)
static char * conversion_file
Definition: initdb.c:158
#define free(a)
Definition: header.h:65
static char ** readfile(const char *path)
Definition: initdb.c:482

◆ setup_data_file_paths()

void setup_data_file_paths ( void  )

Definition at line 2647 of file initdb.c.

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

Referenced by main().

2648 {
2649  set_input(&bki_file, "postgres.bki");
2650  set_input(&desc_file, "postgres.description");
2651  set_input(&shdesc_file, "postgres.shdescription");
2652  set_input(&hba_file, "pg_hba.conf.sample");
2653  set_input(&ident_file, "pg_ident.conf.sample");
2654  set_input(&conf_file, "postgresql.conf.sample");
2655  set_input(&conversion_file, "conversion_create.sql");
2656  set_input(&dictionary_file, "snowball_create.sql");
2657  set_input(&info_schema_file, "information_schema.sql");
2658  set_input(&features_file, "sql_features.txt");
2659  set_input(&system_views_file, "system_views.sql");
2660 
2661  if (show_setting || debug)
2662  {
2663  fprintf(stderr,
2664  "VERSION=%s\n"
2665  "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2666  "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2667  "POSTGRES_DESCR=%s\nPOSTGRES_SHDESCR=%s\n"
2668  "POSTGRESQL_CONF_SAMPLE=%s\n"
2669  "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2670  PG_VERSION,
2672  username, bki_file,
2674  conf_file,
2675  hba_file, ident_file);
2676  if (show_setting)
2677  exit(0);
2678  }
2679 
2691 }
static bool debug
Definition: initdb.c:138
static char * system_views_file
Definition: initdb.c:162
static char * conf_file
Definition: initdb.c:157
static char * features_file
Definition: initdb.c:161
static char * share_path
Definition: initdb.c:119
static void check_input(char *path)
Definition: initdb.c:814
static char * desc_file
Definition: initdb.c:153
static char * conversion_file
Definition: initdb.c:158
static char * pg_data
Definition: initdb.c:122
static char * dictionary_file
Definition: initdb.c:159
static char * info_schema_file
Definition: initdb.c:160
static char * username
Definition: initdb.c:132
static char bin_path[MAXPGPATH]
Definition: initdb.c:228
static char * ident_file
Definition: initdb.c:156
static char * shdesc_file
Definition: initdb.c:154
static char * bki_file
Definition: initdb.c:152
static char * hba_file
Definition: initdb.c:155
static bool show_setting
Definition: initdb.c:142
static void set_input(char **dest, const char *filename)
Definition: initdb.c:805

◆ setup_depend()

static void setup_depend ( FILE *  cmdfd)
static

Definition at line 1566 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

1567 {
1568  const char *const *line;
1569  static const char *const pg_depend_setup[] = {
1570  /*
1571  * Make PIN entries in pg_depend for all objects made so far in the
1572  * tables that the dependency code handles. This is overkill (the
1573  * system doesn't really depend on having every last weird datatype,
1574  * for instance) but generating only the minimum required set of
1575  * dependencies seems hard.
1576  *
1577  * Catalogs that are intentionally not scanned here are:
1578  *
1579  * pg_database: it's a feature, not a bug, that template1 is not
1580  * pinned.
1581  *
1582  * pg_extension: a pinned extension isn't really an extension, hmm?
1583  *
1584  * pg_tablespace: tablespaces don't participate in the dependency
1585  * code, and DropTableSpace() explicitly protects the built-in
1586  * tablespaces.
1587  *
1588  * First delete any already-made entries; PINs override all else, and
1589  * must be the only entries for their objects.
1590  */
1591  "DELETE FROM pg_depend;\n\n",
1592  "VACUUM pg_depend;\n\n",
1593  "DELETE FROM pg_shdepend;\n\n",
1594  "VACUUM pg_shdepend;\n\n",
1595 
1596  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1597  " FROM pg_class;\n\n",
1598  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1599  " FROM pg_proc;\n\n",
1600  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1601  " FROM pg_type;\n\n",
1602  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1603  " FROM pg_cast;\n\n",
1604  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1605  " FROM pg_constraint;\n\n",
1606  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1607  " FROM pg_conversion;\n\n",
1608  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1609  " FROM pg_attrdef;\n\n",
1610  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1611  " FROM pg_language;\n\n",
1612  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1613  " FROM pg_operator;\n\n",
1614  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1615  " FROM pg_opclass;\n\n",
1616  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1617  " FROM pg_opfamily;\n\n",
1618  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1619  " FROM pg_am;\n\n",
1620  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1621  " FROM pg_amop;\n\n",
1622  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1623  " FROM pg_amproc;\n\n",
1624  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1625  " FROM pg_rewrite;\n\n",
1626  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1627  " FROM pg_trigger;\n\n",
1628 
1629  /*
1630  * restriction here to avoid pinning the public namespace
1631  */
1632  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1633  " FROM pg_namespace "
1634  " WHERE nspname LIKE 'pg%';\n\n",
1635 
1636  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1637  " FROM pg_ts_parser;\n\n",
1638  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1639  " FROM pg_ts_dict;\n\n",
1640  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1641  " FROM pg_ts_template;\n\n",
1642  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1643  " FROM pg_ts_config;\n\n",
1644  "INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' "
1645  " FROM pg_collation;\n\n",
1646  "INSERT INTO pg_shdepend SELECT 0,0,0,0, tableoid,oid, 'p' "
1647  " FROM pg_authid;\n\n",
1648  NULL
1649  };
1650 
1651  for (line = pg_depend_setup; *line != NULL; line++)
1652  PG_CMD_PUTS(*line);
1653 }
#define PG_CMD_PUTS(line)

◆ setup_description()

static void setup_description ( FILE *  cmdfd)
static

Definition at line 1681 of file initdb.c.

References desc_file, escape_quotes(), PG_CMD_PRINTF1, PG_CMD_PUTS, and shdesc_file.

Referenced by initialize_data_directory().

1682 {
1683  PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_description ( "
1684  " objoid oid, "
1685  " classname name, "
1686  " objsubid int4, "
1687  " description text) WITHOUT OIDS;\n\n");
1688 
1689  PG_CMD_PRINTF1("COPY tmp_pg_description FROM E'%s';\n\n",
1691 
1692  PG_CMD_PUTS("INSERT INTO pg_description "
1693  " SELECT t.objoid, c.oid, t.objsubid, t.description "
1694  " FROM tmp_pg_description t, pg_class c "
1695  " WHERE c.relname = t.classname;\n\n");
1696 
1697  PG_CMD_PUTS("CREATE TEMP TABLE tmp_pg_shdescription ( "
1698  " objoid oid, "
1699  " classname name, "
1700  " description text) WITHOUT OIDS;\n\n");
1701 
1702  PG_CMD_PRINTF1("COPY tmp_pg_shdescription FROM E'%s';\n\n",
1704 
1705  PG_CMD_PUTS("INSERT INTO pg_shdescription "
1706  " SELECT t.objoid, c.oid, t.description "
1707  " FROM tmp_pg_shdescription t, pg_class c "
1708  " WHERE c.relname = t.classname;\n\n");
1709 
1710  /* Create default descriptions for operator implementation functions */
1711  PG_CMD_PUTS("WITH funcdescs AS ( "
1712  "SELECT p.oid as p_oid, o.oid as o_oid, oprname "
1713  "FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
1714  "INSERT INTO pg_description "
1715  " SELECT p_oid, 'pg_proc'::regclass, 0, "
1716  " 'implementation of ' || oprname || ' operator' "
1717  " FROM funcdescs "
1718  " WHERE NOT EXISTS (SELECT 1 FROM pg_description "
1719  " WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass) "
1720  " AND NOT EXISTS (SELECT 1 FROM pg_description "
1721  " WHERE objoid = o_oid AND classoid = 'pg_operator'::regclass"
1722  " AND description LIKE 'deprecated%');\n\n");
1723 
1724  /*
1725  * Even though the tables are temp, drop them explicitly so they don't get
1726  * copied into template0/postgres databases.
1727  */
1728  PG_CMD_PUTS("DROP TABLE tmp_pg_description;\n\n");
1729  PG_CMD_PUTS("DROP TABLE tmp_pg_shdescription;\n\n");
1730 }
static char * escape_quotes(const char *src)
#define PG_CMD_PUTS(line)
static char * desc_file
Definition: initdb.c:153
#define PG_CMD_PRINTF1(fmt, arg1)
static char * shdesc_file
Definition: initdb.c:154

◆ setup_dictionary()

static void setup_dictionary ( FILE *  cmdfd)
static

Definition at line 1774 of file initdb.c.

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

Referenced by initialize_data_directory().

1775 {
1776  char **line;
1777  char **conv_lines;
1778 
1779  conv_lines = readfile(dictionary_file);
1780  for (line = conv_lines; *line != NULL; line++)
1781  {
1782  PG_CMD_PUTS(*line);
1783  free(*line);
1784  }
1785 
1786  PG_CMD_PUTS("\n\n");
1787 
1788  free(conv_lines);
1789 }
#define PG_CMD_PUTS(line)
static char * dictionary_file
Definition: initdb.c:159
#define free(a)
Definition: header.h:65
static char ** readfile(const char *path)
Definition: initdb.c:482

◆ setup_locale_encoding()

void setup_locale_encoding ( void  )

Definition at line 2562 of file initdb.c.

References _, check_locale_encoding(), encoding, encodingid, 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_UTF8, pg_valid_server_encoding_id(), progname, and setlocales().

Referenced by main().

2563 {
2564  setlocales();
2565 
2566  if (strcmp(lc_ctype, lc_collate) == 0 &&
2567  strcmp(lc_ctype, lc_time) == 0 &&
2568  strcmp(lc_ctype, lc_numeric) == 0 &&
2569  strcmp(lc_ctype, lc_monetary) == 0 &&
2570  strcmp(lc_ctype, lc_messages) == 0)
2571  printf(_("The database cluster will be initialized with locale \"%s\".\n"), lc_ctype);
2572  else
2573  {
2574  printf(_("The database cluster will be initialized with locales\n"
2575  " COLLATE: %s\n"
2576  " CTYPE: %s\n"
2577  " MESSAGES: %s\n"
2578  " MONETARY: %s\n"
2579  " NUMERIC: %s\n"
2580  " TIME: %s\n"),
2581  lc_collate,
2582  lc_ctype,
2583  lc_messages,
2584  lc_monetary,
2585  lc_numeric,
2586  lc_time);
2587  }
2588 
2589  if (!encoding)
2590  {
2591  int ctype_enc;
2592 
2593  ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);
2594 
2595  if (ctype_enc == -1)
2596  {
2597  /* Couldn't recognize the locale's codeset */
2598  fprintf(stderr, _("%s: could not find suitable encoding for locale \"%s\"\n"),
2599  progname, lc_ctype);
2600  fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
2601  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
2602  progname);
2603  exit(1);
2604  }
2605  else if (!pg_valid_server_encoding_id(ctype_enc))
2606  {
2607  /*
2608  * We recognized it, but it's not a legal server encoding. On
2609  * Windows, UTF-8 works with any locale, so we can fall back to
2610  * UTF-8.
2611  */
2612 #ifdef WIN32
2613  encodingid = PG_UTF8;
2614  printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
2615  "The default database encoding will be set to \"%s\" instead.\n"),
2616  pg_encoding_to_char(ctype_enc),
2618 #else
2619  fprintf(stderr,
2620  _("%s: locale \"%s\" requires unsupported encoding \"%s\"\n"),
2621  progname, lc_ctype, pg_encoding_to_char(ctype_enc));
2622  fprintf(stderr,
2623  _("Encoding \"%s\" is not allowed as a server-side encoding.\n"
2624  "Rerun %s with a different locale selection.\n"),
2625  pg_encoding_to_char(ctype_enc), progname);
2626  exit(1);
2627 #endif
2628  }
2629  else
2630  {
2631  encodingid = ctype_enc;
2632  printf(_("The default database encoding has accordingly been set to \"%s\".\n"),
2634  }
2635  }
2636  else
2638 
2641  exit(1); /* check_locale_encoding printed the error */
2642 
2643 }
static int encodingid
Definition: initdb.c:151
static char * lc_monetary
Definition: initdb.c:127
static char * lc_collate
Definition: initdb.c:125
int pg_valid_server_encoding_id(int encoding)
Definition: encnames.c:515
static int get_encoding_id(const char *encoding_name)
Definition: initdb.c:694
static char * lc_time
Definition: initdb.c:129
static char * lc_messages
Definition: initdb.c:130
static char * lc_ctype
Definition: initdb.c:126
static char * lc_numeric
Definition: initdb.c:128
static const char * progname
Definition: initdb.c:150
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:433
static bool check_locale_encoding(const char *locale, int user_enc)
Definition: initdb.c:2291
static char * encoding
Definition: initdb.c:123
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
static void setlocales(void)
Definition: initdb.c:2327
#define _(x)
Definition: elog.c:84

◆ setup_pgdata()

void setup_pgdata ( void  )

Definition at line 2474 of file initdb.c.

References _, canonicalize_path(), pg_data, pg_strdup(), pgdata_native, progname, psprintf(), and putenv.

Referenced by main().

2475 {
2476  char *pgdata_get_env,
2477  *pgdata_set_env;
2478 
2479  if (!pg_data)
2480  {
2481  pgdata_get_env = getenv("PGDATA");
2482  if (pgdata_get_env && strlen(pgdata_get_env))
2483  {
2484  /* PGDATA found */
2485  pg_data = pg_strdup(pgdata_get_env);
2486  }
2487  else
2488  {
2489  fprintf(stderr,
2490  _("%s: no data directory specified\n"
2491  "You must identify the directory where the data for this database system\n"
2492  "will reside. Do this with either the invocation option -D or the\n"
2493  "environment variable PGDATA.\n"),
2494  progname);
2495  exit(1);
2496  }
2497  }
2498 
2501 
2502  /*
2503  * we have to set PGDATA for postgres rather than pass it on the command
2504  * line to avoid dumb quoting problems on Windows, and we would especially
2505  * need quotes otherwise on Windows because paths there are most likely to
2506  * have embedded spaces.
2507  */
2508  pgdata_set_env = psprintf("PGDATA=%s", pg_data);
2509  putenv(pgdata_set_env);
2510 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void canonicalize_path(char *path)
Definition: path.c:254
static char * pgdata_native
Definition: initdb.c:171
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * pg_data
Definition: initdb.c:122
static const char * progname
Definition: initdb.c:150
#define putenv(x)
Definition: win32_port.h:487
#define _(x)
Definition: elog.c:84

◆ setup_privileges()

static void setup_privileges ( FILE *  cmdfd)
static

Definition at line 1811 of file initdb.c.

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

Referenced by initialize_data_directory().

1812 {
1813  char **line;
1814  char **priv_lines;
1815  static char *privileges_setup[] = {
1816  "UPDATE pg_class "
1817  " SET relacl = (SELECT array_agg(a.acl) FROM "
1818  " (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl "
1819  " UNION SELECT unnest(pg_catalog.acldefault("
1820  " CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) " THEN 's' "
1821  " ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
1822  " ) as a) "
1823  " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1824  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1825  CppAsString2(RELKIND_SEQUENCE) ")"
1826  " AND relacl IS NULL;\n\n",
1827  "GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n\n",
1828  "GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n\n",
1829  "REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n",
1830  "INSERT INTO pg_init_privs "
1831  " (objoid, classoid, objsubid, initprivs, privtype)"
1832  " SELECT"
1833  " oid,"
1834  " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1835  " 0,"
1836  " relacl,"
1837  " 'i'"
1838  " FROM"
1839  " pg_class"
1840  " WHERE"
1841  " relacl IS NOT NULL"
1842  " AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1843  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1844  CppAsString2(RELKIND_SEQUENCE) ");\n\n",
1845  "INSERT INTO pg_init_privs "
1846  " (objoid, classoid, objsubid, initprivs, privtype)"
1847  " SELECT"
1848  " pg_class.oid,"
1849  " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1850  " pg_attribute.attnum,"
1851  " pg_attribute.attacl,"
1852  " 'i'"
1853  " FROM"
1854  " pg_class"
1855  " JOIN pg_attribute ON (pg_class.oid = pg_attribute.attrelid)"
1856  " WHERE"
1857  " pg_attribute.attacl IS NOT NULL"
1858  " AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1859  CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1860  CppAsString2(RELKIND_SEQUENCE) ");\n\n",
1861  "INSERT INTO pg_init_privs "
1862  " (objoid, classoid, objsubid, initprivs, privtype)"
1863  " SELECT"
1864  " oid,"
1865  " (SELECT oid FROM pg_class WHERE relname = 'pg_proc'),"
1866  " 0,"
1867  " proacl,"
1868  " 'i'"
1869  " FROM"
1870  " pg_proc"
1871  " WHERE"
1872  " proacl IS NOT NULL;\n\n",
1873  "INSERT INTO pg_init_privs "
1874  " (objoid, classoid, objsubid, initprivs, privtype)"
1875  " SELECT"
1876  " oid,"
1877  " (SELECT oid FROM pg_class WHERE relname = 'pg_type'),"
1878  " 0,"
1879  " typacl,"
1880  " 'i'"
1881  " FROM"
1882  " pg_type"
1883  " WHERE"
1884  " typacl IS NOT NULL;\n\n",
1885  "INSERT INTO pg_init_privs "
1886  " (objoid, classoid, objsubid, initprivs, privtype)"
1887  " SELECT"
1888  " oid,"
1889  " (SELECT oid FROM pg_class WHERE relname = 'pg_language'),"
1890  " 0,"
1891  " lanacl,"
1892  " 'i'"
1893  " FROM"
1894  " pg_language"
1895  " WHERE"
1896  " lanacl IS NOT NULL;\n\n",
1897  "INSERT INTO pg_init_privs "
1898  " (objoid, classoid, objsubid, initprivs, privtype)"
1899  " SELECT"
1900  " oid,"
1901  " (SELECT oid FROM pg_class WHERE "
1902  " relname = 'pg_largeobject_metadata'),"
1903  " 0,"
1904  " lomacl,"
1905  " 'i'"
1906  " FROM"
1907  " pg_largeobject_metadata"
1908  " WHERE"
1909  " lomacl IS NOT NULL;\n\n",
1910  "INSERT INTO pg_init_privs "
1911  " (objoid, classoid, objsubid, initprivs, privtype)"
1912  " SELECT"
1913  " oid,"
1914  " (SELECT oid FROM pg_class WHERE relname = 'pg_namespace'),"
1915  " 0,"
1916  " nspacl,"
1917  " 'i'"
1918  " FROM"
1919  " pg_namespace"
1920  " WHERE"
1921  " nspacl IS NOT NULL;\n\n",
1922  "INSERT INTO pg_init_privs "
1923  " (objoid, classoid, objsubid, initprivs, privtype)"
1924  " SELECT"
1925  " oid,"
1926  " (SELECT oid FROM pg_class WHERE "
1927  " relname = 'pg_foreign_data_wrapper'),"
1928  " 0,"
1929  " fdwacl,"
1930  " 'i'"
1931  " FROM"
1932  " pg_foreign_data_wrapper"
1933  " WHERE"
1934  " fdwacl IS NOT NULL;\n\n",
1935  "INSERT INTO pg_init_privs "
1936  " (objoid, classoid, objsubid, initprivs, privtype)"
1937  " SELECT"
1938  " oid,"
1939  " (SELECT oid FROM pg_class "
1940  " WHERE relname = 'pg_foreign_server'),"
1941  " 0,"
1942  " srvacl,"
1943  " 'i'"
1944  " FROM"
1945  " pg_foreign_server"
1946  " WHERE"
1947  " srvacl IS NOT NULL;\n\n",
1948  NULL
1949  };
1950 
1951  priv_lines = replace_token(privileges_setup, "$POSTGRES_SUPERUSERNAME",
1953  for (line = priv_lines; *line != NULL; line++)
1954  PG_CMD_PUTS(*line);
1955 }
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:399
static char * escape_quotes(const char *src)
#define PG_CMD_PUTS(line)
#define CppAsString2(x)
Definition: c.h:224
static char * username
Definition: initdb.c:132

◆ setup_schema()

static void setup_schema ( FILE *  cmdfd)
static

Definition at line 1989 of file initdb.c.

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

Referenced by initialize_data_directory().

1990 {
1991  char **line;
1992  char **lines;
1993 
1994  lines = readfile(info_schema_file);
1995 
1996  for (line = lines; *line != NULL; line++)
1997  {
1998  PG_CMD_PUTS(*line);
1999  free(*line);
2000  }
2001 
2002  PG_CMD_PUTS("\n\n");
2003 
2004  free(lines);
2005 
2006  PG_CMD_PRINTF1("UPDATE information_schema.sql_implementation_info "
2007  " SET character_value = '%s' "
2008  " WHERE implementation_info_name = 'DBMS VERSION';\n\n",
2009  infoversion);
2010 
2011  PG_CMD_PRINTF1("COPY information_schema.sql_features "
2012  " (feature_id, feature_name, sub_feature_id, "
2013  " sub_feature_name, is_supported, comments) "
2014  " FROM E'%s';\n\n",
2016 }
static char infoversion[100]
Definition: initdb.c:167
static char * features_file
Definition: initdb.c:161
static char * escape_quotes(const char *src)
#define PG_CMD_PUTS(line)
#define PG_CMD_PRINTF1(fmt, arg1)
static char * info_schema_file
Definition: initdb.c:160
#define free(a)
Definition: header.h:65
static char ** readfile(const char *path)
Definition: initdb.c:482

◆ setup_signals()

void setup_signals ( void  )

Definition at line 2730 of file initdb.c.

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

Referenced by initialize_data_directory().

2731 {
2732  /* some of these are not valid on Windows */
2733 #ifdef SIGHUP
2735 #endif
2736 #ifdef SIGINT
2737  pqsignal(SIGINT, trapsig);
2738 #endif
2739 #ifdef SIGQUIT
2741 #endif
2742 #ifdef SIGTERM
2743  pqsignal(SIGTERM, trapsig);
2744 #endif
2745 
2746  /* Ignore SIGPIPE when writing to backend, so we can clean up */
2747 #ifdef SIGPIPE
2749 #endif
2750 
2751  /* Prevent SIGSYS so we can probe for kernel calls that might not work */
2752 #ifdef SIGSYS
2753  pqsignal(SIGSYS, SIG_IGN);
2754 #endif
2755 }
#define SIGQUIT
Definition: win32_port.h:164
#define SIGPIPE
Definition: win32_port.h:168
#define SIGHUP
Definition: win32_port.h:163
#define SIG_IGN
Definition: win32_port.h:160
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void trapsig(int signum)
Definition: initdb.c:2115

◆ setup_sysviews()

static void setup_sysviews ( FILE *  cmdfd)
static

Definition at line 1659 of file initdb.c.

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

Referenced by initialize_data_directory().

1660 {
1661  char **line;
1662  char **sysviews_setup;
1663 
1664  sysviews_setup = readfile(system_views_file);
1665 
1666  for (line = sysviews_setup; *line != NULL; line++)
1667  {
1668  PG_CMD_PUTS(*line);
1669  free(*line);
1670  }
1671 
1672  PG_CMD_PUTS("\n\n");
1673 
1674  free(sysviews_setup);
1675 }
static char * system_views_file
Definition: initdb.c:162
#define PG_CMD_PUTS(line)
#define free(a)
Definition: header.h:65
static char ** readfile(const char *path)
Definition: initdb.c:482

◆ setup_text_search()

void setup_text_search ( void  )

Definition at line 2695 of file initdb.c.

References _, default_text_search_config, find_matching_ts_config(), lc_ctype, and progname.

Referenced by main().

2696 {
2698  {
2701  {
2702  printf(_("%s: could not find suitable text search configuration for locale \"%s\"\n"),
2703  progname, lc_ctype);
2704  default_text_search_config = "simple";
2705  }
2706  }
2707  else
2708  {
2709  const char *checkmatch = find_matching_ts_config(lc_ctype);
2710 
2711  if (checkmatch == NULL)
2712  {
2713  printf(_("%s: warning: suitable text search configuration for locale \"%s\" is unknown\n"),
2714  progname, lc_ctype);
2715  }
2716  else if (strcmp(checkmatch, default_text_search_config) != 0)
2717  {
2718  printf(_("%s: warning: specified text search configuration \"%s\" might not match locale \"%s\"\n"),
2720  }
2721  }
2722 
2723  printf(_("The default text search configuration will be set to \"%s\".\n"),
2725 
2726 }
static const char * find_matching_ts_config(const char *lc_type)
Definition: initdb.c:759
static const char * default_text_search_config
Definition: initdb.c:131
static char * lc_ctype
Definition: initdb.c:126
static const char * progname
Definition: initdb.c:150
#define _(x)
Definition: elog.c:84

◆ test_config_settings()

static void test_config_settings ( void  )
static

Definition at line 960 of file initdb.c.

References _, backend_exec, boot_options, choose_dsm_implementation(), DEVNULL, dynamic_shared_memory_type, i, MAXPGPATH, MIN_BUFS_FOR_CONNS, n_buffers, n_connections, snprintf(), and status().

Referenced by initialize_data_directory().

961 {
962  /*
963  * This macro defines the minimum shared_buffers we want for a given
964  * max_connections value. The arrays show the settings to try.
965  */
966 #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
967 
968  static const int trial_conns[] = {
969  100, 50, 40, 30, 20
970  };
971  static const int trial_bufs[] = {
972  16384, 8192, 4096, 3584, 3072, 2560, 2048, 1536,
973  1000, 900, 800, 700, 600, 500,
974  400, 300, 200, 100, 50
975  };
976 
977  char cmd[MAXPGPATH];
978  const int connslen = sizeof(trial_conns) / sizeof(int);
979  const int bufslen = sizeof(trial_bufs) / sizeof(int);
980  int i,
981  status,
982  test_conns,
983  test_buffs,
984  ok_buffers = 0;
985 
986 
987  printf(_("selecting default max_connections ... "));
988  fflush(stdout);
989 
990  for (i = 0; i < connslen; i++)
991  {
992  test_conns = trial_conns[i];
993  test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
994 
995  snprintf(cmd, sizeof(cmd),
996  "\"%s\" --boot -x0 %s "
997  "-c max_connections=%d "
998  "-c shared_buffers=%d "
999  "-c dynamic_shared_memory_type=none "
1000  "< \"%s\" > \"%s\" 2>&1",
1002  test_conns, test_buffs,
1003  DEVNULL, DEVNULL);
1004  status = system(cmd);
1005  if (status == 0)
1006  {
1007  ok_buffers = test_buffs;
1008  break;
1009  }
1010  }
1011  if (i >= connslen)
1012  i = connslen - 1;
1013  n_connections = trial_conns[i];
1014 
1015  printf("%d\n", n_connections);
1016 
1017  printf(_("selecting default shared_buffers ... "));
1018  fflush(stdout);
1019 
1020  for (i = 0; i < bufslen; i++)
1021  {
1022  /* Use same amount of memory, independent of BLCKSZ */
1023  test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
1024  if (test_buffs <= ok_buffers)
1025  {
1026  test_buffs = ok_buffers;
1027  break;
1028  }
1029 
1030  snprintf(cmd, sizeof(cmd),
1031  "\"%s\" --boot -x0 %s "
1032  "-c max_connections=%d "
1033  "-c shared_buffers=%d "
1034  "-c dynamic_shared_memory_type=none "
1035  "< \"%s\" > \"%s\" 2>&1",
1037  n_connections, test_buffs,
1038  DEVNULL, DEVNULL);
1039  status = system(cmd);
1040  if (status == 0)
1041  break;
1042  }
1043  n_buffers = test_buffs;
1044 
1045  if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1046  printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
1047  else
1048  printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
1049 
1050  printf(_("selecting dynamic shared memory implementation ... "));
1051  fflush(stdout);
1053  printf("%s\n", dynamic_shared_memory_type);
1054 }
#define MIN_BUFS_FOR_CONNS(nconns)
static char * choose_dsm_implementation(void)
Definition: initdb.c:920
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int n_connections
Definition: initdb.c:174
#define MAXPGPATH
#define DEVNULL
Definition: port.h:123
static const char * boot_options
Definition: initdb.c:198
static int n_buffers
Definition: initdb.c:175
int i
static char backend_exec[MAXPGPATH]
Definition: initdb.c:229
static char * dynamic_shared_memory_type
Definition: initdb.c:176
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:84

◆ trapsig()

static void trapsig ( int  signum)
static

Definition at line 2115 of file initdb.c.

References caught_signal, and pqsignal().

Referenced by setup_signals().

2116 {
2117  /* handle systems that reset the handler, like Windows (grr) */
2118  pqsignal(signum, trapsig);
2119  caught_signal = true;
2120 }
static bool caught_signal
Definition: initdb.c:168
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void trapsig(int signum)
Definition: initdb.c:2115

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 2378 of file initdb.c.

References _.

Referenced by main().

2379 {
2380  printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2381  printf(_("Usage:\n"));
2382  printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2383  printf(_("\nOptions:\n"));
2384  printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2385  printf(_(" --auth-host=METHOD default authentication method for local TCP/IP connections\n"));
2386  printf(_(" --auth-local=METHOD default authentication method for local-socket connections\n"));
2387  printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2388  printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2389  printf(_(" -g, --allow-group-access allow group read/execute on data directory\n"));
2390  printf(_(" --locale=LOCALE set default locale for new databases\n"));
2391  printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2392  " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2393  " set default locale in the respective category for\n"
2394  " new databases (default taken from environment)\n"));
2395  printf(_(" --no-locale equivalent to --locale=C\n"));
2396  printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2397  printf(_(" -T, --text-search-config=CFG\n"
2398  " default text search configuration\n"));
2399  printf(_(" -U, --username=NAME database superuser name\n"));
2400  printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2401  printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
2402  printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
2403  printf(_("\nLess commonly used options:\n"));
2404  printf(_(" -d, --debug generate lots of debugging output\n"));
2405  printf(_(" -k, --data-checksums use data page checksums\n"));
2406  printf(_(" -L DIRECTORY where to find the input files\n"));
2407  printf(_(" -n, --no-clean do not clean up after errors\n"));
2408  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
2409  printf(_(" -s, --show show internal settings\n"));
2410  printf(_(" -S, --sync-only only sync data directory\n"));
2411  printf(_("\nOther options:\n"));
2412  printf(_(" -V, --version output version information, then exit\n"));
2413  printf(_(" -?, --help show this help, then exit\n"));
2414  printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2415  "is used.\n"));
2416  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
2417 }
static const char * progname
Definition: initdb.c:150
#define _(x)
Definition: elog.c:84

◆ vacuum_db()

static void vacuum_db ( FILE *  cmdfd)
static

Definition at line 2031 of file initdb.c.

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

2032 {
2033  /* Run analyze before VACUUM so the statistics are frozen. */
2034  PG_CMD_PUTS("ANALYZE;\n\nVACUUM FREEZE;\n\n");
2035 }
#define PG_CMD_PUTS(line)

◆ warn_on_mount_point()

void warn_on_mount_point ( int  error)

Definition at line 2938 of file initdb.c.

References _.

Referenced by create_data_directory(), and create_xlog_or_symlink().

2939 {
2940  if (error == 2)
2941  fprintf(stderr,
2942  _("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.\n"));
2943  else if (error == 3)
2944  fprintf(stderr,
2945  _("It contains a lost+found directory, perhaps due to it being a mount point.\n"));
2946 
2947  fprintf(stderr,
2948  _("Using a mount point directly as the data directory is not recommended.\n"
2949  "Create a subdirectory under the mount point.\n"));
2950 }
static void error(void)
Definition: sql-dyntest.c:147
#define _(x)
Definition: elog.c:84

◆ write_version_file()

static void write_version_file ( const char *  extrapath)
static

Definition at line 855 of file initdb.c.

References _, exit_nicely(), free, PG_BINARY_W, pg_data, progname, psprintf(), strerror(), and version_file.

Referenced by initialize_data_directory().

856 {
857  FILE *version_file;
858  char *path;
859 
860  if (extrapath == NULL)
861  path = psprintf("%s/PG_VERSION", pg_data);
862  else
863  path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
864 
865  if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
866  {
867  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
868  progname, path, strerror(errno));
869  exit_nicely();
870  }
871  if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
872  fclose(version_file))
873  {
874  fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
875  progname, path, strerror(errno));
876  exit_nicely();
877  }
878  free(path);
879 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PG_BINARY_W
Definition: c.h:1083
static void exit_nicely(void)
Definition: initdb.c:240
static char * pg_data
Definition: initdb.c:122
static const char * progname
Definition: initdb.c:150
#define free(a)
Definition: header.h:65
const char * strerror(int errnum)
Definition: strerror.c:19
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:99
#define _(x)
Definition: elog.c:84

◆ writefile()

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

Definition at line 544 of file initdb.c.

References _, exit_nicely(), free, progname, and strerror().

Referenced by setup_config().

545 {
546  FILE *out_file;
547  char **line;
548 
549  if ((out_file = fopen(path, "w")) == NULL)
550  {
551  fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
552  progname, path, strerror(errno));
553  exit_nicely();
554  }
555  for (line = lines; *line != NULL; line++)
556  {
557  if (fputs(*line, out_file) < 0)
558  {
559  fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
560  progname, path, strerror(errno));
561  exit_nicely();
562  }
563  free(*line);
564  }
565  if (fclose(out_file))
566  {
567  fprintf(stderr, _("%s: could not write file \"%s\": %s\n"),
568  progname, path, strerror(errno));
569  exit_nicely();
570  }
571 }
static void exit_nicely(void)
Definition: initdb.c:240
static const char * progname
Definition: initdb.c:150
#define free(a)
Definition: header.h:65
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84

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 80 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 102 of file initdb.c.

Referenced by main().

◆ authmethodhost

const char* authmethodhost = NULL
static

Definition at line 136 of file initdb.c.

Referenced by main(), and setup_config().

◆ authmethodlocal

const char* authmethodlocal = NULL
static

Definition at line 137 of file initdb.c.

Referenced by main(), and setup_config().

◆ authwarning

char* authwarning = NULL
static

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

Referenced by initialize_data_directory().

◆ bin_path

char bin_path[MAXPGPATH]
static

Definition at line 228 of file initdb.c.

Referenced by setup_bin_paths(), and setup_data_file_paths().

◆ bki_file

char* bki_file
static

Definition at line 152 of file initdb.c.

Referenced by bootstrap_template1(), and setup_data_file_paths().

◆ boot_options

const char* boot_options = "-F"
static

Definition at line 198 of file initdb.c.

Referenced by bootstrap_template1(), and test_config_settings().

◆ caught_signal

bool caught_signal = false
static

Definition at line 168 of file initdb.c.

Referenced by check_ok(), and trapsig().

◆ conf_file

char* conf_file
static

Definition at line 157 of file initdb.c.

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

◆ conversion_file

char* conversion_file
static

Definition at line 158 of file initdb.c.

Referenced by setup_conversion(), and setup_data_file_paths().

◆ data_checksums

bool data_checksums = false
static

Definition at line 143 of file initdb.c.

Referenced by bootstrap_template1(), and main().

◆ debug

bool debug = false
static

Definition at line 138 of file initdb.c.

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

◆ default_text_search_config

const char* default_text_search_config = NULL
static

Definition at line 131 of file initdb.c.

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

◆ desc_file

char* desc_file
static

Definition at line 153 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_description().

◆ dictionary_file

char* dictionary_file
static

Definition at line 159 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_dictionary().

◆ do_sync

bool do_sync = true
static

Definition at line 140 of file initdb.c.

Referenced by main().

◆ dynamic_shared_memory_type

char* dynamic_shared_memory_type = NULL
static

Definition at line 176 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ encoding

char* encoding = NULL
static

Definition at line 123 of file initdb.c.

Referenced by main(), and setup_locale_encoding().

◆ encodingid

int encodingid
static

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

Referenced by create_data_directory(), and popen_check().

◆ found_existing_xlogdir

bool found_existing_xlogdir = false
static

Definition at line 166 of file initdb.c.

Referenced by create_xlog_or_symlink(), and popen_check().

◆ hba_file

char* hba_file
static

Definition at line 155 of file initdb.c.

Referenced by setup_config(), and setup_data_file_paths().

◆ ident_file

char* ident_file
static

Definition at line 156 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 167 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 130 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 127 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 128 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 129 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 163 of file initdb.c.

Referenced by create_data_directory(), and popen_check().

◆ made_new_xlogdir

bool made_new_xlogdir = false
static

Definition at line 165 of file initdb.c.

Referenced by create_xlog_or_symlink(), and popen_check().

◆ n_buffers

int n_buffers = 50
static

Definition at line 175 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ n_connections

int n_connections = 10
static

Definition at line 174 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ noclean

bool noclean = false
static

Definition at line 139 of file initdb.c.

Referenced by main(), and popen_check().

◆ output_errno

int output_errno = 0
static

Definition at line 170 of file initdb.c.

Referenced by check_ok().

◆ output_failed

bool output_failed = false
static

Definition at line 169 of file initdb.c.

Referenced by check_ok().

◆ pg_data

◆ pgdata_native

char* pgdata_native
static

Definition at line 171 of file initdb.c.

Referenced by main(), and setup_pgdata().

◆ progname

◆ pwfilename

char* pwfilename = NULL
static

Definition at line 134 of file initdb.c.

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

◆ pwprompt

bool pwprompt = false
static

Definition at line 133 of file initdb.c.

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

◆ share_path

char* share_path = NULL
static

◆ shdesc_file

char* shdesc_file
static

Definition at line 154 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_description().

◆ show_setting

bool show_setting = false
static

Definition at line 142 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 145 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 201 of file initdb.c.

Referenced by initialize_data_directory().

◆ superuser_password

char* superuser_password = NULL
static

Definition at line 135 of file initdb.c.

Referenced by get_su_pwd(), and setup_auth().

◆ sync_only

bool sync_only = false
static

Definition at line 141 of file initdb.c.

Referenced by main().

◆ system_views_file

char* system_views_file
static

Definition at line 162 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_sysviews().

◆ tsearch_config_languages

const struct tsearch_config_match tsearch_config_languages[]
static

Definition at line 718 of file initdb.c.

◆ username

◆ wal_segment_size_mb

int wal_segment_size_mb
static

Definition at line 146 of file initdb.c.

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

◆ xlog_dir

char* xlog_dir = NULL
static

Definition at line 144 of file initdb.c.

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