Before you can do anything, you must initialize a database storage
area on disk. We call this a database cluster.
(SQL uses the term catalog cluster instead.) A
database cluster is a collection of databases is accessible by a
single instance of a running database server. After initialization, a
database cluster will contain a database named
template1. As the name suggests, this will be used
as a template for subsequently created databases; it should not be
used for actual work. (See Chapter 5 for information
about creating databases.)
In file system terms, a database cluster will be a single directory
under which all data will be stored. We call this the data
directory or data area. It is
completely up to you where you choose to store your data. There is no
default, although locations such as
/usr/local/pgsql/data or
/var/lib/pgsql/data are popular. To initialize a
database cluster, use the command initdb, which is
installed with PostgreSQL. The desired
file system location of your database system is indicated by the
-D option, for example
$ initdb -D /usr/local/pgsql/data
Note that you must execute this command while logged into the
PostgreSQL user account, which is
described in the previous section.
Tip:
As an alternative to the -D option, you can set
the environment variable PGDATA.
initdb will attempt to create the directory you
specify if it does not already exist. It is likely that it will not
have the permission to do so (if you followed our advice and created
an unprivileged account). In that case you should create the
directory yourself (as root) and change the owner to be the
PostgreSQL user. Here is how this might
be done:
root# mkdir /usr/local/pgsql/data
root# chown postgres /usr/local/pgsql/data
root# su postgres
postgres$ initdb -D /usr/local/pgsql/data
initdb will refuse to run if the data directory
looks like it it has already been initialized.
Because the data directory contains all the data stored in the
database, it is essential that it be secured from unauthorized
access. initdb therefore revokes access
permissions from everyone but the
PostgreSQL user.
However, while the directory contents are secure, the default
client authentication setup allows any local user to connect to the
database and even become the database superuser. If you don't trust
other local users, we recommend you use initdb's
-W or --pwprompt option to assign a
password to the database superuser. After initdb,
modify the pg_hba.conf file to use md5 or
password instead of trust authentication
before you start the server for the first time. (Other,
approaches include using ident authentication or
file system permissions to restrict connections. See Chapter 6 for more information.)
initdb also initializes the default locale for
the database cluster. Normally, it will just take the locale
settings in the environment and apply them to the initialized
database. It is possible to specify a different locale for the
database; more information about that can be found in Section 7.1. One surprise you might encounter while running
initdb is a notice similar to this:
The database cluster will be initialized with locale de_DE.
This locale setting will prevent the use of indexes for pattern matching
operations. If that is a concern, rerun initdb with the collation order
set to "C". For more information see the Administrator's Guide.
This is intended to warn you that the currently selected locale
will cause indexes to be sorted in an order that prevents them from
being used for LIKE and regular-expression searches. If you need
good performance in such searches, you should set your current
locale to C and re-run initdb, e.g.,
by running initdb --lc-collate=C. The sort
order used within a particular database cluster is set by
initdb and cannot be changed later, short of
dumping all data, rerunning initdb, and
reloading the data. So it's important to make this choice correctly
the first time.