Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
21.3. Template Databases
Prev UpChapter 21. Managing DatabasesHome Next

21.3. Template Databases#

CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database namedtemplate1. Thus that database is thetemplate from which new databases are made. If you add objects totemplate1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural languagePL/Perl intemplate1, it will automatically be available in user databases without any extra action being taken when those databases are created.

However,CREATE DATABASE does not copy database-levelGRANT permissions attached to the source database. The new database has default database-level permissions.

There is a second standard system database namedtemplate0. This database contains the same data as the initial contents oftemplate1, that is, only the standard objects predefined by your version ofPostgres Pro.template0 should never be changed after the database cluster has been initialized. By instructingCREATE DATABASE to copytemplate0 instead oftemplate1, you can create apristine user database (one where no user-defined objects exist and where the system objects have not been altered) that contains none of the site-local additions intemplate1. This is particularly handy when restoring apg_dump dump: the dump script should be restored in a pristine database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added totemplate1 later on.

Another common reason for copyingtemplate0 instead oftemplate1 is that new encoding and locale settings can be specified when copyingtemplate0, whereas a copy oftemplate1 must use the same settings it does. This is becausetemplate1 might contain encoding-specific or locale-specific data, whiletemplate0 is known not to.

To create a database by copyingtemplate0, use:

CREATE DATABASEdbname TEMPLATE template0;

from the SQL environment, or:

createdb -T template0dbname

from the shell.

It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template forCREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purposeCOPY DATABASE facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied.CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.

Two useful flags exist inpg_database for each database: the columnsdatistemplate anddatallowconn.datistemplate can be set to indicate that a database is intended as a template forCREATE DATABASE. If this flag is set, the database can be cloned by any user withCREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. Ifdatallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). Thetemplate0 database is normally markeddatallowconn = false to prevent its modification. Bothtemplate0 andtemplate1 should always be marked withdatistemplate = true.

Note

template1 andtemplate0 do not have any special status beyond the fact that the nametemplate1 is the default source database name forCREATE DATABASE. For example, one could droptemplate1 and recreate it fromtemplate0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk intemplate1. (To deletetemplate1, it must havepg_database.datistemplate = false.)

Thepostgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy oftemplate1 and can be dropped and recreated if necessary.


Prev Up Next
21.2. Creating a Database Home 21.4. Database Configuration
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