Creating a Database Cluster

Before you can do anything, you must initialize a database storage area on disk. This is known as a database cluster. (SQL speaks of a catalog cluster instead.) A database cluster is a collection of databases that will be accessible through a single instance of a running database server. After initialization, a database cluster will contain one database named template1. As the name suggests, this will be used as a template for any subsequently created database; do not use it for actual work and do not drop this database.

In file system terms, a database cluster is a single directory under which all data will be stored. We call this the data directory or data area. You can store your data anywhere, there is no default, although locations such as /var/lib/pgsql/data or /usr/local/pgsql/data are popular.

The instructions that follow describe how to create a database cluster area.

  1. Log in as root.

  2. Create a directory that will be the database cluster area and transfer ownership of it to the postgres user account:
    root# mkdir /var/lib/pgsql/data
    root# chown postgres /var/lib/pgsql/data

  3. Log in as the postgres user account.
    root# su postgres

  4. To initialize a database cluster, you use the command initdb. You can indicate the desired location of your database system with the -D option, or by setting the PGDATA environment variable. For example:
    postgres> initdb -D /var/lib/pgsql/data

    Tip

    As an alternative to the -D option, you can set the environment variable PGDATA.

    initdb will refuse to run if the data directory appears to belong to an already initialized installation.

    Because the data directory contains all the data stored in the database, it is essential that it be well secured from unauthorized access. initdb therefore revokes access permissions from everyone but the postgres user account.

However, while the directory contents are secure, the default pg_hba.conf authentication method of trust 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 option -W or --pwprompt to assign a password to the database superuser. After initdb, modify pg_hba.conf to use md5 or password, instead of trust, authentication before you start the server for the first time. (Other, possibly more convenient approaches include using ident authentication or file system permissions to restrict connections. See Chapter 2 for more information.)