PostgreSQL Source Code git master
initdb.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "sys/mman.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 "catalog/pg_database_d.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/logging.h"
#include "common/pg_prng.h"
#include "common/restricted_token.h"
#include "common/string.h"
#include "common/username.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.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  _stringlist
 
struct  tsearch_config_match
 

Macros

#define AUTHTRUST_WARNING
 
#define PG_CMD_DECL   FILE *cmdfd
 
#define PG_CMD_OPEN(cmd)
 
#define PG_CMD_CLOSE()
 
#define PG_CMD_PUTS(line)
 
#define PG_CMD_PRINTF(fmt, ...)
 
#define LETTERS   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
#define DIGITS   "0123456789"
 
#define MIN_BUFS_FOR_CONNS(nconns)   ((nconns) * 10)
 
#define AV_SLOTS_FOR_CONNS(nconns)   ((nconns) / 6)
 

Typedefs

typedef struct _stringlist _stringlist
 
typedef char * save_locale_t
 

Functions

const char * select_default_timezone (const char *share_path)
 
static char ** replace_token (char **lines, const char *token, const char *replacement)
 
static char ** replace_guc_value (char **lines, const char *guc_name, const char *guc_value, bool mark_as_comment)
 
static bool guc_value_requires_quotes (const char *guc_value)
 
static char ** readfile (const char *path)
 
static void writefile (char *path, char **lines)
 
static FILE * popen_check (const char *command, const char *mode)
 
static char * get_id (void)
 
static int get_encoding_id (const char *encoding_name)
 
static void set_input (char **dest, const char *filename)
 
static void check_input (char *path)
 
static void write_version_file (const char *extrapath)
 
static void set_null_conf (void)
 
static void test_config_settings (void)
 
static bool test_specific_config_settings (int test_conns, int test_av_slots, int test_buffs)
 
static void setup_config (void)
 
static void bootstrap_template1 (void)
 
static void setup_auth (FILE *cmdfd)
 
static void get_su_pwd (void)
 
static void setup_depend (FILE *cmdfd)
 
static void setup_run_file (FILE *cmdfd, const char *filename)
 
static void setup_description (FILE *cmdfd)
 
static void setup_collation (FILE *cmdfd)
 
static void setup_privileges (FILE *cmdfd)
 
static void set_info_version (void)
 
static void setup_schema (FILE *cmdfd)
 
static void load_plpgsql (FILE *cmdfd)
 
static void vacuum_db (FILE *cmdfd)
 
static void make_template0 (FILE *cmdfd)
 
static void make_postgres (FILE *cmdfd)
 
static void trapsig (SIGNAL_ARGS)
 
static void check_ok (void)
 
static char * escape_quotes (const char *src)
 
static char * escape_quotes_bki (const char *src)
 
static int locale_date_order (const char *locale)
 
static void check_locale_name (int category, const char *locale, char **canonname)
 
static bool check_locale_encoding (const char *locale, int user_enc)
 
static void setlocales (void)
 
static void usage (const char *progname)
 
void setup_pgdata (void)
 
void setup_bin_paths (const char *argv0)
 
void setup_data_file_paths (void)
 
void setup_locale_encoding (void)
 
void setup_signals (void)
 
void setup_text_search (void)
 
void create_data_directory (void)
 
void create_xlog_or_symlink (void)
 
void warn_on_mount_point (int error)
 
void initialize_data_directory (void)
 
static save_locale_t save_global_locale (int category)
 
static void restore_global_locale (int category, save_locale_t save)
 
static void add_stringlist_item (_stringlist **listhead, const char *str)
 
static void cleanup_directories_atexit (void)
 
static char * encodingid_to_string (int enc)
 
static const char * find_matching_ts_config (const char *lc_type)
 
static const char * choose_dsm_implementation (void)
 
static char * pretty_wal_size (int segment_count)
 
static size_t my_strftime (char *s, size_t max, const char *fmt, const struct tm *tm)
 
static bool check_icu_locale_encoding (int user_enc)
 
static char * icu_language_tag (const char *loc_str)
 
static void icu_validate_locale (const char *loc_str)
 
static void check_authmethod_unspecified (const char **authmethod)
 
static void check_authmethod_valid (const char *authmethod, const char *const *valid_methods, const char *conntype)
 
static void check_need_password (const char *authmethodlocal, const char *authmethodhost)
 
int main (int argc, char *argv[])
 

Variables

static const char *const auth_methods_host []
 
static const char *const auth_methods_local []
 
static char * share_path = NULL
 
static char * pg_data = NULL
 
static char * encoding = NULL
 
static char * locale = NULL
 
static char * lc_collate = NULL
 
static char * lc_ctype = NULL
 
static char * lc_monetary = NULL
 
static char * lc_numeric = NULL
 
static char * lc_time = NULL
 
static char * lc_messages = NULL
 
static char locale_provider = COLLPROVIDER_LIBC
 
static bool builtin_locale_specified = false
 
static char * datlocale = NULL
 
static bool icu_locale_specified = false
 
static char * icu_rules = 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 _stringlistextra_guc_names = NULL
 
static _stringlistextra_guc_values = NULL
 
static bool debug = false
 
static bool noclean = false
 
static bool noinstructions = false
 
static bool do_sync = true
 
static bool sync_only = false
 
static bool show_setting = false
 
static bool data_checksums = true
 
static char * xlog_dir = NULL
 
static int wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024)
 
static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC
 
static const char * progname
 
static int encodingid
 
static char * bki_file
 
static char * hba_file
 
static char * ident_file
 
static char * conf_file
 
static char * dictionary_file
 
static char * info_schema_file
 
static char * features_file
 
static char * system_constraints_file
 
static char * system_functions_file
 
static char * system_views_file
 
static bool success = false
 
static bool made_new_pgdata = false
 
static bool found_existing_pgdata = false
 
static bool made_new_xlogdir = false
 
static bool found_existing_xlogdir = false
 
static char infoversion [100]
 
static bool caught_signal = false
 
static bool output_failed = false
 
static int output_errno = 0
 
static char * pgdata_native
 
static int n_connections = 10
 
static int n_av_slots = 16
 
static int n_buffers = 50
 
static const char * dynamic_shared_memory_type = NULL
 
static const char * default_timezone = NULL
 
static bool authwarning = false
 
static const char *const boot_options = "-F -c log_checkpoints=false"
 
static const char *const backend_options = "--single -F -O -j -c search_path=pg_catalog -c exit_on_error=true -c log_checkpoints=false"
 
static char * extra_options = ""
 
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 207 of file initdb.c.

◆ AV_SLOTS_FOR_CONNS

#define AV_SLOTS_FOR_CONNS (   nconns)    ((nconns) / 6)

◆ DIGITS

#define DIGITS   "0123456789"

◆ LETTERS

#define LETTERS   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

◆ MIN_BUFS_FOR_CONNS

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

◆ PG_CMD_CLOSE

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

Definition at line 327 of file initdb.c.

◆ PG_CMD_DECL

#define PG_CMD_DECL   FILE *cmdfd

Definition at line 318 of file initdb.c.

◆ PG_CMD_OPEN

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

Definition at line 320 of file initdb.c.

◆ PG_CMD_PRINTF

#define PG_CMD_PRINTF (   fmt,
  ... 
)
Value:
do { \
if (fprintf(cmdfd, fmt, __VA_ARGS__) < 0 || fflush(cmdfd) < 0) \
output_failed = true, output_errno = errno; \
} while (0)
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
static int output_errno
Definition: initdb.c:194

Definition at line 339 of file initdb.c.

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

Definition at line 333 of file initdb.c.

Typedef Documentation

◆ _stringlist

typedef struct _stringlist _stringlist

◆ save_locale_t

typedef char* save_locale_t

Definition at line 348 of file initdb.c.

Function Documentation

◆ add_stringlist_item()

static void add_stringlist_item ( _stringlist **  listhead,
const char *  str 
)
static

Definition at line 444 of file initdb.c.

445{
446 _stringlist *newentry = pg_malloc(sizeof(_stringlist));
447 _stringlist *oldentry;
448
449 newentry->str = pg_strdup(str);
450 newentry->next = NULL;
451 if (*listhead == NULL)
452 *listhead = newentry;
453 else
454 {
455 for (oldentry = *listhead; oldentry->next; oldentry = oldentry->next)
456 /* skip */ ;
457 oldentry->next = newentry;
458 }
459}
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * str
char * str
Definition: initdb.c:92
struct _stringlist * next
Definition: initdb.c:93

References _stringlist::next, pg_malloc(), pg_strdup(), _stringlist::str, and str.

Referenced by ecpg_start_test(), isolation_init(), isolation_start_test(), main(), psql_init(), and psql_start_test().

◆ bootstrap_template1()

static void bootstrap_template1 ( void  )
static

Definition at line 1557 of file initdb.c.

1558{
1560 PQExpBufferData cmd;
1561 char **line;
1562 char **bki_lines;
1563 char headerline[MAXPGPATH];
1564 char buf[64];
1565
1566 printf(_("running bootstrap script ... "));
1567 fflush(stdout);
1568
1569 bki_lines = readfile(bki_file);
1570
1571 /* Check that bki file appears to be of the right version */
1572
1573 snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1574 PG_MAJORVERSION);
1575
1576 if (strcmp(headerline, *bki_lines) != 0)
1577 {
1578 pg_log_error("input file \"%s\" does not belong to PostgreSQL %s",
1579 bki_file, PG_VERSION);
1580 pg_log_error_hint("Specify the correct path using the option -L.");
1581 exit(1);
1582 }
1583
1584 /* Substitute for various symbols used in the BKI file */
1585
1586 sprintf(buf, "%d", NAMEDATALEN);
1587 bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1588
1589 sprintf(buf, "%d", (int) sizeof(Pointer));
1590 bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
1591
1592 bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
1593 (sizeof(Pointer) == 4) ? "i" : "d");
1594
1595 bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
1596 FLOAT8PASSBYVAL ? "true" : "false");
1597
1598 bki_lines = replace_token(bki_lines, "POSTGRES",
1600
1601 bki_lines = replace_token(bki_lines, "ENCODING",
1603
1604 bki_lines = replace_token(bki_lines, "LC_COLLATE",
1606
1607 bki_lines = replace_token(bki_lines, "LC_CTYPE",
1609
1610 bki_lines = replace_token(bki_lines, "DATLOCALE",
1611 datlocale ? escape_quotes_bki(datlocale) : "_null_");
1612
1613 bki_lines = replace_token(bki_lines, "ICU_RULES",
1614 icu_rules ? escape_quotes_bki(icu_rules) : "_null_");
1615
1616 sprintf(buf, "%c", locale_provider);
1617 bki_lines = replace_token(bki_lines, "LOCALE_PROVIDER", buf);
1618
1619 /* Also ensure backend isn't confused by this environment var: */
1620 unsetenv("PGCLIENTENCODING");
1621
1622 initPQExpBuffer(&cmd);
1623
1624 printfPQExpBuffer(&cmd, "\"%s\" --boot %s %s", backend_exec, boot_options, extra_options);
1625 appendPQExpBuffer(&cmd, " -X %d", wal_segment_size_mb * (1024 * 1024));
1626 if (data_checksums)
1627 appendPQExpBuffer(&cmd, " -k");
1628 if (debug)
1629 appendPQExpBuffer(&cmd, " -d 5");
1630
1631
1632 PG_CMD_OPEN(cmd.data);
1633
1634 for (line = bki_lines; *line != NULL; line++)
1635 {
1636 PG_CMD_PUTS(*line);
1637 free(*line);
1638 }
1639
1640 PG_CMD_CLOSE();
1641
1642 termPQExpBuffer(&cmd);
1643 free(bki_lines);
1644
1645 check_ok();
1646}
char * Pointer
Definition: c.h:493
#define FLOAT8PASSBYVAL
Definition: c.h:606
#define _(x)
Definition: elog.c:90
#define free(a)
Definition: header.h:65
static char * escape_quotes_bki(const char *src)
Definition: initdb.c:421
static char * encodingid_to_string(int enc)
Definition: initdb.c:833
#define PG_CMD_CLOSE()
Definition: initdb.c:327
static char * datlocale
Definition: initdb.c:149
static char * lc_collate
Definition: initdb.c:141
#define PG_CMD_PUTS(line)
Definition: initdb.c:333
static char * lc_ctype
Definition: initdb.c:142
static char ** readfile(const char *path)
Definition: initdb.c:675
static bool debug
Definition: initdb.c:161
static char backend_exec[MAXPGPATH]
Definition: initdb.c:259
static bool data_checksums
Definition: initdb.c:167
#define PG_CMD_DECL
Definition: initdb.c:318
static char locale_provider
Definition: initdb.c:147
static int wal_segment_size_mb
Definition: initdb.c:169
static char ** replace_token(char **lines, const char *token, const char *replacement)
Definition: initdb.c:472
static char * icu_rules
Definition: initdb.c:151
static char * username
Definition: initdb.c:153
static int encodingid
Definition: initdb.c:175
static void check_ok(void)
Definition: initdb.c:2121
static char * extra_options
Definition: initdb.c:228
static const char *const boot_options
Definition: initdb.c:224
#define PG_CMD_OPEN(cmd)
Definition: initdb.c:320
static char * bki_file
Definition: initdb.c:176
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
#define NAMEDATALEN
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:72
#define sprintf
Definition: port.h:241
#define snprintf
Definition: port.h:239
#define printf(...)
Definition: port.h:245
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
#define unsetenv(x)
Definition: win32_port.h:546

References _, appendPQExpBuffer(), backend_exec, bki_file, boot_options, buf, check_ok(), PQExpBufferData::data, data_checksums, datlocale, debug, encodingid, encodingid_to_string(), escape_quotes_bki(), extra_options, FLOAT8PASSBYVAL, free, icu_rules, initPQExpBuffer(), lc_collate, lc_ctype, locale_provider, MAXPGPATH, NAMEDATALEN, PG_CMD_CLOSE, PG_CMD_DECL, PG_CMD_OPEN, PG_CMD_PUTS, pg_log_error, pg_log_error_hint, printf, printfPQExpBuffer(), readfile(), replace_token(), snprintf, sprintf, generate_unaccent_rules::stdout, termPQExpBuffer(), unsetenv, username, and wal_segment_size_mb.

Referenced by initialize_data_directory().

◆ check_authmethod_unspecified()

static void check_authmethod_unspecified ( const char **  authmethod)
static

Definition at line 2583 of file initdb.c.

2584{
2585 if (*authmethod == NULL)
2586 {
2587 authwarning = true;
2588 *authmethod = "trust";
2589 }
2590}
static bool authwarning
Definition: initdb.c:212

References authwarning.

Referenced by main().

◆ check_authmethod_valid()

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

Definition at line 2593 of file initdb.c.

2594{
2595 const char *const *p;
2596
2597 for (p = valid_methods; *p; p++)
2598 {
2599 if (strcmp(authmethod, *p) == 0)
2600 return;
2601 }
2602
2603 pg_fatal("invalid authentication method \"%s\" for \"%s\" connections",
2604 authmethod, conntype);
2605}
#define pg_fatal(...)

References pg_fatal.

Referenced by main().

◆ check_icu_locale_encoding()

static bool check_icu_locale_encoding ( int  user_enc)
static

Definition at line 2312 of file initdb.c.

2313{
2314 if (!(is_encoding_supported_by_icu(user_enc)))
2315 {
2316 pg_log_error("encoding mismatch");
2317 pg_log_error_detail("The encoding you selected (%s) is not supported with the ICU provider.",
2318 pg_encoding_to_char(user_enc));
2319 pg_log_error_hint("Rerun %s and either do not specify an encoding explicitly, "
2320 "or choose a matching combination.",
2321 progname);
2322 return false;
2323 }
2324 return true;
2325}
bool is_encoding_supported_by_icu(int encoding)
Definition: encnames.c:461
static const char * progname
Definition: initdb.c:174
#define pg_log_error_detail(...)
Definition: logging.h:109
#define pg_encoding_to_char
Definition: pg_wchar.h:630

References is_encoding_supported_by_icu(), pg_encoding_to_char, pg_log_error, pg_log_error_detail, pg_log_error_hint, and progname.

Referenced by setup_locale_encoding().

◆ check_input()

static void check_input ( char *  path)
static

Definition at line 992 of file initdb.c.

993{
994 struct stat statbuf;
995
996 if (stat(path, &statbuf) != 0)
997 {
998 if (errno == ENOENT)
999 {
1000 pg_log_error("file \"%s\" does not exist", path);
1001 pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
1002 }
1003 else
1004 {
1005 pg_log_error("could not access file \"%s\": %m", path);
1006 pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
1007 }
1008 exit(1);
1009 }
1010 if (!S_ISREG(statbuf.st_mode))
1011 {
1012 pg_log_error("file \"%s\" is not a regular file", path);
1013 pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
1014 exit(1);
1015 }
1016}
#define stat
Definition: win32_port.h:274
#define S_ISREG(m)
Definition: win32_port.h:318

References pg_log_error, pg_log_error_hint, S_ISREG, stat::st_mode, and stat.

Referenced by setup_data_file_paths().

◆ check_locale_encoding()

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

Definition at line 2277 of file initdb.c.

2278{
2279 int locale_enc;
2280
2281 locale_enc = pg_get_encoding_from_locale(locale, true);
2282
2283 /* See notes in createdb() to understand these tests */
2284 if (!(locale_enc == user_enc ||
2285 locale_enc == PG_SQL_ASCII ||
2286 locale_enc == -1 ||
2287#ifdef WIN32
2288 user_enc == PG_UTF8 ||
2289#endif
2290 user_enc == PG_SQL_ASCII))
2291 {
2292 pg_log_error("encoding mismatch");
2293 pg_log_error_detail("The encoding you selected (%s) and the encoding that the "
2294 "selected locale uses (%s) do not match. This would lead to "
2295 "misbehavior in various character string processing functions.",
2296 pg_encoding_to_char(user_enc),
2297 pg_encoding_to_char(locale_enc));
2298 pg_log_error_hint("Rerun %s and either do not specify an encoding explicitly, "
2299 "or choose a matching combination.",
2300 progname);
2301 return false;
2302 }
2303 return true;
2304}
static char * locale
Definition: initdb.c:140
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
@ PG_UTF8
Definition: pg_wchar.h:232
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
Definition: chklocale.c:301

References locale, pg_encoding_to_char, pg_get_encoding_from_locale(), pg_log_error, pg_log_error_detail, pg_log_error_hint, PG_SQL_ASCII, PG_UTF8, and progname.

Referenced by setup_locale_encoding().

◆ check_locale_name()

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

Definition at line 2214 of file initdb.c.

2215{
2216 save_locale_t save;
2217 char *res;
2218
2219 /* Don't let Windows' non-ASCII locale names in. */
2220 if (locale && !pg_is_ascii(locale))
2221 pg_fatal("locale name \"%s\" contains non-ASCII characters", locale);
2222
2223 if (canonname)
2224 *canonname = NULL; /* in case of failure */
2225
2226 save = save_global_locale(category);
2227
2228 /* for setlocale() call */
2229 if (!locale)
2230 locale = "";
2231
2232 /* set the locale with setlocale, to see if it accepts it. */
2233 res = setlocale(category, locale);
2234
2235 /* save canonical name if requested. */
2236 if (res && canonname)
2237 *canonname = pg_strdup(res);
2238
2239 /* restore old value. */
2240 restore_global_locale(category, save);
2241
2242 /* complain if locale wasn't valid */
2243 if (res == NULL)
2244 {
2245 if (*locale)
2246 {
2247 pg_log_error("invalid locale name \"%s\"", locale);
2248 pg_log_error_hint("If the locale name is specific to ICU, use --icu-locale.");
2249 exit(1);
2250 }
2251 else
2252 {
2253 /*
2254 * If no relevant switch was given on command line, locale is an
2255 * empty string, which is not too helpful to report. Presumably
2256 * setlocale() found something it did not like in the environment.
2257 * Ideally we'd report the bad environment variable, but since
2258 * setlocale's behavior is implementation-specific, it's hard to
2259 * be sure what it didn't like. Print a safe generic message.
2260 */
2261 pg_fatal("invalid locale settings; check LANG and LC_* environment variables");
2262 }
2263 }
2264
2265 /* Don't let Windows' non-ASCII locale names out. */
2266 if (canonname && !pg_is_ascii(*canonname))
2267 pg_fatal("locale name \"%s\" contains non-ASCII characters",
2268 *canonname);
2269}
static void restore_global_locale(int category, save_locale_t save)
Definition: initdb.c:388
static save_locale_t save_global_locale(int category)
Definition: initdb.c:364
char * save_locale_t
Definition: initdb.c:348
bool pg_is_ascii(const char *str)
Definition: string.c:132
#define setlocale(a, b)
Definition: win32_port.h:475

References locale, pg_fatal, pg_is_ascii(), pg_log_error, pg_log_error_hint, pg_strdup(), restore_global_locale(), save_global_locale(), and setlocale.

Referenced by setlocales().

◆ check_need_password()

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

Definition at line 2608 of file initdb.c.

2609{
2610 if ((strcmp(authmethodlocal, "md5") == 0 ||
2611 strcmp(authmethodlocal, "password") == 0 ||
2612 strcmp(authmethodlocal, "scram-sha-256") == 0) &&
2613 (strcmp(authmethodhost, "md5") == 0 ||
2614 strcmp(authmethodhost, "password") == 0 ||
2615 strcmp(authmethodhost, "scram-sha-256") == 0) &&
2616 !(pwprompt || pwfilename))
2617 pg_fatal("must specify a password for the superuser to enable password authentication");
2618}
static const char * authmethodhost
Definition: initdb.c:157
static char * pwfilename
Definition: initdb.c:155
static bool pwprompt
Definition: initdb.c:154
static const char * authmethodlocal
Definition: initdb.c:158

References authmethodhost, authmethodlocal, pg_fatal, pwfilename, and pwprompt.

Referenced by main().

◆ check_ok()

static void check_ok ( void  )
static

Definition at line 2121 of file initdb.c.

2122{
2123 if (caught_signal)
2124 {
2125 printf(_("caught signal\n"));
2126 fflush(stdout);
2127 exit(1);
2128 }
2129 else if (output_failed)
2130 {
2131 printf(_("could not write to child process: %s\n"),
2133 fflush(stdout);
2134 exit(1);
2135 }
2136 else
2137 {
2138 /* all seems well */
2139 printf(_("ok\n"));
2140 fflush(stdout);
2141 }
2142}
static bool output_failed
Definition: initdb.c:193
static bool caught_signal
Definition: initdb.c:192
#define strerror
Definition: port.h:252

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

Referenced by adjust_data_dir(), bootstrap_template1(), check_cluster_versions(), check_for_connection_status(), check_for_data_types_usage(), check_for_incompatible_polymorphics(), check_for_isn_and_int8_passing_mismatch(), check_for_new_tablespace_dir(), check_for_pg_role_prefix(), check_for_prepared_transactions(), check_for_tables_with_oids(), check_for_user_defined_encoding_conversions(), check_for_user_defined_postfix_ops(), check_is_install_user(), check_loadable_libraries(), check_new_cluster_logical_replication_slots(), check_new_cluster_subscription_configuration(), check_old_cluster_for_valid_slots(), check_old_cluster_subscription_state(), copy_subdir_files(), copy_xact_xlog_xid(), create_data_directory(), create_logical_replication_slots(), create_new_objects(), create_script_for_old_cluster_deletion(), create_xlog_or_symlink(), disable_old_cluster(), generate_old_dump(), initialize_data_directory(), main(), old_9_6_invalidate_hash_indexes(), prepare_new_cluster(), prepare_new_globals(), remove_new_subdir(), report_extension_updates(), set_frozenxids(), set_locale_and_encoding(), set_new_cluster_char_signedness(), setup_config(), and transfer_all_new_tablespaces().

◆ choose_dsm_implementation()

static const char * choose_dsm_implementation ( void  )
static

Definition at line 1075 of file initdb.c.

1076{
1077#if defined(HAVE_SHM_OPEN) && !defined(__sun__)
1078 int ntries = 10;
1080
1081 /* Initialize prng; this function is its only user in this program. */
1082 pg_prng_seed(&prng_state, (uint64) (getpid() ^ time(NULL)));
1083
1084 while (ntries > 0)
1085 {
1086 uint32 handle;
1087 char name[64];
1088 int fd;
1089
1090 handle = pg_prng_uint32(&prng_state);
1091 snprintf(name, 64, "/PostgreSQL.%u", handle);
1092 if ((fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0600)) != -1)
1093 {
1094 close(fd);
1095 shm_unlink(name);
1096 return "posix";
1097 }
1098 if (errno != EEXIST)
1099 break;
1100 --ntries;
1101 }
1102#endif
1103
1104#ifdef WIN32
1105 return "windows";
1106#else
1107 return "sysv";
1108#endif
1109}
uint64_t uint64
Definition: c.h:503
uint32_t uint32
Definition: c.h:502
#define close(a)
Definition: win32.h:12
uint32 pg_prng_uint32(pg_prng_state *state)
Definition: pg_prng.c:227
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:89
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static pg_prng_state prng_state
const char * name

References close, fd(), name, pg_prng_seed(), pg_prng_uint32(), prng_state, and snprintf.

Referenced by test_config_settings().

◆ cleanup_directories_atexit()

static void cleanup_directories_atexit ( void  )
static

Definition at line 761 of file initdb.c.

762{
763 if (success)
764 return;
765
766 if (!noclean)
767 {
768 if (made_new_pgdata)
769 {
770 pg_log_info("removing data directory \"%s\"", pg_data);
771 if (!rmtree(pg_data, true))
772 pg_log_error("failed to remove data directory");
773 }
774 else if (found_existing_pgdata)
775 {
776 pg_log_info("removing contents of data directory \"%s\"",
777 pg_data);
778 if (!rmtree(pg_data, false))
779 pg_log_error("failed to remove contents of data directory");
780 }
781
783 {
784 pg_log_info("removing WAL directory \"%s\"", xlog_dir);
785 if (!rmtree(xlog_dir, true))
786 pg_log_error("failed to remove WAL directory");
787 }
788 else if (found_existing_xlogdir)
789 {
790 pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
791 if (!rmtree(xlog_dir, false))
792 pg_log_error("failed to remove contents of WAL directory");
793 }
794 /* otherwise died during startup, do nothing! */
795 }
796 else
797 {
799 pg_log_info("data directory \"%s\" not removed at user's request",
800 pg_data);
801
803 pg_log_info("WAL directory \"%s\" not removed at user's request",
804 xlog_dir);
805 }
806}
static bool noclean
Definition: initdb.c:162
static bool found_existing_pgdata
Definition: initdb.c:188
static bool found_existing_xlogdir
Definition: initdb.c:190
static char * xlog_dir
Definition: initdb.c:168
static bool success
Definition: initdb.c:186
static bool made_new_xlogdir
Definition: initdb.c:189
static char * pg_data
Definition: initdb.c:138
static bool made_new_pgdata
Definition: initdb.c:187
#define pg_log_info(...)
Definition: logging.h:124
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:50

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

Referenced by main().

◆ create_data_directory()

void create_data_directory ( void  )

Definition at line 2901 of file initdb.c.

2902{
2903 int ret;
2904
2905 switch ((ret = pg_check_dir(pg_data)))
2906 {
2907 case 0:
2908 /* PGDATA not there, must create it */
2909 printf(_("creating directory %s ... "),
2910 pg_data);
2911 fflush(stdout);
2912
2914 pg_fatal("could not create directory \"%s\": %m", pg_data);
2915 else
2916 check_ok();
2917
2918 made_new_pgdata = true;
2919 break;
2920
2921 case 1:
2922 /* Present but empty, fix permissions and use it */
2923 printf(_("fixing permissions on existing directory %s ... "),
2924 pg_data);
2925 fflush(stdout);
2926
2927 if (chmod(pg_data, pg_dir_create_mode) != 0)
2928 pg_fatal("could not change permissions of directory \"%s\": %m",
2929 pg_data);
2930 else
2931 check_ok();
2932
2933 found_existing_pgdata = true;
2934 break;
2935
2936 case 2:
2937 case 3:
2938 case 4:
2939 /* Present and not empty */
2940 pg_log_error("directory \"%s\" exists but is not empty", pg_data);
2941 if (ret != 4)
2943 else
2944 pg_log_error_hint("If you want to create a new database system, either remove or empty "
2945 "the directory \"%s\" or run %s "
2946 "with an argument other than \"%s\".",
2948 exit(1); /* no further message needed */
2949
2950 default:
2951 /* Trouble accessing directory */
2952 pg_fatal("could not access directory \"%s\": %m", pg_data);
2953 }
2954}
int pg_dir_create_mode
Definition: file_perm.c:18
void warn_on_mount_point(int error)
Definition: initdb.c:3042
int pg_mkdir_p(char *path, int omode)
Definition: pgmkdirp.c:57
int pg_check_dir(const char *dir)
Definition: pgcheckdir.c:33

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

Referenced by initialize_data_directory().

◆ create_xlog_or_symlink()

void create_xlog_or_symlink ( void  )

Definition at line 2959 of file initdb.c.

2960{
2961 char *subdirloc;
2962
2963 /* form name of the place for the subdirectory or symlink */
2964 subdirloc = psprintf("%s/pg_wal", pg_data);
2965
2966 if (xlog_dir)
2967 {
2968 int ret;
2969
2970 /* clean up xlog directory name, check it's absolute */
2973 pg_fatal("WAL directory location must be an absolute path");
2974
2975 /* check if the specified xlog directory exists/is empty */
2976 switch ((ret = pg_check_dir(xlog_dir)))
2977 {
2978 case 0:
2979 /* xlog directory not there, must create it */
2980 printf(_("creating directory %s ... "),
2981 xlog_dir);
2982 fflush(stdout);
2983
2985 pg_fatal("could not create directory \"%s\": %m",
2986 xlog_dir);
2987 else
2988 check_ok();
2989
2990 made_new_xlogdir = true;
2991 break;
2992
2993 case 1:
2994 /* Present but empty, fix permissions and use it */
2995 printf(_("fixing permissions on existing directory %s ... "),
2996 xlog_dir);
2997 fflush(stdout);
2998
2999 if (chmod(xlog_dir, pg_dir_create_mode) != 0)
3000 pg_fatal("could not change permissions of directory \"%s\": %m",
3001 xlog_dir);
3002 else
3003 check_ok();
3004
3006 break;
3007
3008 case 2:
3009 case 3:
3010 case 4:
3011 /* Present and not empty */
3012 pg_log_error("directory \"%s\" exists but is not empty", xlog_dir);
3013 if (ret != 4)
3015 else
3016 pg_log_error_hint("If you want to store the WAL there, either remove or empty the directory \"%s\".",
3017 xlog_dir);
3018 exit(1);
3019
3020 default:
3021 /* Trouble accessing directory */
3022 pg_fatal("could not access directory \"%s\": %m", xlog_dir);
3023 }
3024
3025 if (symlink(xlog_dir, subdirloc) != 0)
3026 pg_fatal("could not create symbolic link \"%s\": %m",
3027 subdirloc);
3028 }
3029 else
3030 {
3031 /* Without -X option, just make the subdirectory normally */
3032 if (mkdir(subdirloc, pg_dir_create_mode) < 0)
3033 pg_fatal("could not create directory \"%s\": %m",
3034 subdirloc);
3035 }
3036
3037 free(subdirloc);
3038}
#define is_absolute_path(filename)
Definition: port.h:104
void canonicalize_path(char *path)
Definition: path.c:337
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define mkdir(a, b)
Definition: win32_port.h:80
#define symlink(oldpath, newpath)
Definition: win32_port.h:225

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

Referenced by initialize_data_directory().

◆ encodingid_to_string()

static char * encodingid_to_string ( int  enc)
static

Definition at line 833 of file initdb.c.

834{
835 char result[20];
836
837 sprintf(result, "%d", enc);
838 return pg_strdup(result);
839}
enc

References enc, pg_strdup(), and sprintf.

Referenced by bootstrap_template1().

◆ escape_quotes()

static char * escape_quotes ( const char *  src)
static

Definition at line 405 of file initdb.c.

406{
407 char *result = escape_single_quotes_ascii(src);
408
409 if (!result)
410 pg_fatal("out of memory");
411 return result;
412}
char * escape_single_quotes_ascii(const char *src)
Definition: quotes.c:33

References escape_single_quotes_ascii(), and pg_fatal.

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

◆ escape_quotes_bki()

static char * escape_quotes_bki ( const char *  src)
static

Definition at line 421 of file initdb.c.

422{
423 char *result;
424 char *data = escape_quotes(src);
425 char *resultp;
426 char *datap;
427
428 result = (char *) pg_malloc(strlen(data) + 3);
429 resultp = result;
430 *resultp++ = '\'';
431 for (datap = data; *datap; datap++)
432 *resultp++ = *datap;
433 *resultp++ = '\'';
434 *resultp = '\0';
435
436 free(data);
437 return result;
438}
static char * escape_quotes(const char *src)
Definition: initdb.c:405
const void * data

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

Referenced by bootstrap_template1().

◆ find_matching_ts_config()

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

Definition at line 937 of file initdb.c.

938{
939 int i;
940 char *langname,
941 *ptr;
942
943 /*
944 * Convert lc_ctype to a language name by stripping everything after an
945 * underscore (usual case) or a hyphen (Windows "locale name"; see
946 * comments at IsoLocaleName()).
947 *
948 * XXX Should ' ' be a stop character? This would select "norwegian" for
949 * the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
950 * should also accept the "nn" and "nb" Unix locales.
951 *
952 * Just for paranoia, we also stop at '.' or '@'.
953 */
954 if (lc_type == NULL)
955 langname = pg_strdup("");
956 else
957 {
958 ptr = langname = pg_strdup(lc_type);
959 while (*ptr &&
960 *ptr != '_' && *ptr != '-' && *ptr != '.' && *ptr != '@')
961 ptr++;
962 *ptr = '\0';
963 }
964
966 {
967 if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
968 {
969 free(langname);
971 }
972 }
973
974 free(langname);
975 return NULL;
976}
static const struct tsearch_config_match tsearch_config_languages[]
Definition: initdb.c:868
int i
Definition: isn.c:74
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
const char * tsconfname
Definition: initdb.c:864

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

Referenced by setup_text_search().

◆ get_encoding_id()

static int get_encoding_id ( const char *  encoding_name)
static

Definition at line 845 of file initdb.c.

846{
847 int enc;
848
849 if (encoding_name && *encoding_name)
850 {
851 if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
852 return enc;
853 }
854 pg_fatal("\"%s\" is not a valid server encoding name",
855 encoding_name ? encoding_name : "(null)");
856}
#define pg_valid_server_encoding
Definition: pg_wchar.h:631

References enc, pg_fatal, and pg_valid_server_encoding.

Referenced by setup_locale_encoding().

◆ get_id()

static char * get_id ( void  )
static

Definition at line 814 of file initdb.c.

815{
816 const char *username;
817
818#ifndef WIN32
819 if (geteuid() == 0) /* 0 is root's uid */
820 {
821 pg_log_error("cannot be run as root");
822 pg_log_error_hint("Please log in (using, e.g., \"su\") as the (unprivileged) user that will own the server process.");
823 exit(1);
824 }
825#endif
826
828
829 return pg_strdup(username);
830}
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74

References get_user_name_or_exit(), pg_log_error, pg_log_error_hint, pg_strdup(), progname, and username.

Referenced by main().

◆ get_su_pwd()

static void get_su_pwd ( void  )
static

Definition at line 1669 of file initdb.c.

1670{
1671 char *pwd1;
1672
1673 if (pwprompt)
1674 {
1675 /*
1676 * Read password from terminal
1677 */
1678 char *pwd2;
1679
1680 printf("\n");
1681 fflush(stdout);
1682 pwd1 = simple_prompt("Enter new superuser password: ", false);
1683 pwd2 = simple_prompt("Enter it again: ", false);
1684 if (strcmp(pwd1, pwd2) != 0)
1685 {
1686 fprintf(stderr, _("Passwords didn't match.\n"));
1687 exit(1);
1688 }
1689 free(pwd2);
1690 }
1691 else
1692 {
1693 /*
1694 * Read password from file
1695 *
1696 * Ideally this should insist that the file not be world-readable.
1697 * However, this option is mainly intended for use on Windows where
1698 * file permissions may not exist at all, so we'll skip the paranoia
1699 * for now.
1700 */
1701 FILE *pwf = fopen(pwfilename, "r");
1702
1703 if (!pwf)
1704 pg_fatal("could not open file \"%s\" for reading: %m",
1705 pwfilename);
1706 pwd1 = pg_get_line(pwf, NULL);
1707 if (!pwd1)
1708 {
1709 if (ferror(pwf))
1710 pg_fatal("could not read password from file \"%s\": %m",
1711 pwfilename);
1712 else
1713 pg_fatal("password file \"%s\" is empty",
1714 pwfilename);
1715 }
1716 fclose(pwf);
1717
1718 (void) pg_strip_crlf(pwd1);
1719 }
1720
1721 superuser_password = pwd1;
1722}
static char * superuser_password
Definition: initdb.c:156
char * pg_get_line(FILE *stream, PromptInterruptContext *prompt_ctx)
Definition: pg_get_line.c:59
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
int pg_strip_crlf(char *str)
Definition: string.c:154

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

Referenced by main().

◆ guc_value_requires_quotes()

static bool guc_value_requires_quotes ( const char *  guc_value)
static

Definition at line 643 of file initdb.c.

644{
645 /* Don't use <ctype.h> macros here, they might accept too much */
646#define LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
647#define DIGITS "0123456789"
648
649 if (*guc_value == '\0')
650 return true; /* empty string must be quoted */
651 if (strchr(LETTERS, *guc_value))
652 {
653 if (strspn(guc_value, LETTERS DIGITS) == strlen(guc_value))
654 return false; /* it's an identifier */
655 return true; /* nope */
656 }
657 if (strchr(DIGITS, *guc_value))
658 {
659 /* skip over digits */
660 guc_value += strspn(guc_value, DIGITS);
661 /* there can be zero or more unit letters after the digits */
662 if (strspn(guc_value, LETTERS) == strlen(guc_value))
663 return false; /* it's a number, possibly with units */
664 return true; /* nope */
665 }
666 return true; /* all else must be quoted */
667}
#define DIGITS
#define LETTERS

References DIGITS, and LETTERS.

Referenced by replace_guc_value().

◆ icu_language_tag()

static char * icu_language_tag ( const char *  loc_str)
static

Definition at line 2332 of file initdb.c.

2333{
2334#ifdef USE_ICU
2335 UErrorCode status;
2336 char *langtag;
2337 size_t buflen = 32; /* arbitrary starting buffer size */
2338 const bool strict = true;
2339
2340 /*
2341 * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
2342 * RFC5646 section 4.4). Additionally, in older ICU versions,
2343 * uloc_toLanguageTag() doesn't always return the ultimate length on the
2344 * first call, necessitating a loop.
2345 */
2346 langtag = pg_malloc(buflen);
2347 while (true)
2348 {
2349 status = U_ZERO_ERROR;
2350 uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
2351
2352 /* try again if the buffer is not large enough */
2353 if (status == U_BUFFER_OVERFLOW_ERROR ||
2354 status == U_STRING_NOT_TERMINATED_WARNING)
2355 {
2356 buflen = buflen * 2;
2357 langtag = pg_realloc(langtag, buflen);
2358 continue;
2359 }
2360
2361 break;
2362 }
2363
2364 if (U_FAILURE(status))
2365 {
2366 pg_free(langtag);
2367
2368 pg_fatal("could not convert locale name \"%s\" to language tag: %s",
2369 loc_str, u_errorName(status));
2370 }
2371
2372 return langtag;
2373#else
2374 pg_fatal("ICU is not supported in this build");
2375 return NULL; /* keep compiler quiet */
2376#endif
2377}
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65

References pg_fatal, pg_free(), pg_malloc(), and pg_realloc().

Referenced by setlocales().

◆ icu_validate_locale()

static void icu_validate_locale ( const char *  loc_str)
static

Definition at line 2385 of file initdb.c.

2386{
2387#ifdef USE_ICU
2388 UErrorCode status;
2389 char lang[ULOC_LANG_CAPACITY];
2390 bool found = false;
2391
2392 /* validate that we can extract the language */
2393 status = U_ZERO_ERROR;
2394 uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
2395 if (U_FAILURE(status))
2396 {
2397 pg_fatal("could not get language from locale \"%s\": %s",
2398 loc_str, u_errorName(status));
2399 return;
2400 }
2401
2402 /* check for special language name */
2403 if (strcmp(lang, "") == 0 ||
2404 strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
2405 found = true;
2406
2407 /* search for matching language within ICU */
2408 for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
2409 {
2410 const char *otherloc = uloc_getAvailable(i);
2411 char otherlang[ULOC_LANG_CAPACITY];
2412
2413 status = U_ZERO_ERROR;
2414 uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
2415 if (U_FAILURE(status))
2416 continue;
2417
2418 if (strcmp(lang, otherlang) == 0)
2419 found = true;
2420 }
2421
2422 if (!found)
2423 pg_fatal("locale \"%s\" has unknown language \"%s\"",
2424 loc_str, lang);
2425#else
2426 pg_fatal("ICU is not supported in this build");
2427#endif
2428}

References i, and pg_fatal.

Referenced by setlocales().

◆ initialize_data_directory()

void initialize_data_directory ( void  )

Definition at line 3055 of file initdb.c.

3056{
3058 PQExpBufferData cmd;
3059 int i;
3060
3061 setup_signals();
3062
3063 /*
3064 * Set mask based on requested PGDATA permissions. pg_mode_mask, and
3065 * friends like pg_dir_create_mode, are set to owner-only by default and
3066 * then updated if -g is passed in by calling SetDataDirectoryCreatePerm()
3067 * when parsing our options (see above).
3068 */
3069 umask(pg_mode_mask);
3070
3072
3074
3075 /* Create required subdirectories (other than pg_wal) */
3076 printf(_("creating subdirectories ... "));
3077 fflush(stdout);
3078
3079 for (i = 0; i < lengthof(subdirs); i++)
3080 {
3081 char *path;
3082
3083 path = psprintf("%s/%s", pg_data, subdirs[i]);
3084
3085 /*
3086 * The parent directory already exists, so we only need mkdir() not
3087 * pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3088 */
3089 if (mkdir(path, pg_dir_create_mode) < 0)
3090 pg_fatal("could not create directory \"%s\": %m", path);
3091
3092 free(path);
3093 }
3094
3095 check_ok();
3096
3097 /* Top level PG_VERSION is checked by bootstrapper, so make it first */
3098 write_version_file(NULL);
3099
3100 /* Select suitable configuration settings */
3101 set_null_conf();
3103
3104 /* Now create all the text config files */
3105 setup_config();
3106
3107 /* Bootstrap template1 */
3109
3110 /*
3111 * Make the per-database PG_VERSION for template1 only after init'ing it
3112 */
3113 write_version_file("base/1");
3114
3115 /*
3116 * Create the stuff we don't need to use bootstrap mode for, using a
3117 * backend running in simple standalone mode.
3118 */
3119 fputs(_("performing post-bootstrap initialization ... "), stdout);
3120 fflush(stdout);
3121
3122 initPQExpBuffer(&cmd);
3123 printfPQExpBuffer(&cmd, "\"%s\" %s %s template1 >%s",
3125
3126 PG_CMD_OPEN(cmd.data);
3127
3128 setup_auth(cmdfd);
3129
3131
3133
3134 setup_depend(cmdfd);
3135
3136 /*
3137 * Note that no objects created after setup_depend() will be "pinned".
3138 * They are all droppable at the whim of the DBA.
3139 */
3140
3142
3143 setup_description(cmdfd);
3144
3145 setup_collation(cmdfd);
3146
3148
3149 setup_privileges(cmdfd);
3150
3151 setup_schema(cmdfd);
3152
3153 load_plpgsql(cmdfd);
3154
3155 vacuum_db(cmdfd);
3156
3157 make_template0(cmdfd);
3158
3159 make_postgres(cmdfd);
3160
3161 PG_CMD_CLOSE();
3162 termPQExpBuffer(&cmd);
3163
3164 check_ok();
3165}
#define lengthof(array)
Definition: c.h:759
int pg_mode_mask
Definition: file_perm.c:25
static void setup_depend(FILE *cmdfd)
Definition: initdb.c:1728
static void setup_collation(FILE *cmdfd)
Definition: initdb.c:1783
void create_data_directory(void)
Definition: initdb.c:2901
static void bootstrap_template1(void)
Definition: initdb.c:1557
static void setup_run_file(FILE *cmdfd, const char *filename)
Definition: initdb.c:1741
static void setup_auth(FILE *cmdfd)
Definition: initdb.c:1652
static void make_postgres(FILE *cmdfd)
Definition: initdb.c:2077
static void test_config_settings(void)
Definition: initdb.c:1117
static const char *const backend_options
Definition: initdb.c:225
static void setup_config(void)
Definition: initdb.c:1290
static void setup_privileges(FILE *cmdfd)
Definition: initdb.c:1816
static void write_version_file(const char *extrapath)
Definition: initdb.c:1023
void setup_signals(void)
Definition: initdb.c:2881
static char * system_views_file
Definition: initdb.c:185
static void setup_description(FILE *cmdfd)
Definition: initdb.c:1762
static void vacuum_db(FILE *cmdfd)
Definition: initdb.c:2013
static const char *const subdirs[]
Definition: initdb.c:230
void create_xlog_or_symlink(void)
Definition: initdb.c:2959
static char * system_functions_file
Definition: initdb.c:184
static char * dictionary_file
Definition: initdb.c:180
static void setup_schema(FILE *cmdfd)
Definition: initdb.c:1984
static char * system_constraints_file
Definition: initdb.c:183
static void set_null_conf(void)
Definition: initdb.c:1046
static void make_template0(FILE *cmdfd)
Definition: initdb.c:2023
static void load_plpgsql(FILE *cmdfd)
Definition: initdb.c:2004
#define DEVNULL
Definition: port.h:161

References _, backend_exec, backend_options, bootstrap_template1(), check_ok(), create_data_directory(), create_xlog_or_symlink(), PQExpBufferData::data, DEVNULL, dictionary_file, extra_options, free, i, initPQExpBuffer(), lengthof, load_plpgsql(), make_postgres(), make_template0(), mkdir, PG_CMD_CLOSE, PG_CMD_DECL, PG_CMD_OPEN, pg_data, pg_dir_create_mode, pg_fatal, pg_mode_mask, printf, printfPQExpBuffer(), psprintf(), set_null_conf(), setup_auth(), setup_collation(), setup_config(), setup_depend(), setup_description(), setup_privileges(), setup_run_file(), setup_schema(), setup_signals(), generate_unaccent_rules::stdout, subdirs, system_constraints_file, system_functions_file, system_views_file, termPQExpBuffer(), test_config_settings(), vacuum_db(), and write_version_file().

Referenced by main().

◆ load_plpgsql()

static void load_plpgsql ( FILE *  cmdfd)
static

Definition at line 2004 of file initdb.c.

2005{
2006 PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n\n");
2007}

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ locale_date_order()

static int locale_date_order ( const char *  locale)
static

Definition at line 2155 of file initdb.c.

2156{
2157 struct tm testtime;
2158 char buf[128];
2159 char *posD;
2160 char *posM;
2161 char *posY;
2162 save_locale_t save;
2163 size_t res;
2164 int result;
2165
2166 result = DATEORDER_MDY; /* default */
2167
2168 save = save_global_locale(LC_TIME);
2169
2170 setlocale(LC_TIME, locale);
2171
2172 memset(&testtime, 0, sizeof(testtime));
2173 testtime.tm_mday = 22;
2174 testtime.tm_mon = 10; /* November, should come out as "11" */
2175 testtime.tm_year = 133; /* 2033 */
2176
2177 res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2178
2179 restore_global_locale(LC_TIME, save);
2180
2181 if (res == 0)
2182 return result;
2183
2184 posM = strstr(buf, "11");
2185 posD = strstr(buf, "22");
2186 posY = strstr(buf, "33");
2187
2188 if (!posM || !posD || !posY)
2189 return result;
2190
2191 if (posY < posM && posM < posD)
2192 result = DATEORDER_YMD;
2193 else if (posD < posM)
2194 result = DATEORDER_DMY;
2195 else
2196 result = DATEORDER_MDY;
2197
2198 return result;
2199}
static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
Definition: initdb.c:2146
static struct pg_tm tm
Definition: localtime.c:104
#define DATEORDER_DMY
Definition: miscadmin.h:243
#define DATEORDER_MDY
Definition: miscadmin.h:244
#define DATEORDER_YMD
Definition: miscadmin.h:242

References buf, DATEORDER_DMY, DATEORDER_MDY, DATEORDER_YMD, locale, my_strftime(), restore_global_locale(), save_global_locale(), setlocale, and tm.

Referenced by setup_config().

◆ main()

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

Definition at line 3169 of file initdb.c.

3170{
3171 static struct option long_options[] = {
3172 {"pgdata", required_argument, NULL, 'D'},
3173 {"encoding", required_argument, NULL, 'E'},
3174 {"locale", required_argument, NULL, 1},
3175 {"lc-collate", required_argument, NULL, 2},
3176 {"lc-ctype", required_argument, NULL, 3},
3177 {"lc-monetary", required_argument, NULL, 4},
3178 {"lc-numeric", required_argument, NULL, 5},
3179 {"lc-time", required_argument, NULL, 6},
3180 {"lc-messages", required_argument, NULL, 7},
3181 {"no-locale", no_argument, NULL, 8},
3182 {"text-search-config", required_argument, NULL, 'T'},
3183 {"auth", required_argument, NULL, 'A'},
3184 {"auth-local", required_argument, NULL, 10},
3185 {"auth-host", required_argument, NULL, 11},
3186 {"pwprompt", no_argument, NULL, 'W'},
3187 {"pwfile", required_argument, NULL, 9},
3188 {"username", required_argument, NULL, 'U'},
3189 {"help", no_argument, NULL, '?'},
3190 {"version", no_argument, NULL, 'V'},
3191 {"debug", no_argument, NULL, 'd'},
3192 {"show", no_argument, NULL, 's'},
3193 {"noclean", no_argument, NULL, 'n'}, /* for backwards compatibility */
3194 {"no-clean", no_argument, NULL, 'n'},
3195 {"nosync", no_argument, NULL, 'N'}, /* for backwards compatibility */
3196 {"no-sync", no_argument, NULL, 'N'},
3197 {"no-instructions", no_argument, NULL, 13},
3198 {"set", required_argument, NULL, 'c'},
3199 {"sync-only", no_argument, NULL, 'S'},
3200 {"waldir", required_argument, NULL, 'X'},
3201 {"wal-segsize", required_argument, NULL, 12},
3202 {"data-checksums", no_argument, NULL, 'k'},
3203 {"allow-group-access", no_argument, NULL, 'g'},
3204 {"discard-caches", no_argument, NULL, 14},
3205 {"locale-provider", required_argument, NULL, 15},
3206 {"builtin-locale", required_argument, NULL, 16},
3207 {"icu-locale", required_argument, NULL, 17},
3208 {"icu-rules", required_argument, NULL, 18},
3209 {"sync-method", required_argument, NULL, 19},
3210 {"no-data-checksums", no_argument, NULL, 20},
3211 {NULL, 0, NULL, 0}
3212 };
3213
3214 /*
3215 * options with no short version return a low integer, the rest return
3216 * their short version value
3217 */
3218 int c;
3219 int option_index;
3220 char *effective_user;
3221 PQExpBuffer start_db_cmd;
3222 char pg_ctl_path[MAXPGPATH];
3223
3224 /*
3225 * Ensure that buffering behavior of stdout matches what it is in
3226 * interactive usage (at least on most platforms). This prevents
3227 * unexpected output ordering when, eg, output is redirected to a file.
3228 * POSIX says we must do this before any other usage of these files.
3229 */
3230 setvbuf(stdout, NULL, PG_IOLBF, 0);
3231
3232 pg_logging_init(argv[0]);
3233 progname = get_progname(argv[0]);
3234 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
3235
3236 if (argc > 1)
3237 {
3238 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
3239 {
3240 usage(progname);
3241 exit(0);
3242 }
3243 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
3244 {
3245 puts("initdb (PostgreSQL) " PG_VERSION);
3246 exit(0);
3247 }
3248 }
3249
3250 /* process command-line options */
3251
3252 while ((c = getopt_long(argc, argv, "A:c:dD:E:gkL:nNsST:U:WX:",
3253 long_options, &option_index)) != -1)
3254 {
3255 switch (c)
3256 {
3257 case 'A':
3259
3260 /*
3261 * When ident is specified, use peer for local connections.
3262 * Mirrored, when peer is specified, use ident for TCP/IP
3263 * connections.
3264 */
3265 if (strcmp(authmethodhost, "ident") == 0)
3266 authmethodlocal = "peer";
3267 else if (strcmp(authmethodlocal, "peer") == 0)
3268 authmethodhost = "ident";
3269 break;
3270 case 10:
3272 break;
3273 case 11:
3275 break;
3276 case 'c':
3277 {
3278 char *buf = pg_strdup(optarg);
3279 char *equals = strchr(buf, '=');
3280
3281 if (!equals)
3282 {
3283 pg_log_error("-c %s requires a value", buf);
3284 pg_log_error_hint("Try \"%s --help\" for more information.",
3285 progname);
3286 exit(1);
3287 }
3288 *equals++ = '\0'; /* terminate variable name */
3291 pfree(buf);
3292 }
3293 break;
3294 case 'D':
3296 break;
3297 case 'E':
3299 break;
3300 case 'W':
3301 pwprompt = true;
3302 break;
3303 case 'U':
3305 break;
3306 case 'd':
3307 debug = true;
3308 printf(_("Running in debug mode.\n"));
3309 break;
3310 case 'n':
3311 noclean = true;
3312 printf(_("Running in no-clean mode. Mistakes will not be cleaned up.\n"));
3313 break;
3314 case 'N':
3315 do_sync = false;
3316 break;
3317 case 'S':
3318 sync_only = true;
3319 break;
3320 case 'k':
3321 data_checksums = true;
3322 break;
3323 case 'L':
3325 break;
3326 case 1:
3328 break;
3329 case 2:
3331 break;
3332 case 3:
3334 break;
3335 case 4:
3337 break;
3338 case 5:
3340 break;
3341 case 6:
3343 break;
3344 case 7:
3346 break;
3347 case 8:
3348 locale = "C";
3349 break;
3350 case 9:
3352 break;
3353 case 's':
3354 show_setting = true;
3355 break;
3356 case 'T':
3358 break;
3359 case 'X':
3361 break;
3362 case 12:
3363 if (!option_parse_int(optarg, "--wal-segsize", 1, 1024, &wal_segment_size_mb))
3364 exit(1);
3365 break;
3366 case 13:
3367 noinstructions = true;
3368 break;
3369 case 'g':
3371 break;
3372 case 14:
3373 extra_options = psprintf("%s %s",
3375 "-c debug_discard_caches=1");
3376 break;
3377 case 15:
3378 if (strcmp(optarg, "builtin") == 0)
3379 locale_provider = COLLPROVIDER_BUILTIN;
3380 else if (strcmp(optarg, "icu") == 0)
3381 locale_provider = COLLPROVIDER_ICU;
3382 else if (strcmp(optarg, "libc") == 0)
3383 locale_provider = COLLPROVIDER_LIBC;
3384 else
3385 pg_fatal("unrecognized locale provider: %s", optarg);
3386 break;
3387 case 16:
3390 break;
3391 case 17:
3393 icu_locale_specified = true;
3394 break;
3395 case 18:
3397 break;
3398 case 19:
3400 exit(1);
3401 break;
3402 case 20:
3403 data_checksums = false;
3404 break;
3405 default:
3406 /* getopt_long already emitted a complaint */
3407 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
3408 exit(1);
3409 }
3410 }
3411
3412
3413 /*
3414 * Non-option argument specifies data directory as long as it wasn't
3415 * already specified with -D / --pgdata
3416 */
3417 if (optind < argc && !pg_data)
3418 {
3419 pg_data = pg_strdup(argv[optind]);
3420 optind++;
3421 }
3422
3423 if (optind < argc)
3424 {
3425 pg_log_error("too many command-line arguments (first is \"%s\")",
3426 argv[optind]);
3427 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
3428 exit(1);
3429 }
3430
3431 if (builtin_locale_specified && locale_provider != COLLPROVIDER_BUILTIN)
3432 pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3433 "--builtin-locale", "builtin");
3434
3435 if (icu_locale_specified && locale_provider != COLLPROVIDER_ICU)
3436 pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3437 "--icu-locale", "icu");
3438
3439 if (icu_rules && locale_provider != COLLPROVIDER_ICU)
3440 pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3441 "--icu-rules", "icu");
3442
3444
3445 /* If we only need to sync, just do it and exit */
3446 if (sync_only)
3447 {
3448 setup_pgdata();
3449
3450 /* must check that directory is readable */
3451 if (pg_check_dir(pg_data) <= 0)
3452 pg_fatal("could not access directory \"%s\": %m", pg_data);
3453
3454 fputs(_("syncing data to disk ... "), stdout);
3455 fflush(stdout);
3456 sync_pgdata(pg_data, PG_VERSION_NUM, sync_method);
3457 check_ok();
3458 return 0;
3459 }
3460
3461 if (pwprompt && pwfilename)
3462 pg_fatal("password prompt and password file cannot be specified together");
3463
3466
3469
3471
3472 if (!IsValidWalSegSize(wal_segment_size_mb * 1024 * 1024))
3473 pg_fatal("argument of %s must be a power of two between 1 and 1024", "--wal-segsize");
3474
3476
3477 setup_pgdata();
3478
3479 setup_bin_paths(argv[0]);
3480
3481 effective_user = get_id();
3482 if (!username)
3483 username = effective_user;
3484
3485 if (strncmp(username, "pg_", 3) == 0)
3486 pg_fatal("superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"", username);
3487
3488 printf(_("The files belonging to this database system will be owned "
3489 "by user \"%s\".\n"
3490 "This user must also own the server process.\n\n"),
3491 effective_user);
3492
3494
3496
3498
3500
3501 printf("\n");
3502
3503 if (data_checksums)
3504 printf(_("Data page checksums are enabled.\n"));
3505 else
3506 printf(_("Data page checksums are disabled.\n"));
3507
3508 if (pwprompt || pwfilename)
3509 get_su_pwd();
3510
3511 printf("\n");
3512
3514
3515 if (do_sync)
3516 {
3517 fputs(_("syncing data to disk ... "), stdout);
3518 fflush(stdout);
3519 sync_pgdata(pg_data, PG_VERSION_NUM, sync_method);
3520 check_ok();
3521 }
3522 else
3523 printf(_("\nSync to disk skipped.\nThe data directory might become corrupt if the operating system crashes.\n"));
3524
3525 if (authwarning)
3526 {
3527 printf("\n");
3528 pg_log_warning("enabling \"trust\" authentication for local connections");
3529 pg_log_warning_hint("You can change this by editing pg_hba.conf or using the option -A, or "
3530 "--auth-local and --auth-host, the next time you run initdb.");
3531 }
3532
3533 if (!noinstructions)
3534 {
3535 /*
3536 * Build up a shell command to tell the user how to start the server
3537 */
3538 start_db_cmd = createPQExpBuffer();
3539
3540 /* Get directory specification used to start initdb ... */
3541 strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
3544 /* ... and tag on pg_ctl instead */
3546
3547 /* Convert the path to use native separators */
3549
3550 /* path to pg_ctl, properly quoted */
3551 appendShellString(start_db_cmd, pg_ctl_path);
3552
3553 /* add -D switch, with properly quoted data directory */
3554 appendPQExpBufferStr(start_db_cmd, " -D ");
3555 appendShellString(start_db_cmd, pgdata_native);
3556
3557 /* add suggested -l switch and "start" command */
3558 /* translator: This is a placeholder in a shell command. */
3559 appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
3560
3561 printf(_("\nSuccess. You can now start the database server using:\n\n"
3562 " %s\n\n"),
3563 start_db_cmd->data);
3564
3565 destroyPQExpBuffer(start_db_cmd);
3566 }
3567
3568
3569 success = true;
3570 return 0;
3571}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
void SetDataDirectoryCreatePerm(int dataDirMode)
Definition: file_perm.c:34
#define PG_DIR_MODE_GROUP
Definition: file_perm.h:35
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
static void usage(const char *progname)
Definition: initdb.c:2529
static char * lc_time
Definition: initdb.c:145
static char * get_id(void)
Definition: initdb.c:814
static char * pgdata_native
Definition: initdb.c:195
static void check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
Definition: initdb.c:2593
static bool icu_locale_specified
Definition: initdb.c:150
static bool noinstructions
Definition: initdb.c:163
void initialize_data_directory(void)
Definition: initdb.c:3055
void setup_text_search(void)
Definition: initdb.c:2847
static char * share_path
Definition: initdb.c:135
void setup_bin_paths(const char *argv0)
Definition: initdb.c:2659
static void check_authmethod_unspecified(const char **authmethod)
Definition: initdb.c:2583
static const char *const auth_methods_host[]
Definition: initdb.c:96
void setup_locale_encoding(void)
Definition: initdb.c:2696
static const char *const auth_methods_local[]
Definition: initdb.c:118
static bool sync_only
Definition: initdb.c:165
static void check_need_password(const char *authmethodlocal, const char *authmethodhost)
Definition: initdb.c:2608
static bool do_sync
Definition: initdb.c:164
static void cleanup_directories_atexit(void)
Definition: initdb.c:761
static char * lc_messages
Definition: initdb.c:146
static bool builtin_locale_specified
Definition: initdb.c:148
static void set_info_version(void)
Definition: initdb.c:1957
void setup_data_file_paths(void)
Definition: initdb.c:2803
static char * encoding
Definition: initdb.c:139
static DataDirSyncMethod sync_method
Definition: initdb.c:170
static _stringlist * extra_guc_names
Definition: initdb.c:159
static void add_stringlist_item(_stringlist **listhead, const char *str)
Definition: initdb.c:444
static _stringlist * extra_guc_values
Definition: initdb.c:160
static char * lc_numeric
Definition: initdb.c:144
void setup_pgdata(void)
Definition: initdb.c:2622
static bool show_setting
Definition: initdb.c:166
static const char * default_text_search_config
Definition: initdb.c:152
static char * lc_monetary
Definition: initdb.c:143
static void get_su_pwd(void)
Definition: initdb.c:1669
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_warning_hint(...)
Definition: logging.h:121
void pfree(void *pointer)
Definition: mcxt.c:1524
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
bool parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
Definition: option_utils.c:90
static char * pg_ctl_path
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
#define pg_log_warning(...)
Definition: pgfnames.c:24
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:286
#define PG_IOLBF
Definition: port.h:389
void get_parent_directory(char *path)
Definition: path.c:1068
void make_native_path(char *filename)
Definition: path.c:236
const char * get_progname(const char *argv0)
Definition: path.c:652
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * c
void get_restricted_token(void)
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:582
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96

References _, add_stringlist_item(), appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), auth_methods_host, auth_methods_local, authmethodhost, authmethodlocal, authwarning, buf, builtin_locale_specified, canonicalize_path(), check_authmethod_unspecified(), check_authmethod_valid(), check_need_password(), check_ok(), cleanup_directories_atexit(), createPQExpBuffer(), PQExpBufferData::data, data_checksums, datlocale, debug, default_text_search_config, destroyPQExpBuffer(), do_sync, encoding, extra_guc_names, extra_guc_values, extra_options, get_id(), get_parent_directory(), get_progname(), get_restricted_token(), get_su_pwd(), getopt_long(), icu_locale_specified, icu_rules, initialize_data_directory(), IsValidWalSegSize, join_path_components(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, locale, locale_provider, make_native_path(), MAXPGPATH, no_argument, noclean, noinstructions, optarg, optind, option_parse_int(), parse_sync_method(), pfree(), pg_check_dir(), pg_ctl_path, pg_data, PG_DIR_MODE_GROUP, pg_fatal, PG_IOLBF, pg_log_error, pg_log_error_hint, pg_log_warning, pg_log_warning_hint, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, pgdata_native, printf, progname, psprintf(), pwfilename, pwprompt, required_argument, set_info_version(), set_pglocale_pgservice(), SetDataDirectoryCreatePerm(), setup_bin_paths(), setup_data_file_paths(), setup_locale_encoding(), setup_pgdata(), setup_text_search(), share_path, show_setting, generate_unaccent_rules::stdout, strlcpy(), success, sync_method, sync_only, usage(), username, wal_segment_size_mb, and xlog_dir.

◆ make_postgres()

static void make_postgres ( FILE *  cmdfd)
static

Definition at line 2077 of file initdb.c.

2078{
2079 /*
2080 * Just as we did for template0, and for the same reasons, assign a fixed
2081 * OID to postgres and select the file_copy strategy.
2082 */
2083 PG_CMD_PUTS("CREATE DATABASE postgres OID = " CppAsString2(PostgresDbOid)
2084 " STRATEGY = file_copy;\n\n");
2085 PG_CMD_PUTS("COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n");
2086}
#define CppAsString2(x)
Definition: c.h:363

References CppAsString2, and PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ make_template0()

static void make_template0 ( FILE *  cmdfd)
static

Definition at line 2023 of file initdb.c.

2024{
2025 /*
2026 * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
2027 * enough to drop and recreate a conflicting database with the same name,
2028 * but if the same OID were used for one system-created database in the
2029 * old cluster and a different system-created database in the new cluster,
2030 * it would fail. To avoid that, assign a fixed OID to template0 rather
2031 * than letting the server choose one.
2032 *
2033 * (Note that, while the user could have dropped and recreated these
2034 * objects in the old cluster, the problem scenario only exists if the OID
2035 * that is in use in the old cluster is also used in the new cluster - and
2036 * the new cluster should be the result of a fresh initdb.)
2037 *
2038 * We use "STRATEGY = file_copy" here because checkpoints during initdb
2039 * are cheap. "STRATEGY = wal_log" would generate more WAL, which would be
2040 * a little bit slower and make the new cluster a little bit bigger.
2041 */
2042 PG_CMD_PUTS("CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false"
2043 " OID = " CppAsString2(Template0DbOid)
2044 " STRATEGY = file_copy;\n\n");
2045
2046 /*
2047 * template0 shouldn't have any collation-dependent objects, so unset the
2048 * collation version. This disables collation version checks when making
2049 * a new database from it.
2050 */
2051 PG_CMD_PUTS("UPDATE pg_database SET datcollversion = NULL WHERE datname = 'template0';\n\n");
2052
2053 /*
2054 * While we are here, do set the collation version on template1.
2055 */
2056 PG_CMD_PUTS("UPDATE pg_database SET datcollversion = pg_database_collation_actual_version(oid) WHERE datname = 'template1';\n\n");
2057
2058 /*
2059 * Explicitly revoke public create-schema and create-temp-table privileges
2060 * in template1 and template0; else the latter would be on by default
2061 */
2062 PG_CMD_PUTS("REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n");
2063 PG_CMD_PUTS("REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n");
2064
2065 PG_CMD_PUTS("COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n");
2066
2067 /*
2068 * Finally vacuum to clean up dead rows in pg_database
2069 */
2070 PG_CMD_PUTS("VACUUM pg_database;\n\n");
2071}

References CppAsString2, and PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ my_strftime()

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

Definition at line 2146 of file initdb.c.

2147{
2148 return strftime(s, max, fmt, tm);
2149}

References tm.

Referenced by locale_date_order().

◆ popen_check()

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

Definition at line 744 of file initdb.c.

745{
746 FILE *cmdfd;
747
748 fflush(NULL);
749 errno = 0;
750 cmdfd = popen(command, mode);
751 if (cmdfd == NULL)
752 pg_log_error("could not execute command \"%s\": %m", command);
753 return cmdfd;
754}
static PgChecksumMode mode
Definition: pg_checksums.c:55

References mode, and pg_log_error.

◆ pretty_wal_size()

static char * pretty_wal_size ( int  segment_count)
static

Definition at line 1273 of file initdb.c.

1274{
1275 int sz = wal_segment_size_mb * segment_count;
1276 char *result = pg_malloc(14);
1277
1278 if ((sz % 1024) == 0)
1279 snprintf(result, 14, "%dGB", sz / 1024);
1280 else
1281 snprintf(result, 14, "%dMB", sz);
1282
1283 return result;
1284}

References pg_malloc(), snprintf, and wal_segment_size_mb.

Referenced by setup_config().

◆ readfile()

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

Definition at line 675 of file initdb.c.

676{
677 char **result;
678 FILE *infile;
679 StringInfoData line;
680 int maxlines;
681 int n;
682
683 if ((infile = fopen(path, "r")) == NULL)
684 pg_fatal("could not open file \"%s\" for reading: %m", path);
685
686 initStringInfo(&line);
687
688 maxlines = 1024;
689 result = (char **) pg_malloc(maxlines * sizeof(char *));
690
691 n = 0;
692 while (pg_get_line_buf(infile, &line))
693 {
694 /* make sure there will be room for a trailing NULL pointer */
695 if (n >= maxlines - 1)
696 {
697 maxlines *= 2;
698 result = (char **) pg_realloc(result, maxlines * sizeof(char *));
699 }
700
701 result[n++] = pg_strdup(line.data);
702 }
703 result[n] = NULL;
704
705 pfree(line.data);
706
707 fclose(infile);
708
709 return result;
710}
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:95
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
static void infile(const char *name)
Definition: zic.c:1243

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

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

◆ replace_guc_value()

static char ** replace_guc_value ( char **  lines,
const char *  guc_name,
const char *  guc_value,
bool  mark_as_comment 
)
static

Definition at line 527 of file initdb.c.

529{
530 int namelen = strlen(guc_name);
532 int i;
533
534 /* prepare the replacement line, except for possible comment and newline */
535 if (mark_as_comment)
537 appendPQExpBuffer(newline, "%s = ", guc_name);
538 if (guc_value_requires_quotes(guc_value))
539 appendPQExpBuffer(newline, "'%s'", escape_quotes(guc_value));
540 else
541 appendPQExpBufferStr(newline, guc_value);
542
543 for (i = 0; lines[i]; i++)
544 {
545 const char *where;
546 const char *namestart;
547
548 /*
549 * Look for a line assigning to guc_name. Typically it will be
550 * preceded by '#', but that might not be the case if a -c switch
551 * overrides a previous assignment. We allow leading whitespace too,
552 * although normally there wouldn't be any.
553 */
554 where = lines[i];
555 while (*where == '#' || isspace((unsigned char) *where))
556 where++;
557 if (pg_strncasecmp(where, guc_name, namelen) != 0)
558 continue;
559 namestart = where;
560 where += namelen;
561 while (isspace((unsigned char) *where))
562 where++;
563 if (*where != '=')
564 continue;
565
566 /* found it -- let's use the canonical casing shown in the file */
567 memcpy(&newline->data[mark_as_comment ? 1 : 0], namestart, namelen);
568
569 /* now append the original comment if any */
570 where = strrchr(where, '#');
571 if (where)
572 {
573 /*
574 * We try to preserve original indentation, which is tedious.
575 * oldindent and newindent are measured in de-tab-ified columns.
576 */
577 const char *ptr;
578 int oldindent = 0;
579 int newindent;
580
581 for (ptr = lines[i]; ptr < where; ptr++)
582 {
583 if (*ptr == '\t')
584 oldindent += 8 - (oldindent % 8);
585 else
586 oldindent++;
587 }
588 /* ignore the possibility of tabs in guc_value */
589 newindent = newline->len;
590 /* append appropriate tabs and spaces, forcing at least one */
591 oldindent = Max(oldindent, newindent + 1);
592 while (newindent < oldindent)
593 {
594 int newindent_if_tab = newindent + 8 - (newindent % 8);
595
596 if (newindent_if_tab <= oldindent)
597 {
599 newindent = newindent_if_tab;
600 }
601 else
602 {
604 newindent++;
605 }
606 }
607 /* and finally append the old comment */
609 /* we'll have appended the original newline; don't add another */
610 }
611 else
613
614 free(lines[i]);
615 lines[i] = newline->data;
616
617 break; /* assume there's only one match */
618 }
619
620 if (lines[i] == NULL)
621 {
622 /*
623 * No match, so append a new entry. (We rely on the bootstrap server
624 * to complain if it's not a valid GUC name.)
625 */
627 lines = pg_realloc_array(lines, char *, i + 2);
628 lines[i++] = newline->data;
629 lines[i] = NULL; /* keep the array null-terminated */
630 }
631
632 free(newline); /* but don't free newline->data */
633
634 return lines;
635}
#define Max(x, y)
Definition: c.h:969
#define pg_realloc_array(pointer, type, count)
Definition: fe_memutils.h:63
#define newline
Definition: indent_codes.h:35
static bool guc_value_requires_quotes(const char *guc_value)
Definition: initdb.c:643
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), createPQExpBuffer(), escape_quotes(), free, guc_value_requires_quotes(), i, Max, newline, pg_realloc_array, and pg_strncasecmp().

Referenced by setup_config().

◆ replace_token()

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

Definition at line 472 of file initdb.c.

473{
474 int toklen,
475 replen,
476 diff;
477
478 toklen = strlen(token);
479 replen = strlen(replacement);
480 diff = replen - toklen;
481
482 for (int i = 0; lines[i]; i++)
483 {
484 char *where;
485 char *newline;
486 int pre;
487
488 /* nothing to do if no change needed */
489 if ((where = strstr(lines[i], token)) == NULL)
490 continue;
491
492 /* if we get here a change is needed - set up new line */
493
494 newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
495
496 pre = where - lines[i];
497
498 memcpy(newline, lines[i], pre);
499
500 memcpy(newline + pre, replacement, replen);
501
502 strcpy(newline + pre + replen, lines[i] + pre + toklen);
503
504 free(lines[i]);
505 lines[i] = newline;
506 }
507
508 return lines;
509}

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

Referenced by bootstrap_template1(), and setup_config().

◆ restore_global_locale()

static void restore_global_locale ( int  category,
save_locale_t  save 
)
static

Definition at line 388 of file initdb.c.

389{
390#ifdef WIN32
391 if (!_wsetlocale(category, save))
392 pg_fatal("failed to restore old locale");
393#else
394 if (!setlocale(category, save))
395 pg_fatal("failed to restore old locale \"%s\"", save);
396#endif
397 free(save);
398}

References free, pg_fatal, and setlocale.

Referenced by check_locale_name(), and locale_date_order().

◆ save_global_locale()

static save_locale_t save_global_locale ( int  category)
static

Definition at line 364 of file initdb.c.

365{
366 save_locale_t save;
367
368#ifdef WIN32
369 save = _wsetlocale(category, NULL);
370 if (!save)
371 pg_fatal("_wsetlocale() failed");
372 save = wcsdup(save);
373 if (!save)
374 pg_fatal("out of memory");
375#else
376 save = setlocale(category, NULL);
377 if (!save)
378 pg_fatal("setlocale() failed");
379 save = pg_strdup(save);
380#endif
381 return save;
382}

References pg_fatal, pg_strdup(), and setlocale.

Referenced by check_locale_name(), and locale_date_order().

◆ select_default_timezone()

const char * select_default_timezone ( const char *  share_path)

Definition at line 1757 of file findtimezone.c.

1758{
1759 const char *tzname;
1760
1761 /* Initialize timezone directory path, if needed */
1762#ifndef SYSTEMTZDIR
1763 snprintf(tzdirpath, sizeof(tzdirpath), "%s/timezone", share_path);
1764#endif
1765
1766 /* Check TZ environment variable */
1767 tzname = getenv("TZ");
1768 if (validate_zone(tzname))
1769 return tzname;
1770
1771 /* Nope, so try to identify the system timezone */
1772 tzname = identify_system_timezone();
1773 if (validate_zone(tzname))
1774 return tzname;
1775
1776 return NULL;
1777}
static const char * identify_system_timezone(void)
Definition: findtimezone.c:331
static bool validate_zone(const char *tzname)
static char tzdirpath[MAXPGPATH]
Definition: findtimezone.c:27

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

Referenced by test_config_settings().

◆ set_info_version()

static void set_info_version ( void  )
static

Definition at line 1957 of file initdb.c.

1958{
1959 char *letterversion;
1960 long major = 0,
1961 minor = 0,
1962 micro = 0;
1963 char *endptr;
1964 char *vstr = pg_strdup(PG_VERSION);
1965 char *ptr;
1966
1967 ptr = vstr + (strlen(vstr) - 1);
1968 while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
1969 ptr--;
1970 letterversion = ptr + 1;
1971 major = strtol(vstr, &endptr, 10);
1972 if (*endptr)
1973 minor = strtol(endptr + 1, &endptr, 10);
1974 if (*endptr)
1975 micro = strtol(endptr + 1, &endptr, 10);
1976 snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1977 major, minor, micro, letterversion);
1978}
static char infoversion[100]
Definition: initdb.c:191

References infoversion, pg_strdup(), and snprintf.

Referenced by main().

◆ set_input()

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

Definition at line 983 of file initdb.c.

984{
985 *dest = psprintf("%s/%s", share_path, filename);
986}
static char * filename
Definition: pg_dumpall.c:124

References generate_unaccent_rules::dest, filename, psprintf(), and share_path.

Referenced by setup_data_file_paths().

◆ set_null_conf()

static void set_null_conf ( void  )
static

Definition at line 1046 of file initdb.c.

1047{
1048 FILE *conf_file;
1049 char *path;
1050
1051 path = psprintf("%s/postgresql.conf", pg_data);
1052 conf_file = fopen(path, PG_BINARY_W);
1053 if (conf_file == NULL)
1054 pg_fatal("could not open file \"%s\" for writing: %m", path);
1055 if (fclose(conf_file))
1056 pg_fatal("could not write file \"%s\": %m", path);
1057 free(path);
1058}
#define PG_BINARY_W
Definition: c.h:1247
static char * conf_file
Definition: initdb.c:179

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

Referenced by initialize_data_directory().

◆ setlocales()

static void setlocales ( void  )
static

Definition at line 2436 of file initdb.c.

2437{
2438 char *canonname;
2439
2440 /* set empty lc_* and datlocale values to locale config if set */
2441
2442 if (locale)
2443 {
2444 if (!lc_ctype)
2445 lc_ctype = locale;
2446 if (!lc_collate)
2448 if (!lc_numeric)
2450 if (!lc_time)
2451 lc_time = locale;
2452 if (!lc_monetary)
2454 if (!lc_messages)
2456 if (!datlocale && locale_provider != COLLPROVIDER_LIBC)
2457 datlocale = locale;
2458 }
2459
2460 /*
2461 * canonicalize locale names, and obtain any missing values from our
2462 * current environment
2463 */
2464 check_locale_name(LC_CTYPE, lc_ctype, &canonname);
2465 lc_ctype = canonname;
2466 check_locale_name(LC_COLLATE, lc_collate, &canonname);
2467 lc_collate = canonname;
2468 check_locale_name(LC_NUMERIC, lc_numeric, &canonname);
2469 lc_numeric = canonname;
2470 check_locale_name(LC_TIME, lc_time, &canonname);
2471 lc_time = canonname;
2472 check_locale_name(LC_MONETARY, lc_monetary, &canonname);
2473 lc_monetary = canonname;
2474#if defined(LC_MESSAGES) && !defined(WIN32)
2475 check_locale_name(LC_MESSAGES, lc_messages, &canonname);
2476 lc_messages = canonname;
2477#else
2478 /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2479 check_locale_name(LC_CTYPE, lc_messages, &canonname);
2480 lc_messages = canonname;
2481#endif
2482
2483 if (locale_provider != COLLPROVIDER_LIBC && datlocale == NULL)
2484 pg_fatal("locale must be specified if provider is %s",
2485 collprovider_name(locale_provider));
2486
2487 if (locale_provider == COLLPROVIDER_BUILTIN)
2488 {
2489 if (strcmp(datlocale, "C") == 0)
2490 canonname = "C";
2491 else if (strcmp(datlocale, "C.UTF-8") == 0 ||
2492 strcmp(datlocale, "C.UTF8") == 0)
2493 canonname = "C.UTF-8";
2494 else if (strcmp(datlocale, "PG_UNICODE_FAST") == 0)
2495 canonname = "PG_UNICODE_FAST";
2496 else
2497 pg_fatal("invalid locale name \"%s\" for builtin provider",
2498 datlocale);
2499
2500 datlocale = canonname;
2501 }
2502 else if (locale_provider == COLLPROVIDER_ICU)
2503 {
2504 char *langtag;
2505
2506 /* canonicalize to a language tag */
2507 langtag = icu_language_tag(datlocale);
2508 printf(_("Using language tag \"%s\" for ICU locale \"%s\".\n"),
2509 langtag, datlocale);
2511 datlocale = langtag;
2512
2514
2515 /*
2516 * In supported builds, the ICU locale ID will be opened during
2517 * post-bootstrap initialization, which will perform extra checks.
2518 */
2519#ifndef USE_ICU
2520 pg_fatal("ICU is not supported in this build");
2521#endif
2522 }
2523}
static char * icu_language_tag(const char *loc_str)
Definition: initdb.c:2332
static void check_locale_name(int category, const char *locale, char **canonname)
Definition: initdb.c:2214
static void icu_validate_locale(const char *loc_str)
Definition: initdb.c:2385

References _, check_locale_name(), datlocale, icu_language_tag(), icu_validate_locale(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, locale, locale_provider, pg_fatal, pg_free(), and printf.

Referenced by setup_locale_encoding().

◆ setup_auth()

static void setup_auth ( FILE *  cmdfd)
static

Definition at line 1652 of file initdb.c.

1653{
1654 /*
1655 * The authid table shouldn't be readable except through views, to ensure
1656 * passwords are not publicly visible.
1657 */
1658 PG_CMD_PUTS("REVOKE ALL ON pg_authid FROM public;\n\n");
1659
1661 PG_CMD_PRINTF("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1663}
#define PG_CMD_PRINTF(fmt,...)
Definition: initdb.c:339

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

Referenced by initialize_data_directory().

◆ setup_bin_paths()

void setup_bin_paths ( const char *  argv0)

Definition at line 2659 of file initdb.c.

2660{
2661 int ret;
2662
2663 if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
2664 backend_exec)) < 0)
2665 {
2666 char full_path[MAXPGPATH];
2667
2668 if (find_my_exec(argv0, full_path) < 0)
2669 strlcpy(full_path, progname, sizeof(full_path));
2670
2671 if (ret == -1)
2672 pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
2673 "postgres", progname, full_path);
2674 else
2675 pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
2676 "postgres", full_path, progname);
2677 }
2678
2679 /* store binary directory */
2680 strcpy(bin_path, backend_exec);
2683
2684 if (!share_path)
2685 {
2688 }
2689 else if (!is_absolute_path(share_path))
2690 pg_fatal("input file location must be an absolute path");
2691
2693}
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:310
static char bin_path[MAXPGPATH]
Definition: initdb.c:258
static char * argv0
Definition: pg_ctl.c:93
void get_share_path(const char *my_exec_path, char *ret_path)
Definition: path.c:902
char * last_dir_separator(const char *filename)
Definition: path.c:145
#define PG_BACKEND_VERSIONSTR
Definition: port.h:144

References argv0, 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_fatal, pg_malloc(), progname, share_path, and strlcpy().

Referenced by main().

◆ setup_collation()

static void setup_collation ( FILE *  cmdfd)
static

Definition at line 1783 of file initdb.c.

1784{
1785 /*
1786 * Set the collation version for collations defined in pg_collation.dat,
1787 * but not the ones where we know that the collation behavior will never
1788 * change.
1789 */
1790 PG_CMD_PUTS("UPDATE pg_collation SET collversion = pg_collation_actual_version(oid) WHERE collname = 'unicode';\n\n");
1791
1792 /* Import all collations we can find in the operating system */
1793 PG_CMD_PUTS("SELECT pg_import_system_collations('pg_catalog');\n\n");
1794}

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ setup_config()

static void setup_config ( void  )
static

Definition at line 1290 of file initdb.c.

1291{
1292 char **conflines;
1293 char repltok[MAXPGPATH];
1294 char path[MAXPGPATH];
1295 _stringlist *gnames,
1296 *gvalues;
1297
1298 fputs(_("creating configuration files ... "), stdout);
1299 fflush(stdout);
1300
1301 /* postgresql.conf */
1302
1303 conflines = readfile(conf_file);
1304
1305 snprintf(repltok, sizeof(repltok), "%d", n_connections);
1306 conflines = replace_guc_value(conflines, "max_connections",
1307 repltok, false);
1308
1309 snprintf(repltok, sizeof(repltok), "%d", n_av_slots);
1310 conflines = replace_guc_value(conflines, "autovacuum_worker_slots",
1311 repltok, false);
1312
1313 if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1314 snprintf(repltok, sizeof(repltok), "%dMB",
1315 (n_buffers * (BLCKSZ / 1024)) / 1024);
1316 else
1317 snprintf(repltok, sizeof(repltok), "%dkB",
1318 n_buffers * (BLCKSZ / 1024));
1319 conflines = replace_guc_value(conflines, "shared_buffers",
1320 repltok, false);
1321
1322 conflines = replace_guc_value(conflines, "lc_messages",
1323 lc_messages, false);
1324
1325 conflines = replace_guc_value(conflines, "lc_monetary",
1326 lc_monetary, false);
1327
1328 conflines = replace_guc_value(conflines, "lc_numeric",
1329 lc_numeric, false);
1330
1331 conflines = replace_guc_value(conflines, "lc_time",
1332 lc_time, false);
1333
1334 switch (locale_date_order(lc_time))
1335 {
1336 case DATEORDER_YMD:
1337 strcpy(repltok, "iso, ymd");
1338 break;
1339 case DATEORDER_DMY:
1340 strcpy(repltok, "iso, dmy");
1341 break;
1342 case DATEORDER_MDY:
1343 default:
1344 strcpy(repltok, "iso, mdy");
1345 break;
1346 }
1347 conflines = replace_guc_value(conflines, "datestyle",
1348 repltok, false);
1349
1350 snprintf(repltok, sizeof(repltok), "pg_catalog.%s",
1352 conflines = replace_guc_value(conflines, "default_text_search_config",
1353 repltok, false);
1354
1355 if (default_timezone)
1356 {
1357 conflines = replace_guc_value(conflines, "timezone",
1358 default_timezone, false);
1359 conflines = replace_guc_value(conflines, "log_timezone",
1360 default_timezone, false);
1361 }
1362
1363 conflines = replace_guc_value(conflines, "dynamic_shared_memory_type",
1365
1366 /* Caution: these depend on wal_segment_size_mb, they're not constants */
1367 conflines = replace_guc_value(conflines, "min_wal_size",
1369
1370 conflines = replace_guc_value(conflines, "max_wal_size",
1372
1373 /*
1374 * Fix up various entries to match the true compile-time defaults. Since
1375 * these are indeed defaults, keep the postgresql.conf lines commented.
1376 */
1377 conflines = replace_guc_value(conflines, "unix_socket_directories",
1378 DEFAULT_PGSOCKET_DIR, true);
1379
1380 conflines = replace_guc_value(conflines, "port",
1381 DEF_PGPORT_STR, true);
1382
1383#if DEFAULT_BACKEND_FLUSH_AFTER > 0
1384 snprintf(repltok, sizeof(repltok), "%dkB",
1385 DEFAULT_BACKEND_FLUSH_AFTER * (BLCKSZ / 1024));
1386 conflines = replace_guc_value(conflines, "backend_flush_after",
1387 repltok, true);
1388#endif
1389
1390#if DEFAULT_BGWRITER_FLUSH_AFTER > 0
1391 snprintf(repltok, sizeof(repltok), "%dkB",
1392 DEFAULT_BGWRITER_FLUSH_AFTER * (BLCKSZ / 1024));
1393 conflines = replace_guc_value(conflines, "bgwriter_flush_after",
1394 repltok, true);
1395#endif
1396
1397#if DEFAULT_CHECKPOINT_FLUSH_AFTER > 0
1398 snprintf(repltok, sizeof(repltok), "%dkB",
1399 DEFAULT_CHECKPOINT_FLUSH_AFTER * (BLCKSZ / 1024));
1400 conflines = replace_guc_value(conflines, "checkpoint_flush_after",
1401 repltok, true);
1402#endif
1403
1404#ifndef USE_PREFETCH
1405 conflines = replace_guc_value(conflines, "effective_io_concurrency",
1406 "0", true);
1407#endif
1408
1409#ifdef WIN32
1410 conflines = replace_guc_value(conflines, "update_process_title",
1411 "off", true);
1412#endif
1413
1414 /*
1415 * Change password_encryption setting to md5 if md5 was chosen as an
1416 * authentication method, unless scram-sha-256 was also chosen.
1417 */
1418 if ((strcmp(authmethodlocal, "md5") == 0 &&
1419 strcmp(authmethodhost, "scram-sha-256") != 0) ||
1420 (strcmp(authmethodhost, "md5") == 0 &&
1421 strcmp(authmethodlocal, "scram-sha-256") != 0))
1422 {
1423 conflines = replace_guc_value(conflines, "password_encryption",
1424 "md5", false);
1425 }
1426
1427 /*
1428 * If group access has been enabled for the cluster then it makes sense to
1429 * ensure that the log files also allow group access. Otherwise a backup
1430 * from a user in the group would fail if the log files were not
1431 * relocated.
1432 */
1434 {
1435 conflines = replace_guc_value(conflines, "log_file_mode",
1436 "0640", false);
1437 }
1438
1439 /*
1440 * Now replace anything that's overridden via -c switches.
1441 */
1442 for (gnames = extra_guc_names, gvalues = extra_guc_values;
1443 gnames != NULL; /* assume lists have the same length */
1444 gnames = gnames->next, gvalues = gvalues->next)
1445 {
1446 conflines = replace_guc_value(conflines, gnames->str,
1447 gvalues->str, false);
1448 }
1449
1450 /* ... and write out the finished postgresql.conf file */
1451 snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1452
1453 writefile(path, conflines);
1454 if (chmod(path, pg_file_create_mode) != 0)
1455 pg_fatal("could not change permissions of \"%s\": %m", path);
1456
1457
1458 /* postgresql.auto.conf */
1459
1460 conflines = pg_malloc_array(char *, 3);
1461 conflines[0] = pg_strdup("# Do not edit this file manually!\n");
1462 conflines[1] = pg_strdup("# It will be overwritten by the ALTER SYSTEM command.\n");
1463 conflines[2] = NULL;
1464
1465 sprintf(path, "%s/postgresql.auto.conf", pg_data);
1466
1467 writefile(path, conflines);
1468 if (chmod(path, pg_file_create_mode) != 0)
1469 pg_fatal("could not change permissions of \"%s\": %m", path);
1470
1471
1472 /* pg_hba.conf */
1473
1474 conflines = readfile(hba_file);
1475
1476 conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
1477
1478
1479 /*
1480 * Probe to see if there is really any platform support for IPv6, and
1481 * comment out the relevant pg_hba line if not. This avoids runtime
1482 * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1483 * useful on Windows, where executables built on a machine with IPv6 may
1484 * have to run on a machine without.
1485 */
1486 {
1487 struct addrinfo *gai_result;
1488 struct addrinfo hints;
1489 int err = 0;
1490
1491#ifdef WIN32
1492 /* need to call WSAStartup before calling getaddrinfo */
1493 WSADATA wsaData;
1494
1495 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1496#endif
1497
1498 /* for best results, this code should match parse_hba_line() */
1499 hints.ai_flags = AI_NUMERICHOST;
1500 hints.ai_family = AF_UNSPEC;
1501 hints.ai_socktype = 0;
1502 hints.ai_protocol = 0;
1503 hints.ai_addrlen = 0;
1504 hints.ai_canonname = NULL;
1505 hints.ai_addr = NULL;
1506 hints.ai_next = NULL;
1507
1508 if (err != 0 ||
1509 getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1510 {
1511 conflines = replace_token(conflines,
1512 "host all all ::1",
1513 "#host all all ::1");
1514 conflines = replace_token(conflines,
1515 "host replication all ::1",
1516 "#host replication all ::1");
1517 }
1518 }
1519
1520 /* Replace default authentication methods */
1521 conflines = replace_token(conflines,
1522 "@authmethodhost@",
1524 conflines = replace_token(conflines,
1525 "@authmethodlocal@",
1527
1528 conflines = replace_token(conflines,
1529 "@authcomment@",
1530 (strcmp(authmethodlocal, "trust") == 0 || strcmp(authmethodhost, "trust") == 0) ? AUTHTRUST_WARNING : "");
1531
1532 snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1533
1534 writefile(path, conflines);
1535 if (chmod(path, pg_file_create_mode) != 0)
1536 pg_fatal("could not change permissions of \"%s\": %m", path);
1537
1538
1539 /* pg_ident.conf */
1540
1541 conflines = readfile(ident_file);
1542
1543 snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1544
1545 writefile(path, conflines);
1546 if (chmod(path, pg_file_create_mode) != 0)
1547 pg_fatal("could not change permissions of \"%s\": %m", path);
1548
1549 check_ok();
1550}
void err(int eval, const char *fmt,...)
Definition: err.c:43
#define pg_malloc_array(type, count)
Definition: fe_memutils.h:56
int pg_file_create_mode
Definition: file_perm.c:19
static const char * default_timezone
Definition: initdb.c:202
static char * hba_file
Definition: initdb.c:177
static int n_connections
Definition: initdb.c:198
static int n_buffers
Definition: initdb.c:200
static char * ident_file
Definition: initdb.c:178
static int locale_date_order(const char *locale)
Definition: initdb.c:2155
static int n_av_slots
Definition: initdb.c:199
static char * pretty_wal_size(int segment_count)
Definition: initdb.c:1273
static char ** replace_guc_value(char **lines, const char *guc_name, const char *guc_value, bool mark_as_comment)
Definition: initdb.c:527
static const char * dynamic_shared_memory_type
Definition: initdb.c:201
#define AUTHTRUST_WARNING
Definition: initdb.c:207
static void writefile(char *path, char **lines)
Definition: initdb.c:722
#define DEFAULT_PGSOCKET_DIR
#define DEFAULT_BACKEND_FLUSH_AFTER
#define DEFAULT_CHECKPOINT_FLUSH_AFTER
#define DEFAULT_BGWRITER_FLUSH_AFTER
#define DEFAULT_MAX_WAL_SEGS
Definition: xlog_internal.h:92
#define DEFAULT_MIN_WAL_SEGS
Definition: xlog_internal.h:91

References _, authmethodhost, authmethodlocal, AUTHTRUST_WARNING, check_ok(), conf_file, DATEORDER_DMY, DATEORDER_MDY, DATEORDER_YMD, DEFAULT_BACKEND_FLUSH_AFTER, DEFAULT_BGWRITER_FLUSH_AFTER, DEFAULT_CHECKPOINT_FLUSH_AFTER, DEFAULT_MAX_WAL_SEGS, DEFAULT_MIN_WAL_SEGS, DEFAULT_PGSOCKET_DIR, default_text_search_config, default_timezone, dynamic_shared_memory_type, err(), extra_guc_names, extra_guc_values, hba_file, ident_file, lc_messages, lc_monetary, lc_numeric, lc_time, locale_date_order(), MAXPGPATH, n_av_slots, n_buffers, n_connections, _stringlist::next, pg_data, pg_dir_create_mode, PG_DIR_MODE_GROUP, pg_fatal, pg_file_create_mode, pg_malloc_array, pg_strdup(), pretty_wal_size(), readfile(), replace_guc_value(), replace_token(), snprintf, sprintf, generate_unaccent_rules::stdout, _stringlist::str, and writefile().

Referenced by initialize_data_directory().

◆ setup_data_file_paths()

void setup_data_file_paths ( void  )

Definition at line 2803 of file initdb.c.

2804{
2805 set_input(&bki_file, "postgres.bki");
2806 set_input(&hba_file, "pg_hba.conf.sample");
2807 set_input(&ident_file, "pg_ident.conf.sample");
2808 set_input(&conf_file, "postgresql.conf.sample");
2809 set_input(&dictionary_file, "snowball_create.sql");
2810 set_input(&info_schema_file, "information_schema.sql");
2811 set_input(&features_file, "sql_features.txt");
2812 set_input(&system_constraints_file, "system_constraints.sql");
2813 set_input(&system_functions_file, "system_functions.sql");
2814 set_input(&system_views_file, "system_views.sql");
2815
2816 if (show_setting || debug)
2817 {
2818 fprintf(stderr,
2819 "VERSION=%s\n"
2820 "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2821 "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2822 "POSTGRESQL_CONF_SAMPLE=%s\n"
2823 "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\n",
2824 PG_VERSION,
2827 conf_file,
2829 if (show_setting)
2830 exit(0);
2831 }
2832
2843}
static char * features_file
Definition: initdb.c:182
static void set_input(char **dest, const char *filename)
Definition: initdb.c:983
static void check_input(char *path)
Definition: initdb.c:992
static char * info_schema_file
Definition: initdb.c:181

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

Referenced by main().

◆ setup_depend()

static void setup_depend ( FILE *  cmdfd)
static

Definition at line 1728 of file initdb.c.

1729{
1730 /*
1731 * Advance the OID counter so that subsequently-created objects aren't
1732 * pinned.
1733 */
1734 PG_CMD_PUTS("SELECT pg_stop_making_pinned_objects();\n\n");
1735}

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ setup_description()

static void setup_description ( FILE *  cmdfd)
static

Definition at line 1762 of file initdb.c.

1763{
1764 /* Create default descriptions for operator implementation functions */
1765 PG_CMD_PUTS("WITH funcdescs AS ( "
1766 "SELECT p.oid as p_oid, o.oid as o_oid, oprname "
1767 "FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
1768 "INSERT INTO pg_description "
1769 " SELECT p_oid, 'pg_proc'::regclass, 0, "
1770 " 'implementation of ' || oprname || ' operator' "
1771 " FROM funcdescs "
1772 " WHERE NOT EXISTS (SELECT 1 FROM pg_description "
1773 " WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass) "
1774 " AND NOT EXISTS (SELECT 1 FROM pg_description "
1775 " WHERE objoid = o_oid AND classoid = 'pg_operator'::regclass"
1776 " AND description LIKE 'deprecated%');\n\n");
1777}

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ setup_locale_encoding()

void setup_locale_encoding ( void  )

Definition at line 2696 of file initdb.c.

2697{
2698 setlocales();
2699
2700 if (locale_provider == COLLPROVIDER_LIBC &&
2701 strcmp(lc_ctype, lc_collate) == 0 &&
2702 strcmp(lc_ctype, lc_time) == 0 &&
2703 strcmp(lc_ctype, lc_numeric) == 0 &&
2704 strcmp(lc_ctype, lc_monetary) == 0 &&
2705 strcmp(lc_ctype, lc_messages) == 0 &&
2706 (!datlocale || strcmp(lc_ctype, datlocale) == 0))
2707 printf(_("The database cluster will be initialized with locale \"%s\".\n"), lc_ctype);
2708 else
2709 {
2710 printf(_("The database cluster will be initialized with this locale configuration:\n"));
2711 printf(_(" locale provider: %s\n"), collprovider_name(locale_provider));
2712 if (locale_provider != COLLPROVIDER_LIBC)
2713 printf(_(" default collation: %s\n"), datlocale);
2714 printf(_(" LC_COLLATE: %s\n"
2715 " LC_CTYPE: %s\n"
2716 " LC_MESSAGES: %s\n"
2717 " LC_MONETARY: %s\n"
2718 " LC_NUMERIC: %s\n"
2719 " LC_TIME: %s\n"),
2720 lc_collate,
2721 lc_ctype,
2724 lc_numeric,
2725 lc_time);
2726 }
2727
2728 if (!encoding)
2729 {
2730 int ctype_enc;
2731
2732 ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);
2733
2734 /*
2735 * If ctype_enc=SQL_ASCII, it's compatible with any encoding. ICU does
2736 * not support SQL_ASCII, so select UTF-8 instead.
2737 */
2738 if (locale_provider == COLLPROVIDER_ICU && ctype_enc == PG_SQL_ASCII)
2739 ctype_enc = PG_UTF8;
2740
2741 if (ctype_enc == -1)
2742 {
2743 /* Couldn't recognize the locale's codeset */
2744 pg_log_error("could not find suitable encoding for locale \"%s\"",
2745 lc_ctype);
2746 pg_log_error_hint("Rerun %s with the -E option.", progname);
2747 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2748 exit(1);
2749 }
2750 else if (!pg_valid_server_encoding_id(ctype_enc))
2751 {
2752 /*
2753 * We recognized it, but it's not a legal server encoding. On
2754 * Windows, UTF-8 works with any locale, so we can fall back to
2755 * UTF-8.
2756 */
2757#ifdef WIN32
2759 printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
2760 "The default database encoding will be set to \"%s\" instead.\n"),
2761 pg_encoding_to_char(ctype_enc),
2763#else
2764 pg_log_error("locale \"%s\" requires unsupported encoding \"%s\"",
2765 lc_ctype, pg_encoding_to_char(ctype_enc));
2766 pg_log_error_detail("Encoding \"%s\" is not allowed as a server-side encoding.",
2767 pg_encoding_to_char(ctype_enc));
2768 pg_log_error_hint("Rerun %s with a different locale selection.",
2769 progname);
2770 exit(1);
2771#endif
2772 }
2773 else
2774 {
2775 encodingid = ctype_enc;
2776 printf(_("The default database encoding has accordingly been set to \"%s\".\n"),
2778 }
2779 }
2780 else
2782
2785 exit(1); /* check_locale_encoding printed the error */
2786
2787 if (locale_provider == COLLPROVIDER_BUILTIN)
2788 {
2789 if ((strcmp(datlocale, "C.UTF-8") == 0 ||
2790 strcmp(datlocale, "PG_UNICODE_FAST") == 0) &&
2792 pg_fatal("builtin provider locale \"%s\" requires encoding \"%s\"",
2793 datlocale, "UTF-8");
2794 }
2795
2796 if (locale_provider == COLLPROVIDER_ICU &&
2798 exit(1);
2799}
static bool check_icu_locale_encoding(int user_enc)
Definition: initdb.c:2312
static int get_encoding_id(const char *encoding_name)
Definition: initdb.c:845
static bool check_locale_encoding(const char *locale, int user_enc)
Definition: initdb.c:2277
static void setlocales(void)
Definition: initdb.c:2436
#define pg_valid_server_encoding_id
Definition: pg_wchar.h:632

References _, check_icu_locale_encoding(), check_locale_encoding(), datlocale, encoding, encodingid, get_encoding_id(), lc_collate, lc_ctype, lc_messages, lc_monetary, lc_numeric, lc_time, locale_provider, pg_encoding_to_char, pg_fatal, pg_get_encoding_from_locale(), pg_log_error, pg_log_error_detail, pg_log_error_hint, PG_SQL_ASCII, PG_UTF8, pg_valid_server_encoding_id, printf, progname, and setlocales().

Referenced by main().

◆ setup_pgdata()

void setup_pgdata ( void  )

Definition at line 2622 of file initdb.c.

2623{
2624 char *pgdata_get_env;
2625
2626 if (!pg_data)
2627 {
2628 pgdata_get_env = getenv("PGDATA");
2629 if (pgdata_get_env && strlen(pgdata_get_env))
2630 {
2631 /* PGDATA found */
2632 pg_data = pg_strdup(pgdata_get_env);
2633 }
2634 else
2635 {
2636 pg_log_error("no data directory specified");
2637 pg_log_error_hint("You must identify the directory where the data for this database system "
2638 "will reside. Do this with either the invocation option -D or the "
2639 "environment variable PGDATA.");
2640 exit(1);
2641 }
2642 }
2643
2646
2647 /*
2648 * we have to set PGDATA for postgres rather than pass it on the command
2649 * line to avoid dumb quoting problems on Windows, and we would especially
2650 * need quotes otherwise on Windows because paths there are most likely to
2651 * have embedded spaces.
2652 */
2653 if (setenv("PGDATA", pg_data, 1) != 0)
2654 pg_fatal("could not set environment");
2655}
#define setenv(x, y, z)
Definition: win32_port.h:545

References canonicalize_path(), pg_data, pg_fatal, pg_log_error, pg_log_error_hint, pg_strdup(), pgdata_native, and setenv.

Referenced by main().

◆ setup_privileges()

static void setup_privileges ( FILE *  cmdfd)
static

Definition at line 1816 of file initdb.c.

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

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

Referenced by initialize_data_directory().

◆ setup_run_file()

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

Definition at line 1741 of file initdb.c.

1742{
1743 char **lines;
1744
1745 lines = readfile(filename);
1746
1747 for (char **line = lines; *line != NULL; line++)
1748 {
1749 PG_CMD_PUTS(*line);
1750 free(*line);
1751 }
1752
1753 PG_CMD_PUTS("\n\n");
1754
1755 free(lines);
1756}

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

Referenced by initialize_data_directory(), and setup_schema().

◆ setup_schema()

static void setup_schema ( FILE *  cmdfd)
static

Definition at line 1984 of file initdb.c.

1985{
1987
1988 PG_CMD_PRINTF("UPDATE information_schema.sql_implementation_info "
1989 " SET character_value = '%s' "
1990 " WHERE implementation_info_name = 'DBMS VERSION';\n\n",
1991 infoversion);
1992
1993 PG_CMD_PRINTF("COPY information_schema.sql_features "
1994 " (feature_id, feature_name, sub_feature_id, "
1995 " sub_feature_name, is_supported, comments) "
1996 " FROM E'%s';\n\n",
1998}

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

Referenced by initialize_data_directory().

◆ setup_signals()

void setup_signals ( void  )

Definition at line 2881 of file initdb.c.

2882{
2883 pqsignal(SIGINT, trapsig);
2884 pqsignal(SIGTERM, trapsig);
2885
2886 /* the following are not valid on Windows */
2887#ifndef WIN32
2890
2891 /* Ignore SIGPIPE when writing to backend, so we can clean up */
2892 pqsignal(SIGPIPE, SIG_IGN);
2893
2894 /* Prevent SIGSYS so we can probe for kernel calls that might not work */
2895 pqsignal(SIGSYS, SIG_IGN);
2896#endif
2897}
static void trapsig(SIGNAL_ARGS)
Definition: initdb.c:2110
#define pqsignal
Definition: port.h:521
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define SIGQUIT
Definition: win32_port.h:159

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

Referenced by initialize_data_directory().

◆ setup_text_search()

void setup_text_search ( void  )

Definition at line 2847 of file initdb.c.

2848{
2850 {
2853 {
2854 pg_log_info("could not find suitable text search configuration for locale \"%s\"",
2855 lc_ctype);
2856 default_text_search_config = "simple";
2857 }
2858 }
2859 else
2860 {
2861 const char *checkmatch = find_matching_ts_config(lc_ctype);
2862
2863 if (checkmatch == NULL)
2864 {
2865 pg_log_warning("suitable text search configuration for locale \"%s\" is unknown",
2866 lc_ctype);
2867 }
2868 else if (strcmp(checkmatch, default_text_search_config) != 0)
2869 {
2870 pg_log_warning("specified text search configuration \"%s\" might not match locale \"%s\"",
2872 }
2873 }
2874
2875 printf(_("The default text search configuration will be set to \"%s\".\n"),
2877}
static const char * find_matching_ts_config(const char *lc_type)
Definition: initdb.c:937

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

Referenced by main().

◆ test_config_settings()

static void test_config_settings ( void  )
static

Definition at line 1117 of file initdb.c.

1118{
1119 /*
1120 * This macro defines the minimum shared_buffers we want for a given
1121 * max_connections value. The arrays show the settings to try.
1122 */
1123#define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
1124
1125 /*
1126 * This macro defines the default value of autovacuum_worker_slots we want
1127 * for a given max_connections value. Note that it has been carefully
1128 * crafted to provide specific values for the associated values in
1129 * trial_conns. We want it to return autovacuum_worker_slots's initial
1130 * default value (16) for the maximum value in trial_conns (100), and we
1131 * want it to return close to the minimum value we'd consider (3, which is
1132 * the default of autovacuum_max_workers) for the minimum value in
1133 * trial_conns (25).
1134 */
1135#define AV_SLOTS_FOR_CONNS(nconns) ((nconns) / 6)
1136
1137 static const int trial_conns[] = {
1138 100, 50, 40, 30, 25
1139 };
1140 static const int trial_bufs[] = {
1141 16384, 8192, 4096, 3584, 3072, 2560, 2048, 1536,
1142 1000, 900, 800, 700, 600, 500,
1143 400, 300, 200, 100, 50
1144 };
1145
1146 const int connslen = sizeof(trial_conns) / sizeof(int);
1147 const int bufslen = sizeof(trial_bufs) / sizeof(int);
1148 int i,
1149 test_conns,
1150 test_buffs,
1151 ok_buffers = 0;
1152
1153 /*
1154 * Need to determine working DSM implementation first so that subsequent
1155 * tests don't fail because DSM setting doesn't work.
1156 */
1157 printf(_("selecting dynamic shared memory implementation ... "));
1158 fflush(stdout);
1161
1162 /*
1163 * Probe for max_connections before shared_buffers, since it is subject to
1164 * more constraints than shared_buffers. We also choose the default
1165 * autovacuum_worker_slots here.
1166 */
1167 printf(_("selecting default \"max_connections\" ... "));
1168 fflush(stdout);
1169
1170 for (i = 0; i < connslen; i++)
1171 {
1172 test_conns = trial_conns[i];
1173 n_av_slots = AV_SLOTS_FOR_CONNS(test_conns);
1174 test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
1175
1176 if (test_specific_config_settings(test_conns, n_av_slots, test_buffs))
1177 {
1178 ok_buffers = test_buffs;
1179 break;
1180 }
1181 }
1182 if (i >= connslen)
1183 i = connslen - 1;
1184 n_connections = trial_conns[i];
1185
1186 printf("%d\n", n_connections);
1187
1188 /*
1189 * We chose the default for autovacuum_worker_slots during the
1190 * max_connections tests above, but we print a progress message anyway.
1191 */
1192 printf(_("selecting default \"autovacuum_worker_slots\" ... %d\n"),
1193 n_av_slots);
1194
1195 printf(_("selecting default \"shared_buffers\" ... "));
1196 fflush(stdout);
1197
1198 for (i = 0; i < bufslen; i++)
1199 {
1200 /* Use same amount of memory, independent of BLCKSZ */
1201 test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
1202 if (test_buffs <= ok_buffers)
1203 {
1204 test_buffs = ok_buffers;
1205 break;
1206 }
1207
1209 break;
1210 }
1211 n_buffers = test_buffs;
1212
1213 if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1214 printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
1215 else
1216 printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
1217
1218 printf(_("selecting default time zone ... "));
1219 fflush(stdout);
1221 printf("%s\n", default_timezone ? default_timezone : "GMT");
1222}
static bool test_specific_config_settings(int test_conns, int test_av_slots, int test_buffs)
Definition: initdb.c:1228
#define MIN_BUFS_FOR_CONNS(nconns)
#define AV_SLOTS_FOR_CONNS(nconns)
static const char * choose_dsm_implementation(void)
Definition: initdb.c:1075
const char * select_default_timezone(const char *share_path)

References _, AV_SLOTS_FOR_CONNS, choose_dsm_implementation(), default_timezone, dynamic_shared_memory_type, i, MIN_BUFS_FOR_CONNS, n_av_slots, n_buffers, n_connections, printf, select_default_timezone(), share_path, generate_unaccent_rules::stdout, and test_specific_config_settings().

Referenced by initialize_data_directory().

◆ test_specific_config_settings()

static bool test_specific_config_settings ( int  test_conns,
int  test_av_slots,
int  test_buffs 
)
static

Definition at line 1228 of file initdb.c.

1229{
1230 PQExpBufferData cmd;
1231 _stringlist *gnames,
1232 *gvalues;
1233 int status;
1234
1235 initPQExpBuffer(&cmd);
1236
1237 /* Set up the test postmaster invocation */
1238 printfPQExpBuffer(&cmd,
1239 "\"%s\" --check %s %s "
1240 "-c max_connections=%d "
1241 "-c autovacuum_worker_slots=%d "
1242 "-c shared_buffers=%d "
1243 "-c dynamic_shared_memory_type=%s",
1245 test_conns, test_av_slots, test_buffs,
1247
1248 /* Add any user-given setting overrides */
1249 for (gnames = extra_guc_names, gvalues = extra_guc_values;
1250 gnames != NULL; /* assume lists have the same length */
1251 gnames = gnames->next, gvalues = gvalues->next)
1252 {
1253 appendPQExpBuffer(&cmd, " -c %s=", gnames->str);
1254 appendShellString(&cmd, gvalues->str);
1255 }
1256
1257 appendPQExpBuffer(&cmd,
1258 " < \"%s\" > \"%s\" 2>&1",
1259 DEVNULL, DEVNULL);
1260
1261 fflush(NULL);
1262 status = system(cmd.data);
1263
1264 termPQExpBuffer(&cmd);
1265
1266 return (status == 0);
1267}

References appendPQExpBuffer(), appendShellString(), backend_exec, boot_options, PQExpBufferData::data, DEVNULL, dynamic_shared_memory_type, extra_guc_names, extra_guc_values, extra_options, initPQExpBuffer(), _stringlist::next, printfPQExpBuffer(), _stringlist::str, and termPQExpBuffer().

Referenced by test_config_settings().

◆ trapsig()

static void trapsig ( SIGNAL_ARGS  )
static

Definition at line 2110 of file initdb.c.

2111{
2112 /* handle systems that reset the handler, like Windows (grr) */
2113 pqsignal(postgres_signal_arg, trapsig);
2114 caught_signal = true;
2115}

References caught_signal, pqsignal, and trapsig().

Referenced by setup_signals(), and trapsig().

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 2529 of file initdb.c.

2530{
2531 printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2532 printf(_("Usage:\n"));
2533 printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2534 printf(_("\nOptions:\n"));
2535 printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
2536 printf(_(" --auth-host=METHOD default authentication method for local TCP/IP connections\n"));
2537 printf(_(" --auth-local=METHOD default authentication method for local-socket connections\n"));
2538 printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2539 printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2540 printf(_(" -g, --allow-group-access allow group read/execute on data directory\n"));
2541 printf(_(" --icu-locale=LOCALE set ICU locale ID for new databases\n"));
2542 printf(_(" --icu-rules=RULES set additional ICU collation rules for new databases\n"));
2543 printf(_(" -k, --data-checksums use data page checksums\n"));
2544 printf(_(" --locale=LOCALE set default locale for new databases\n"));
2545 printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2546 " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2547 " set default locale in the respective category for\n"
2548 " new databases (default taken from environment)\n"));
2549 printf(_(" --no-locale equivalent to --locale=C\n"));
2550 printf(_(" --builtin-locale=LOCALE\n"
2551 " set builtin locale name for new databases\n"));
2552 printf(_(" --locale-provider={builtin|libc|icu}\n"
2553 " set default locale provider for new databases\n"));
2554 printf(_(" --no-data-checksums do not use data page checksums\n"));
2555 printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
2556 printf(_(" -T, --text-search-config=CFG\n"
2557 " default text search configuration\n"));
2558 printf(_(" -U, --username=NAME database superuser name\n"));
2559 printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
2560 printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
2561 printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
2562 printf(_("\nLess commonly used options:\n"));
2563 printf(_(" -c, --set NAME=VALUE override default setting for server parameter\n"));
2564 printf(_(" -d, --debug generate lots of debugging output\n"));
2565 printf(_(" --discard-caches set debug_discard_caches=1\n"));
2566 printf(_(" -L DIRECTORY where to find the input files\n"));
2567 printf(_(" -n, --no-clean do not clean up after errors\n"));
2568 printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
2569 printf(_(" --no-instructions do not print instructions for next steps\n"));
2570 printf(_(" -s, --show show internal settings, then exit\n"));
2571 printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
2572 printf(_(" -S, --sync-only only sync database files to disk, then exit\n"));
2573 printf(_("\nOther options:\n"));
2574 printf(_(" -V, --version output version information, then exit\n"));
2575 printf(_(" -?, --help show this help, then exit\n"));
2576 printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2577 "is used.\n"));
2578 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2579 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
2580}

References _, printf, and progname.

Referenced by main().

◆ vacuum_db()

static void vacuum_db ( FILE *  cmdfd)
static

Definition at line 2013 of file initdb.c.

2014{
2015 /* Run analyze before VACUUM so the statistics are frozen. */
2016 PG_CMD_PUTS("ANALYZE;\n\nVACUUM FREEZE;\n\n");
2017}

References PG_CMD_PUTS.

Referenced by initialize_data_directory().

◆ warn_on_mount_point()

void warn_on_mount_point ( int  error)

Definition at line 3042 of file initdb.c.

3043{
3044 if (error == 2)
3045 pg_log_error_detail("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.");
3046 else if (error == 3)
3047 pg_log_error_detail("It contains a lost+found directory, perhaps due to it being a mount point.");
3048
3049 pg_log_error_hint("Using a mount point directly as the data directory is not recommended.\n"
3050 "Create a subdirectory under the mount point.");
3051}
static void error(void)
Definition: sql-dyntest.c:147

References error(), pg_log_error_detail, and pg_log_error_hint.

Referenced by create_data_directory(), and create_xlog_or_symlink().

◆ write_version_file()

static void write_version_file ( const char *  extrapath)
static

Definition at line 1023 of file initdb.c.

1024{
1025 FILE *version_file;
1026 char *path;
1027
1028 if (extrapath == NULL)
1029 path = psprintf("%s/PG_VERSION", pg_data);
1030 else
1031 path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
1032
1033 if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
1034 pg_fatal("could not open file \"%s\" for writing: %m", path);
1035 if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
1036 fclose(version_file))
1037 pg_fatal("could not write file \"%s\": %m", path);
1038 free(path);
1039}
static char version_file[MAXPGPATH]
Definition: pg_ctl.c:98

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

Referenced by initialize_data_directory().

◆ writefile()

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

Definition at line 722 of file initdb.c.

723{
724 FILE *out_file;
725 char **line;
726
727 if ((out_file = fopen(path, "w")) == NULL)
728 pg_fatal("could not open file \"%s\" for writing: %m", path);
729 for (line = lines; *line != NULL; line++)
730 {
731 if (fputs(*line, out_file) < 0)
732 pg_fatal("could not write file \"%s\": %m", path);
733 free(*line);
734 }
735 if (fclose(out_file))
736 pg_fatal("could not close file \"%s\": %m", path);
737 free(lines);
738}

References free, and pg_fatal.

Referenced by setup_config().

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 96 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 118 of file initdb.c.

Referenced by main().

◆ authmethodhost

const char* authmethodhost = NULL
static

Definition at line 157 of file initdb.c.

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

◆ authmethodlocal

const char* authmethodlocal = NULL
static

Definition at line 158 of file initdb.c.

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

◆ authwarning

bool authwarning = false
static

Definition at line 212 of file initdb.c.

Referenced by check_authmethod_unspecified(), and main().

◆ backend_exec

char backend_exec[MAXPGPATH]
static

◆ backend_options

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

Definition at line 225 of file initdb.c.

Referenced by initialize_data_directory().

◆ bin_path

char bin_path[MAXPGPATH]
static

Definition at line 258 of file initdb.c.

Referenced by make_relative_path(), setup_bin_paths(), and setup_data_file_paths().

◆ bki_file

char* bki_file
static

Definition at line 176 of file initdb.c.

Referenced by bootstrap_template1(), and setup_data_file_paths().

◆ boot_options

const char* const boot_options = "-F -c log_checkpoints=false"
static

Definition at line 224 of file initdb.c.

Referenced by bootstrap_template1(), and test_specific_config_settings().

◆ builtin_locale_specified

bool builtin_locale_specified = false
static

Definition at line 148 of file initdb.c.

Referenced by main().

◆ caught_signal

bool caught_signal = false
static

Definition at line 192 of file initdb.c.

Referenced by check_ok(), and trapsig().

◆ conf_file

char* conf_file
static

Definition at line 179 of file initdb.c.

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

◆ data_checksums

bool data_checksums = true
static

Definition at line 167 of file initdb.c.

Referenced by bootstrap_template1(), and main().

◆ datlocale

char* datlocale = NULL
static

Definition at line 149 of file initdb.c.

Referenced by bootstrap_template1(), main(), setlocales(), and setup_locale_encoding().

◆ debug

◆ default_text_search_config

const char* default_text_search_config = NULL
static

Definition at line 152 of file initdb.c.

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

◆ default_timezone

const char* default_timezone = NULL
static

Definition at line 202 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ dictionary_file

char* dictionary_file
static

Definition at line 180 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ do_sync

bool do_sync = true
static

Definition at line 164 of file initdb.c.

Referenced by main(), sync_target_dir(), and update_controlfile().

◆ dynamic_shared_memory_type

const char* dynamic_shared_memory_type = NULL
static

Definition at line 201 of file initdb.c.

Referenced by setup_config(), test_config_settings(), and test_specific_config_settings().

◆ encoding

char* encoding = NULL
static

Definition at line 139 of file initdb.c.

Referenced by main(), and setup_locale_encoding().

◆ encodingid

int encodingid
static

Definition at line 175 of file initdb.c.

Referenced by bootstrap_template1(), and setup_locale_encoding().

◆ extra_guc_names

_stringlist* extra_guc_names = NULL
static

Definition at line 159 of file initdb.c.

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

◆ extra_guc_values

_stringlist* extra_guc_values = NULL
static

Definition at line 160 of file initdb.c.

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

◆ extra_options

char* extra_options = ""
static

◆ features_file

char* features_file
static

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

Referenced by cleanup_directories_atexit(), and create_data_directory().

◆ found_existing_xlogdir

bool found_existing_xlogdir = false
static

Definition at line 190 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_xlog_or_symlink().

◆ hba_file

char* hba_file
static

Definition at line 177 of file initdb.c.

Referenced by setup_config(), and setup_data_file_paths().

◆ icu_locale_specified

bool icu_locale_specified = false
static

Definition at line 150 of file initdb.c.

Referenced by main().

◆ icu_rules

char* icu_rules = NULL
static

Definition at line 151 of file initdb.c.

Referenced by bootstrap_template1(), and main().

◆ ident_file

char* ident_file
static

Definition at line 178 of file initdb.c.

Referenced by setup_config(), and setup_data_file_paths().

◆ info_schema_file

char* info_schema_file
static

Definition at line 181 of file initdb.c.

Referenced by setup_data_file_paths(), and setup_schema().

◆ infoversion

char infoversion[100]
static

Definition at line 191 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 146 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 143 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 144 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 145 of file initdb.c.

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

◆ locale

◆ locale_provider

char locale_provider = COLLPROVIDER_LIBC
static

Definition at line 147 of file initdb.c.

Referenced by bootstrap_template1(), main(), setlocales(), and setup_locale_encoding().

◆ made_new_pgdata

bool made_new_pgdata = false
static

Definition at line 187 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_data_directory().

◆ made_new_xlogdir

bool made_new_xlogdir = false
static

Definition at line 189 of file initdb.c.

Referenced by cleanup_directories_atexit(), and create_xlog_or_symlink().

◆ n_av_slots

int n_av_slots = 16
static

Definition at line 199 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ n_buffers

int n_buffers = 50
static

Definition at line 200 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ n_connections

int n_connections = 10
static

Definition at line 198 of file initdb.c.

Referenced by setup_config(), and test_config_settings().

◆ noclean

bool noclean = false
static

Definition at line 162 of file initdb.c.

Referenced by cleanup_directories_atexit(), and main().

◆ noinstructions

bool noinstructions = false
static

Definition at line 163 of file initdb.c.

Referenced by main().

◆ output_errno

int output_errno = 0
static

Definition at line 194 of file initdb.c.

Referenced by check_ok().

◆ output_failed

bool output_failed = false
static

Definition at line 193 of file initdb.c.

Referenced by check_ok().

◆ pg_data

◆ pgdata_native

char* pgdata_native
static

Definition at line 195 of file initdb.c.

Referenced by main(), and setup_pgdata().

◆ progname

◆ pwfilename

char* pwfilename = NULL
static

Definition at line 155 of file initdb.c.

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

◆ pwprompt

bool pwprompt = false
static

Definition at line 154 of file initdb.c.

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

◆ share_path

char* share_path = NULL
static

◆ show_setting

bool show_setting = false
static

Definition at line 166 of file initdb.c.

Referenced by main(), and setup_data_file_paths().

◆ subdirs

const char* const subdirs[]
static
Initial value:
= {
"global",
"pg_wal/archive_status",
"pg_wal/summaries",
"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 230 of file initdb.c.

Referenced by initialize_data_directory().

◆ success

◆ superuser_password

char* superuser_password = NULL
static

Definition at line 156 of file initdb.c.

Referenced by get_su_pwd(), and setup_auth().

◆ sync_method

Definition at line 170 of file initdb.c.

Referenced by _allocAH(), CreateArchive(), main(), parse_sync_method(), and sync_target_dir().

◆ sync_only

bool sync_only = false
static

Definition at line 165 of file initdb.c.

Referenced by main().

◆ system_constraints_file

char* system_constraints_file
static

Definition at line 183 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ system_functions_file

char* system_functions_file
static

Definition at line 184 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ system_views_file

char* system_views_file
static

Definition at line 185 of file initdb.c.

Referenced by initialize_data_directory(), and setup_data_file_paths().

◆ tsearch_config_languages

const struct tsearch_config_match tsearch_config_languages[]
static

Definition at line 868 of file initdb.c.

Referenced by find_matching_ts_config().

◆ username

char* username = NULL
static

Definition at line 153 of file initdb.c.

Referenced by add_client_identification(), BackgroundWorkerInitializeConnection(), bootstrap_template1(), check_password(), dumpUserConfig(), get_id(), get_object_address_defacl(), get_object_address_usermapping(), get_user_name(), getObjectDescription(), getObjectIdentityParts(), has_any_column_privilege_name_id(), has_column_privilege_name_id_attnum(), has_column_privilege_name_id_name(), has_database_privilege_name_id(), has_database_privilege_name_name(), has_foreign_data_wrapper_privilege_name_id(), has_foreign_data_wrapper_privilege_name_name(), has_function_privilege_name_id(), has_function_privilege_name_name(), has_language_privilege_name_id(), has_language_privilege_name_name(), has_largeobject_privilege_name_id(), has_parameter_privilege_name_name(), has_schema_privilege_name_id(), has_schema_privilege_name_name(), has_sequence_privilege_name_id(), has_server_privilege_name_id(), has_server_privilege_name_name(), has_table_privilege_name_id(), has_tablespace_privilege_name_id(), has_tablespace_privilege_name_name(), has_type_privilege_name_id(), has_type_privilege_name_name(), InitPostgres(), log_status_format(), main(), mock_scram_secret(), passwordFromFile(), pg_fe_getusername(), pg_has_role_name_id(), pg_has_role_name_name(), PostgresMain(), PostgresSingleUserMain(), process_pgfdw_appname(), prompt_for_password(), scram_mock_salt(), scram_verify_plain_password(), setup_auth(), setup_data_file_paths(), and setup_privileges().

◆ wal_segment_size_mb

int wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024)
static

Definition at line 169 of file initdb.c.

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

◆ xlog_dir

char* xlog_dir = NULL
static

Definition at line 168 of file initdb.c.

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