From 2cf48ca04bf59985117e04dd71644a507be90dbb Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 14 Nov 2000 18:37:49 +0000 Subject: Extend CREATE DATABASE to allow selection of a template database to be cloned, rather than always cloning template1. Modify initdb to generate two identical databases rather than one, template0 and template1. Connections to template0 are disallowed, so that it will always remain in its virgin as-initdb'd state. pg_dumpall now dumps databases with restore commands that say CREATE DATABASE foo WITH TEMPLATE = template0. This allows proper behavior when there is user-added data in template1. initdb forced! --- doc/src/sgml/ref/create_database.sgml | 110 +++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 22 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml index d9e371b1be8..0bae8450c16 100644 --- a/doc/src/sgml/ref/create_database.sgml +++ b/doc/src/sgml/ref/create_database.sgml @@ -1,5 +1,5 @@ @@ -23,7 +23,10 @@ Postgres documentation 1999-12-11 -CREATE DATABASE name [ WITH LOCATION = 'dbpath' ] +CREATE DATABASE name + [ WITH [ LOCATION = 'dbpath' ] + [ TEMPLATE = template ] + [ ENCODING = encoding ] ] @@ -48,8 +51,30 @@ CREATE DATABASE name [ WITH LOCATIO dbpath - An alternate location where to store the new database in the filesystem. - See below for caveats. + An alternate filesystem location in which to store the new database, + specified as a string literal; + or DEFAULT to use the default location. + + + + + template + + + Name of template from which to create the new database, + or DEFAULT to use the default template + (template1). + + + + + encoding + + + Multibyte encoding method to use in the new database. Specify + a string literal name (e.g., 'SQL_ASCII'), + or an integer encoding number, or DEFAULT + to use the default encoding. @@ -98,11 +123,10 @@ CREATE DATABASE name [ WITH LOCATIO - ERROR: Single quotes are not allowed in database names. - ERROR: Single quotes are not allowed in database paths. + ERROR: database path may not contain single quotes - The database name and + The database location dbpath cannot contain single quotes. This is required so that the shell commands that create the database directory can execute safely. @@ -111,18 +135,7 @@ CREATE DATABASE name [ WITH LOCATIO - ERROR: The path 'xxx' is invalid. - - - The expansion of the specified dbpath - (see below) failed. Check the path you entered or make sure that the - environment variable you are referencing does exist. - - - - - - ERROR: createdb: May not be called in a transaction block. + ERROR: CREATE DATABASE: may not be called in a transaction block If you have an explicit transaction block in progress you cannot call @@ -133,6 +146,9 @@ CREATE DATABASE name [ WITH LOCATIO ERROR: Unable to create database directory 'path'. + + + ERROR: Could not initialize database directory. @@ -169,10 +185,10 @@ CREATE DATABASE name [ WITH LOCATIO command. - If the path contains a slash, the leading part is interpreted - as an environment variable, which must be known to the + If the path name does not contain a slash, it is interpreted + as an environment variable name, which must be known to the server process. This way the database administrator can - exercise control over at which locations databases can be created. + exercise control over locations in which databases can be created. (A customary choice is, e.g., 'PGDATA2'.) If the server is compiled with ALLOW_ABSOLUTE_DBPATHS (not so by default), absolute path names, as identified by @@ -181,6 +197,29 @@ CREATE DATABASE name [ WITH LOCATIO are allowed as well. + + By default, the new database will be created by cloning the standard + system database template1. A different template can be + specified by writing TEMPLATE = + name. In particular, + by writing TEMPLATE = template0, you can create a virgin + database containing only the standard objects predefined by your + version of Postgres. This is useful if you wish to avoid copying + any installation-local objects that may have been added to template1. + + + + The optional encoding parameter allows selection of the database encoding, + if your server was compiled with multibyte encoding support. When not + specified, it defaults to the encoding used by the selected template + database. + + + + Optional parameters can be written in any order, not only the order + illustrated above. + + 1999-12-11 @@ -221,6 +260,33 @@ comment from Olly; response from Thomas... Not sure if the dump/reload would guarantee that the alternate data area gets refreshed though... --> + + + Although it is possible to copy a database other than template1 by + specifying its name as the template, this is not (yet) intended as + a general-purpose COPY DATABASE facility. In particular, it is + essential that the source database be idle (no data-altering transactions + in progress) + for the duration of the copying operation. CREATE DATABASE will check + that no backend processes (other than itself) are connected to + the source database at the start of the operation, but this does not + guarantee that changes cannot be made while the copy proceeds. Therefore, + we recommend that databases used as templates be treated as read-only. + + + + Two useful flags exist in pg_database for each + database: datistemplate and + datallowconn. datistemplate + may be set to indicate that a database is intended as a template for + CREATE DATABASE. If this flag is set, the database may be cloned by + any user with CREATEDB privileges; if it is not set, only superusers + and the owner of the database may clone it. + If datallowconn is false, then no new connections + to that database will be allowed (but existing sessions are not killed + simply by setting the flag false). The template0 + database is normally marked this way to prevent modification of it. + -- cgit v1.2.3