|The PostgreSQL 9.0 Reference Manual - Volume 3 - Server Administration Guide
by The PostgreSQL Global Development Group
Paperback (6"x9"), 274 pages
RRP £9.95 ($14.95)
Sales of this book support the PostgreSQL project! Get a printed copy>>>
7.2 Creating a Database
In order to create a database, the PostgreSQL server must be up and running (see section 3.3 Starting the Database Server).
Databases are created with the SQL command
CREATE DATABASE name;
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).
The creation of databases is a restricted operation. See section 6.2 Role Attributes for how to grant permission.
Since you need to be connected to the database server in order to
CREATE DATABASE command, the
question remains how the first database at any given
site can be created. The first database is always created by the
initdb command when the data storage area is
initialized. (See section 3.2 Creating a Database Cluster.) This
database is called
create the first “ordinary” database you can connect to
A second database,
is also created during database cluster initialization. Whenever a
new database is created within the
template1 is essentially cloned.
This means that any changes you make in
propagated to all subsequently created databases. Because of this,
avoid creating objects in
template1 unless you want them
propagated to every newly created database. More details
appear in section 7.3 Template Databases.
As a convenience, there is a program you can
execute from the shell to create new databases,
createdb does no magic. It connects to the
database and issues the
CREATE DATABASE command,
exactly as described above.
createdb reference page contains the invocation
details. Note that
createdb without any arguments will create
a database with the current user name.
Note: section 5 Client Authentication contains information about how to restrict who can connect to a given database.
Sometimes you want to create a database for someone else, and have him become the owner of the new database, so he can configure and manage it himself. To achieve that, use one of the following commands:
CREATE DATABASE dbname OWNER rolename;
from the SQL environment, or:
createdb -O rolename dbname
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).
|ISBN 9781906966072||The PostgreSQL 9.0 Reference Manual - Volume 3 - Server Administration Guide||See the print edition|