INITDB(1)

πŸ“¦ version 13.23, in opensuse-leap 16.0

πŸ’– INITDB(1) πŸ’–

the initdb glow-up: creating a brand-new PostgreSQL database cluster, bestie edition ✨


🌸 SYNOPSIS 🌸

initdb [option...] [--pgdata | -D] directory

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so picture this: initdb creates a whole new PostgreSQL database cluster. and what even is a database cluster? it's literally just a collection of databases that get managed together by a single server instance. main character energy for your data, honestly πŸ‘‘

creating a database cluster is a whole little ritual: you make the directories where the database data will live, generate the shared catalog tables (those are the tables that belong to the whole cluster rather than to any one particular database), and create the template1 and postgres databases. and here's the tea β€” when you later create a new database, everything in template1 gets copied. so like, anything you install in template1 is automatically copied into every database you create after that. iconic behavior, no notes. and postgres is a default database meant for use by users, utilities and third party applications 🫢

so initdb will try to create the data directory you specify, but it might not have permission if the parent directory of your desired data directory is root-owned. annoying, i know. to initialize in that kind of setup, create an empty data directory as root, then use chown to give ownership of that directory to the database user account, then su to become the database user to run initdb. slay, you got this πŸ’…

and listen β€” initdb must be run as the user that will own the server process, because the server needs access to all the files and directories that initdb creates. and since the server cannot be run as root, you must not run initdb as root either. (it will in fact refuse to do so, like it's literally not gonna let you 😭)

for security reasons, the new cluster created by initdb will only be accessible by the cluster owner by default. the --allow-group-access option lets any user in the same group as the cluster owner read files in the cluster. this is useful for performing backups as a non-privileged user β€” very demure, very mindful 🌸

initdb also initializes the database cluster's default locale and character set encoding. the character set encoding, collation order (LC_COLLATE) and character set classes (LC_CTYPE, e.g., upper, lower, digit) can all be set separately for a database when it is created. initdb determines those settings for the template1 database, which will serve as the default for all other databases.

to alter the default collation order or character set classes, use the --lc-collate and --lc-ctype options. quick heads up though: collation orders other than C or POSIX also have a performance penalty. so for these reasons it is important to choose the right locale when running initdb. choose wisely bestie πŸŽ€

the remaining locale categories can be changed later when the server is started. you can also use --locale to set the default for all locale categories, including collation order and character set classes. all server locale values (lc_*) can be displayed via SHOW ALL. more details can be found in Section 23.1.

to alter the default encoding, use the --encoding. more details can be found in Section 23.3.


✨ OPTIONS ✨

  • -A authmethod, --auth=authmethod β€” this one specifies the default authentication method for local users, used in pg_hba.conf (both host and local lines). initdb will prepopulate pg_hba.conf entries using the specified authentication method for non-replication as well as replication connections.
⚠️ do not use trust unless you trust all local users on your system. trust is the default for ease of installation, but like… be careful bestie πŸ’€
  • --auth-host=authmethod β€” specifies the authentication method for local users via TCP/IP connections, used in pg_hba.conf (host lines).
  • --auth-local=authmethod β€” specifies the authentication method for local users via Unix-domain socket connections, used in pg_hba.conf (local lines).
  • -D directory, --pgdata=directory β€” specifies the directory where the database cluster should be stored. this is the only info initdb actually requires, but you can avoid writing it by setting the PGDATA environment variable, which is lowkey convenient since the database server (postgres) can find the database directory later by the same variable 🎯
  • -E encoding, --encoding=encoding β€” selects the encoding of the template database. this will also be the default encoding of any database you create later, unless you override it there. the default is derived from the locale, or SQL_ASCII if that does not work. the character sets supported by the PostgreSQL server are described in Section 23.3.1.
  • -g, --allow-group-access β€” allows users in the same group as the cluster owner to read all cluster files created by initdb. this option is ignored on Windows since it does not support POSIX-style group permissions.
  • -k, --data-checksums β€” use checksums on data pages to help detect corruption by the I/O system that would otherwise be silent. enabling checksums may incur a noticeable performance penalty. if set, checksums are calculated for all objects, in all databases. all checksum failures will be reported in the pg_stat_database view πŸ”
  • --locale=locale β€” sets the default locale for the database cluster. if this option is not specified, the locale is inherited from the environment that initdb runs in. locale support is described in Section 23.1.
  • --lc-collate=locale, --lc-ctype=locale, --lc-messages=locale, --lc-monetary=locale, --lc-numeric=locale, --lc-time=locale β€” like --locale, but only sets the locale in that specific category. one category at a time, very focused, very intentional πŸ’…
  • --no-locale β€” equivalent to --locale=C.
  • -N, --no-sync β€” by default, initdb will wait for all files to be written safely to disk. this option causes initdb to return without waiting, which is faster but means a subsequent operating system crash can leave the data directory corrupt 😬 generally this option is useful for testing, but should not be used when creating a production installation.
  • --pwfile=filename β€” makes initdb read the database superuser's password from a file. the first line of the file is taken as the password.
  • -S, --sync-only β€” safely write all database files to disk and exit. this does not perform any of the normal initdb operations.
  • -T config, --text-search-config=config β€” sets the default text search configuration. see default_text_search_config for further information.
  • -U username, --username=username β€” selects the user name of the database superuser. this defaults to the name of the effective user running initdb. it is really not important what the superuser's name is, but one might choose to keep the customary name postgres, even if the operating system user's name is different.
  • -W, --pwprompt β€” makes initdb prompt for a password to give the database superuser. if you don't plan on using password authentication, this is not important. otherwise you won't be able to use password authentication until you have a password set up πŸ”
  • -X directory, --waldir=directory β€” specifies the directory where the write-ahead log should be stored.
  • --wal-segsize=size β€” set the WAL segment size, in megabytes. this is the size of each individual file in the WAL log. the default size is 16 megabytes. the value must be a power of 2 between 1 and 1024 (megabytes). this option can only be set during initialization, and cannot be changed later β€” so like, commit to it fr fr.

it may be useful to adjust this size to control the granularity of WAL log shipping or archiving. also, in databases with a high volume of WAL, the sheer number of WAL files per directory can become a performance and management problem. increasing the WAL file size will reduce the number of WAL files. efficiency icon behavior πŸ”₯

🧚 other, less commonly used, options are also available:

  • -d, --debug β€” print debugging output from the bootstrap backend and a few other messages of lesser interest for the general public. the bootstrap backend is the program initdb uses to create the catalog tables. this option generates a tremendous amount of extremely boring output 😴
  • -L directory β€” specifies where initdb should find its input files to initialize the database cluster. this is normally not necessary. you will be told if you need to specify their location explicitly.
  • -n, --no-clean β€” by default, when initdb determines that an error prevented it from completely creating the database cluster, it removes any files it might have created before discovering that it cannot finish the job. this option inhibits the tidying-up and is thus useful for debugging.

πŸŽ€ other options:

  • -V, --version β€” print the initdb version and exit.
  • -?, --help β€” show help about initdb command line arguments, and exit.

🌈 ENVIRONMENT 🌈

  • PGDATA β€” specifies the directory where the database cluster is to be stored; can be overridden using the -D option.
  • PG_COLOR β€” specifies whether to use color in diagnostic messages. possible values are always, auto and never. (aesthetic diagnostics, we love to see it 🎨)
  • TZ β€” specifies the default time zone of the created database cluster. the value should be a full time zone name (see Section 8.5.3). πŸ•°οΈ

πŸ“ NOTES πŸ“

initdb can also be invoked via pg_ctl initdb. cute shortcut, iykyk 🫢


πŸ”— SEE ALSO πŸ”—

pg_ctl(1), postgres(1)


okurrr that's the whole page bestie, you're SO ready to spin up a cluster now πŸ’œ periodt. this slay of a manual page came to you from the PostgreSQL 13.23 documentation era, dated 2025 β€” go forth and initdb responsibly, mwah πŸ’‹πŸŒΈ