In order to create a databases, the PostgreSQL server must be up and running (see Section 3.3).
Databases are created with the query language command CREATE DATABASE:
CREATE DATABASE name
where name follows the usual rules for SQL identifiers. The current user automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later on (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 4.2 for how to grant permission.
Since you need to be connected to the database server in order to execute the 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.) By convention this database is called template1. So to create the first "real" database you can connect to template1.
The name "template1" is no accident: When a new database is created, the template database is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. This implies that you should not use the template database for real work, but when used judiciously this feature can be convenient. More details appear in Section 5.3.
As an extra convenience, there is also a program that you can execute from the shell to create new databases, createdb.
createdb dbname
createdb does no magic. It connects to the template1 database and issues the CREATE DATABASE command, exactly as described above. It uses the psql program internally. The reference page on createdb contains the invocation details. Note that createdb without any arguments will create a database with the current user name, which may or may not be what you want.
Note: Chapter 6 contains information about how to restrict who can connect to a given database.
Sometimes you want to create a database for someone else. That user should 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 username;
from the SQL environment, or
createdb -O username dbname
You must be a superuser to be allowed to create a database for someone else.
No comments could be found for this page.
Please use this form to add your own comments regarding your experience with particular features of PostgreSQL, clarifications of the documentation, or hints for other users. Please note, this is not a support forum, and your IP address will be logged. If you have a question or need help, please see the faq, try a mailing list, or join us on IRC. Note that submissions containing URLs or other keywords commonly found in 'spam' comments may be silently discarded. Please contact the webmaster if you think this is happening to you in error.
In order to submit a comment, you must have a community account.
* denotes required field