Contents / Previous / Next


Creating DBs

A DB is a named collection of SQL objects (tables, functions, etc.), also called "catalogs".

An application that connects to the postgre server specifies the name of the DB it wants to connect to.

Create DB

DBs are created with: CREATE DATABASE name; The current user automatically becomes the owner of the new DB.
The creation of DBs is a restricted operation, the owner of the cluster has to grant permission.
It is the privilege of the owner of a DB to remove it.

Bootstrapping
Since you need to be connected to the DB server in order to execute the CREATE DATABASE command, the first DB "template1" is always created by the initdb command.
Alternatively you may use createdb command from the shell to create new DBs.

DB Templates
The name "template1" is no accident: When a new DB is created, the template1 DB is cloned. Any changes you make in template1 are propagated to all subsequently created DBs.

Alternative Locations

It is possible to create a DB in a location other than the default location (it must be accessible by the server). Alternative DB locations are referenced by an environment variable (any name id fine) which gives the absolute path to the storage location. To create the variable in the environment of the server process you must first shut down the server, define the variable, initialize the data area, and finally restart the server: shell> export PGDATA2=/home/postgres/data or in Bourne shells: shell> setenv PGDATA2 /home/postgres/data Init the location so the server may find it next time: shell> initlocation PGDATA2 Then restart the server: shell> /etc/init.d/postgresql restart Create a DB within the new location: shell> CREATE DATABASE name WITH LOCATION = "$PGDATA" Note: It can also be possible to specify absolute paths directly without defining environment variables. This is disallowed by default because it is a security risk.