INITDB(1)

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

πŸ’– NAME πŸ’–

initdb β€” create a new PostgreSQL database cluster πŸŽ€

okay so this is the INITDB(1) page from the PostgreSQL 16.15 Documentation, and bestie, we're gonna get you fully caught up on what initdb does, no cap. ✨


🌸 SYNOPSIS 🌸

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

πŸ¦‹ DESCRIPTION πŸ¦‹

initdb creates a new PostgreSQL database cluster. That's the whole vibe. 🫢

Creating a database cluster consists of creating the directories in which the cluster data will live, generating the shared catalog tables (tables that belong to the whole cluster rather than to any particular database), and creating the postgres, template1, and template0 databases. Iconic lineup, honestly:

  • postgres β€” a default database meant for use by users, utilities and third party applications. she's the main character πŸ’…
  • template1 and template0 β€” meant as source databases to be copied by later CREATE DATABASE commands.

Lowkey important: template0 should never be modified β€” hands off, bestie 🚫 β€” but you can add objects to template1, which by default will be copied into databases created later. See Section 23.3 for more details. iykyk.

So like, initdb will attempt to create the specified data directory, but it might not have permission if the parent directory of the desired data directory is root-owned 😭. To initialize in such a setup, do the little dance:

  1. create an empty data directory as root
  2. then use chown to assign ownership of that directory to the database user account
  3. then su to become the database user to run initdb

initdb must be run as the user that will own the server process, because the server needs to have access to the files and directories that initdb creates. Since the server cannot be run as root, you must not run initdb as root either. (It will in fact refuse to do so. πŸ’€ it said no.)

For security reasons the new cluster created by initdb will only be accessible by the cluster owner by default. The --allow-group-access option allows any user in the same group as the cluster owner to read files in the cluster. This is useful for performing backups as a non-privileged user. 🧚

initdb initializes the database cluster's default locale and character set encoding. These can also be set separately for each database when it is created. initdb determines those settings for the template databases, which will serve as the default for all other databases. so everyone inherits the template's fit, basically πŸ‘‘

By default, initdb uses the locale provider libc (see Section 24.1.4). The libc locale provider takes the locale settings from the environment, and determines the encoding from the locale settings. environment said what it said 🌈

To choose a different locale for the cluster, use the option --locale. There are also individual options --lc-* and --icu-locale (see below) to set values for the individual locale categories. Note that inconsistent settings for different locale categories can give nonsensical results, so this should be used with care. 🫠 fr fr, don't mix-and-match and then act surprised.

Alternatively, initdb can use the ICU library to provide locale services by specifying --locale-provider=icu. The server must be built with ICU support. To choose the specific ICU locale ID to apply, use the option --icu-locale. Note that for implementation reasons and to support legacy code, initdb will still select and initialize libc locale settings when the ICU locale provider is used. legacy code really said "i'm still here" 😭

When initdb runs, it will print out the locale settings it has chosen. If you have complex requirements or specified multiple options, it is advisable to check that the result matches what was intended. πŸ‘€ like, actually read the output, bestie.

More details about locale settings can be found in Section 24.1. πŸ¦‹

To alter the default encoding, use the --encoding. More details can be found in Section 24.3. ⭐

✨ OPTIONS ✨

okay bestie, here's the whole menu of flags. no skipping, we're going through all of them πŸ’…

  • *-A *authmethod, --auth=*authmethod*** β€” sets the default auth method for local users, the one that goes in pg_hba.conf(on thehostandlocal` lines). valid values are in Section 21.1, go look πŸ‘€

initdb will prepopulate the pg_hba.conf entries with whatever method you picked, for both non-replication and replication connections.

do not use trust unless you genuinely trust every local user on your system, fr. it's the default tho, purely for ease of installation 🫢
  • --auth-host=authmethod β€” this one sets the auth method for local users coming in over TCP/IP connections, i.e. the host lines in pg_hba.conf.
  • --auth-local=authmethod β€” and this one's for local users over Unix-domain socket connections, i.e. the local lines in pg_hba.conf.
  • *-D *directory, --pgdata=*directory*** β€” where the database cluster lives. this is literally the only info initdbrequires 😭 but you can skip writing it by setting thePGDATA environment variable, which is lowkey convenient since the server (postgres`) can find the data directory later using that same variable. iconic teamwork.
  • -E encoding, --encoding=`encoding β€” picks the encoding for the template databases. this also becomes the default encoding for any database you create later, unless you override it at that point. the character sets the PostgreSQL server supports are in Section 24.3.1.

by default, the template database encoding is derived from the locale. if --no-locale is specified (or equivalently, if the locale is C or POSIX), then the default is UTF8 for the ICU provider and SQL_ASCII for the libc provider. slay.

  • -g, --allow-group-access β€” lets users in the same group as the cluster owner read all the cluster files initdb creates. this option just gets ignored on Windows because Windows doesn't do POSIX-style group permissions πŸ’€
  • --icu-locale=locale β€” specifies the ICU locale when the ICU provider is in use. locale support is described in Section 24.1.
  • --icu-rules=rules β€” extra collation rules to customize how the default collation behaves. ICU only, bestie.
  • -k, --data-checksums β€” puts checksums on data pages so you can catch corruption from the I/O system that would otherwise be totally silent πŸ™ˆ but ngl, enabling checksums may cost you a noticeable performance penalty. if set, checksums get calculated for all objects, in all databases. all checksum failures get reported in the pg_stat_database view. details in Section 30.2.
  • --locale=locale β€” sets the default locale for the database cluster. if you don't specify this, the locale is inherited from the environment initdb runs in. locale support is described in Section 24.1.
  • --lc-collate=locale, --lc-ctype=locale, --lc-messages=locale, --lc-monetary=locale, --lc-numeric=locale, --lc-time=locale β€” same energy as --locale, but each one only sets the locale in its specified category ✨
  • --no-locale β€” equivalent to --locale=C. that's it, that's the whole thing.
  • --locale-provider={libc|icu} β€” sets the locale provider for databases created in the new cluster. you can override it later in the CREATE DATABASE command when you create new databases. default is libc (see Section 24.1.4).
  • -N, --no-sync β€” by default initdb waits for all files to be safely written to disk. this option makes it return without waiting, which is faster, but means a later operating system crash can leave the data directory corrupt 😭 generally this one's for testing β€” do not use it when you're making a production installation, okay?
  • --no-instructions β€” by default initdb writes instructions for how to start the cluster at the end of its output. this option leaves those out. it's mainly for tools that wrap initdb in platform-specific behavior, where those instructions would probably be wrong anyway.
  • --pwfile=filename β€” makes initdb read the bootstrap superuser's password from a file. the first line of the file is taken as the password πŸ”‘
  • -S, --sync-only β€” safely writes all the database files to disk and exits. it does none of the normal initdb operations. generally useful for making sure recovery is reliable after flipping fsync from off to on.
  • *-T *config, --text-search-config=*config*** β€” sets the default text search configuration. see default_text_search_config` for more info.
  • *-U *username, --username=*username*** β€” sets the user name of the bootstrap superuser. defaults to the name of the operating-system user running initdb`.
  • -W, --pwprompt β€” makes initdb prompt you for a password for the bootstrap superuser. if you're not planning on using password authentication, no big deal. otherwise you literally won't be able to use password authentication until you've got a password set up πŸ’€
  • -X directory, --waldir=`directory β€” specifies the directory where the write-ahead log should be stored.
  • --wal-segsize=size β€” sets the WAL segment size, in megabytes. that's the size of each individual file in the WAL log. default is 16 megabytes 🌸 the value must be a power of 2 between 1 and 1024 (megabytes). and heads up: this can only be set during initialization β€” it cannot be changed later.

it may be useful to tweak this 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. bumping the WAL file size reduces the number of WAL files. math is math ✨


other, less commonly used, options are also available:

  • *-c *name=value, --set*name*=*value*** β€” forcibly sets the server parameter *name* to *value* duringinitdb, and also installs that setting in the generated postgresql.conf` file so it applies during future server runs. you can give this option more than once to set several parameters. it's mainly useful when the environment is such that the server won't start at all with the default parameters 😭
  • -d, --debug β€” prints debugging output from the bootstrap backend plus a few other messages of lesser interest to the general public. the bootstrap backend is the program initdb uses to create the catalog tables. warning: this option generates a tremendous amount of extremely boring output πŸ’€
  • --discard-caches β€” runs the bootstrap backend with the debug_discard_caches=1 option. this takes a very long time and is only of use for deep debugging. like, deep deep.
  • -L directory β€” specifies where initdb should find its input files to initialize the database cluster. normally not necessary. you'll be told if you need to specify their location explicitly 🫢
  • -n, --no-clean β€” by default, when initdb figures out that an error prevented it from completely creating the database cluster, it removes any files it might have created before discovering it can't finish the job. this option inhibits the tidying-up, which is thus useful for debugging.

other options:

  • -V, --version β€” prints the initdb version and exits.
  • -?, --help β€” shows help about initdb command line arguments, and exits.

🌈 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.
  • 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 πŸ¦‹

fun fact bestie: initdb can also be invoked via pg_ctl initdb πŸ‘‘


πŸŽ€ SEE ALSO πŸŽ€

pg_ctl(1), postgres(1), Section 21.1


and that's the whole page, you're so welcome πŸ’– this lil gem is from PostgreSQL 16.15, dated 2026, and it's the INITDB(1) page β€” go forth and initialize your clusters, main character 🫢✨