initdb

Name

initdb -- Create a new PostgreSQL database cluster

Synopsis

initdb --pgdata | -D directory [--username | -U username] [--pwprompt | -W] [--encoding | -E encoding] [-L directory] [--noclean | -n] [--debug | -d]

Options

--pgdata=dbdir, -D dbdir

This option specifies where in the file system the database should be stored. This is the only information required by initdb, but you can avoid writing it by setting the PGDATA environment variable. This can be convenient as the database server (postmaster) locates the database directory using the same variable.

--sysid=sysid, -i sysid

Selects the system id of the database superuser. This defaults to the effective user id of the user running initdb. It is really not important what the superuser sysid is, but one might choose to start the numbering at some number like 1.

--pwprompt, -W

Makes initdb prompt for a password to give the database superuser. If you do not plan on using password authentication, this is not important. Otherwise you will not be able to use password authentication until you have a password set up.

--encoding=encoding, -E encoding

Selects the multibyte encoding of the template database. This will also be the default encoding for any database you create later, unless you override it.

-L directory

Specifies where initdb should find its input files to initialize the database system. This is normally not necessary. You will be told if you need to specify their location explicitly.

--noclean, -n

By default, when initdb determines that an error prevented it from completely creating the database system, it removes any files it may have created before discovering that it could not finish the job. This option inhibits this tidying-up and is thus useful for debugging.

--debug, -d

Print debugging output from the bootstrap backend and a few other messages of lesser interest to the general public. The bootstrap backend is the program initdb uses to create the catalog tables.

Description

initdb creates a new PostgreSQL database cluster. A database cluster is a collection of databases that are managed by a single postmaster. In SQL terminology, this is known as a catalog cluster.

Creating a database system consists of creating the directories in which the database data will live, generating the shared catalog tables (tables that belong to the whole cluster rather than to any particular database), and creating the template1 database. When you create a new database, everything in the template1 database is copied. template1 contains catalog tables completed for things like the built-in types.

You must not execute initdb as root; it must be run by the Linux user account that will run the database server. This is because you cannot run the database server as root and the server needs to have access to the files initdb creates. Furthermore, during the initialization phase, when there are no users and no access controls installed, PostgreSQL will only connect with the name of the current Linux user, so you must log in under the account that will own the server process.

Although initdb will attempt to create the specified data directory, often it will not have permission to do so, since the parent of the desired data directory is often a root-owned directory. To set up an arrangement like this, create an empty data directory as root, then use chown to hand over ownership of that directory to the database user account, then su to become the database user, and finally run initdb as the database user.