Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
21.2. Creating a Database
Prev UpChapter 21. Managing DatabasesHome Next

21.2. Creating a Database#

In order to create a database, thePostgres Pro server must be up and running (seeSection 17.3).

Databases are created with the SQL commandCREATE DATABASE:

CREATE DATABASEname;

wherename follows the usual rules forSQL 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. SeeSection 20.2 for how to grant permission.

Since you need to be connected to the database server in order to execute theCREATE DATABASE command, the question remains how thefirst database at any given site can be created. The first database is always created by theinitdb command when the data storage area is initialized. (SeeSection 17.2.) This database is calledpostgres. So to create the firstordinary database you can connect topostgres.

Two additional databases,template1 andtemplate0, are also created during database cluster initialization. Whenever a new database is created within the cluster,template1 is essentially cloned. This means that any changes you make intemplate1 are propagated to all subsequently created databases. Because of this, avoid creating objects intemplate1 unless you want them propagated to every newly created database.template0 is meant as a pristine copy of the original contents oftemplate1. It can be cloned instead oftemplate1 when it is important to make a database without any such site-local additions. More details appear inSection 21.3.

As a convenience, there is a program you can execute from the shell to create new databases,createdb.

createdbdbname

createdb does no magic. It connects to thepostgres database and issues theCREATE DATABASE command, exactly as described above. Thecreatedb reference page contains the invocation details. Note thatcreatedb without any arguments will create a database with the current user name.

Note

Chapter 19 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 them become the owner of the new database, so they can configure and manage it themselves. To achieve that, use one of the following commands:

CREATE DATABASEdbname OWNERrolename;

from the SQL environment, or:

createdb -Orolenamedbname

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).


Prev Up Next
21.1. Overview Home 21.3. Template Databases
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp