Use CREATE DATABASE to create a new database on the system. When you create a new database, the PostgreSQL user you are logged in as will automatically become the owner of the new database, so be sure you are logged in correctly before using this command.
Note: If absolutely necessary, you can change the owner of a database by performing an UPDATE on the pg_database system table's datdba column to be a different user's PostgreSQL system ID).
usually describes an environment variable, which contains the location of the path to create the database in. This environment variable must exist in the environment of the user running the
. In this manner, administrators have more control over where on the filesystem databases can be created. See Chapter 9, for more information on this.
The directory you choose to store the database in must be prepared with the
) command beforehand. See Chapter 9 for more on these commands.
Note: If PostgreSQL has been compiled with
after configuration), absolute pathnames are allowed. This is not allowed by default, due to security and data integrity issues that can arise from using database locations specified as absolute paths.
To create a new database, PostgreSQL clones a database template (template1, by default). If you wish to use a different database template, specify it with the TEMPLATE clause. To create a completely new database (with no cloned template objects), pass template0 as the name of the template to clone from.
The following example creates a database with the name of booktown:
CREATE DATABASE booktown;
This next example specifies the creation of a database with a different data directory location for the new database:
CREATE DATABASE booktown WITH LOCATION = '/usr/local/pgsql/booktown';