manage-ag.sgml 20.2 KB
Newer Older
1
<!-- $PostgreSQL: pgsql/doc/src/sgml/manage-ag.sgml,v 2.48 2006/09/16 00:30:14 momjian Exp $ -->
2

3 4 5
<chapter id="managing-databases">
 <title>Managing Databases</title>

P
Peter Eisentraut 已提交
6 7
 <indexterm zone="managing-databases"><primary>database</></>

8
 <para>
B
Bruce Momjian 已提交
9 10 11 12 13 14
  Every instance of a running <productname>PostgreSQL</productname>
  server manages one or more databases.  Databases are therefore the
  topmost hierarchical level for organizing <acronym>SQL</acronym>
  objects (<quote>database objects</quote>).  This chapter describes
  the properties of databases, and how to create, manage, and destroy
  them.
15 16
 </para>

17
 <sect1 id="manage-ag-overview">
18 19
  <title>Overview</title>

P
Peter Eisentraut 已提交
20 21 22 23
  <indexterm zone="manage-ag-overview">
   <primary>schema</primary>
  </indexterm>

24
  <para>
B
Bruce Momjian 已提交
25 26 27 28
   A database is a named collection of <acronym>SQL</acronym> objects
   (<quote>database objects</quote>).  Generally, every database
   object (tables, functions, etc.) belongs to one and only one
   database.  (But there are a few system catalogs, for example
29 30
   <literal>pg_database</>, that belong to a whole cluster and
   are accessible from each database within the cluster.)  More
B
Bruce Momjian 已提交
31 32
   accurately, a database is a collection of schemas and the schemas
   contain the tables, functions, etc.  So the full hierarchy is:
33 34
   server, database, schema, table (or some other kind of object,
   such as a function).
35 36
  </para>

37
  <para>
T
Tom Lane 已提交
38
   When connecting to the database server, a client must specify in
39 40 41
   its connection request the name of the database it wants to connect
   to. It is not possible to access more than one database per
   connection. (But an application is not restricted in the number of
T
Tom Lane 已提交
42
   connections it opens to the same or other databases.)  Databases are
43
   physically separated and access control is managed at the
B
Bruce Momjian 已提交
44 45 46 47 48
   connection level.  If one <productname>PostgreSQL</> server
   instance is to house projects or users that should be separate and
   for the most part unaware of each other, it is therefore
   recommendable to put them into separate databases.  If the projects
   or users are interrelated and should be able to use each other's
T
Tom Lane 已提交
49 50 51 52
   resources they should be put in the same database, but possibly
   into separate schemas.  Schemas are a purely logical structure and who can
   access what is managed by the privilege system.  More information about
   managing schemas is in <xref linkend="ddl-schemas">.
53 54
  </para>

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  <para>
   Databases are created with the <command>CREATE DATABASE</> command
   (see <xref linkend="manage-ag-createdb">) and destroyed with the
   <command>DROP DATABASE</> command
   (see <xref linkend="manage-ag-dropdb">).
   To determine the set of existing databases, examine the
   <structname>pg_database</> system catalog, for example
<synopsis>
SELECT datname FROM pg_database;
</synopsis>
   The <xref linkend="app-psql"> program's <literal>\l</> meta-command
   and <option>-l</> command-line option are also useful for listing the
   existing databases.
  </para>

70 71
  <note>
   <para>
72 73
    The <acronym>SQL</> standard calls databases <quote>catalogs</>, but there
    is no difference in practice.
74 75 76
   </para>
  </note>
 </sect1>
77

78
 <sect1 id="manage-ag-createdb">
79
  <title>Creating a Database</title>
80

81
  <para>
82
   In order to create a database, the <productname>PostgreSQL</>
83
   server must be up and running (see <xref
84
   linkend="server-start">).
85 86
  </para>

87
  <para>
88
   Databases are created with the SQL command
89
   <xref linkend="sql-createdatabase" endterm="sql-createdatabase-title">:<indexterm><primary>CREATE
90
   DATABASE</></>
91
<synopsis>
92
CREATE DATABASE <replaceable>name</>;
93
</synopsis>
B
Bruce Momjian 已提交
94
   where <replaceable>name</> follows the usual rules for
95
   <acronym>SQL</acronym> identifiers.  The current role automatically
B
Bruce Momjian 已提交
96 97 98
   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).
99 100
  </para>

101 102
  <para>
   The creation of databases is a restricted operation. See <xref
103
   linkend="role-attributes"> for how to grant permission.
104
  </para>
105

106 107 108 109 110 111
  <para>
   Since you need to be connected to the database server in order to
   execute the <command>CREATE DATABASE</command> command, the
   question remains how the <emphasis>first</> database at any given
   site can be created. The first database is always created by the
   <command>initdb</> command when the data storage area is
P
Peter Eisentraut 已提交
112 113
   initialized. (See <xref linkend="creating-cluster">.)  This
   database is called
114 115 116
   <literal>postgres</>.<indexterm><primary>postgres</></> So to
   create the first <quote>ordinary</> database you can connect to
   <literal>postgres</>.
117
  </para>
118

119
  <para>
120 121 122 123 124
   A second database,
   <literal>template1</literal>,<indexterm><primary>template1</></>
   is also created by
   <command>initdb</>.  Whenever a new database is created within the
   cluster, <literal>template1</literal> is essentially cloned.
125
   This means that any changes you make in <literal>template1</> are
126 127
   propagated to all subsequently created databases. Therefore it is
   unwise to use <literal>template1</> for real work, but when
128 129
   used judiciously this feature can be convenient.  More details
   appear in <xref linkend="manage-ag-templatedbs">.
130 131 132
  </para>

  <para>
T
Tom Lane 已提交
133
   As a convenience, there is a program that you can
134
   execute from the shell to create new databases,
P
Peter Eisentraut 已提交
135
   <command>createdb</>.<indexterm><primary>createdb</></>
136 137 138 139 140

<synopsis>
createdb <replaceable class="parameter">dbname</replaceable>
</synopsis>

141
   <command>createdb</> does no magic. It connects to the <literal>postgres</>
142
   database and issues the <command>CREATE DATABASE</> command,
143
   exactly as described above.
T
Tom Lane 已提交
144
   The <xref linkend="app-createdb"> reference page contains the invocation
145
   details. Note that <command>createdb</> without any arguments will create
146 147 148 149
   a database with the current user name, which may or may not be what
   you want.
  </para>

150 151 152 153 154 155 156 157 158
  <note>
   <para>
    <xref linkend="client-authentication"> contains information about
    how to restrict who can connect to a given database.
   </para>
  </note>

  <para>
   Sometimes you want to create a database for someone else.  That
159
   role should become the owner of the new database, so he can
160 161 162
   configure and manage it himself.  To achieve that, use one of the
   following commands:
<programlisting>
163
CREATE DATABASE <replaceable>dbname</> OWNER <replaceable>rolename</>;
164 165 166
</programlisting>
   from the SQL environment, or
<programlisting>
167
createdb -O <replaceable>rolename</> <replaceable>dbname</>
168
</programlisting>
169
   from the shell.
170
   You must be a superuser to be allowed to create a database for
171
   someone else (that is, for a role you are not a member of).
172 173 174 175 176
  </para>
 </sect1>

 <sect1 id="manage-ag-templatedbs">
  <title>Template Databases</title>
177 178 179 180

  <para>
   <command>CREATE DATABASE</> actually works by copying an existing
   database.  By default, it copies the standard system database named
P
Peter Eisentraut 已提交
181 182 183
   <literal>template1</>.<indexterm><primary>template1</></> Thus that
   database is the <quote>template</> from which new databases are
   made.  If you add objects to <literal>template1</>, these objects
184 185 186
   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
187
   language <application>PL/pgSQL</> in <literal>template1</>, it will
P
Peter Eisentraut 已提交
188 189
   automatically be available in user databases without any extra
   action being taken when those databases are made.
190 191 192
  </para>

  <para>
P
Peter Eisentraut 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
   There is a second standard system database named
   <literal>template0</>.<indexterm><primary>template0</></> This
   database contains the same data as the initial contents of
   <literal>template1</>, that is, only the standard objects
   predefined by your version of
   <productname>PostgreSQL</productname>.  <literal>template0</>
   should never be changed after <command>initdb</>.  By instructing
   <command>CREATE DATABASE</> to copy <literal>template0</> instead
   of <literal>template1</>, you can create a <quote>virgin</> user
   database that contains none of the site-local additions in
   <literal>template1</>.  This is particularly handy when restoring a
   <literal>pg_dump</> dump: the dump script should be restored in a
   virgin database to ensure that one recreates the correct contents
   of the dumped database, without any conflicts with additions that
   may now be present in <literal>template1</>.
208 209
  </para>

210 211 212 213 214 215 216 217 218 219 220 221
  <para>
   To create a database by copying <literal>template0</literal>, use
<programlisting>
CREATE DATABASE <replaceable>dbname</> TEMPLATE template0;
</programlisting>
   from the SQL environment, or
<programlisting>
createdb -T template0 <replaceable>dbname</>
</programlisting>
   from the shell.
  </para>

222 223
  <para>
   It is possible to create additional template databases, and indeed
224
   one may copy any database in a cluster by specifying its name
225 226
   as the template for <command>CREATE DATABASE</>.  It is important to
   understand, however, that this is not (yet) intended as
227 228 229 230 231 232
   a general-purpose <quote><command>COPY DATABASE</command></quote> facility.
   The principal limitation is that no other sessions can be connected to
   the source database while it is being copied.  <command>CREATE
   DATABASE</> will fail if any other connection exists when it starts;
   otherwise, new connections to the source database are locked out
   until <command>CREATE DATABASE</> completes.
233 234 235
  </para>

  <para>
P
Peter Eisentraut 已提交
236
   Two useful flags exist in <literal>pg_database</literal><indexterm><primary>pg_database</></> for each
237
   database: the columns <literal>datistemplate</literal> and
238 239 240 241
   <literal>datallowconn</literal>.  <literal>datistemplate</literal>
   may be set to indicate that a database is intended as a template for
   <command>CREATE DATABASE</>.  If this flag is set, the database may be
   cloned by 
P
Peter Eisentraut 已提交
242
   any user with <literal>CREATEDB</> privileges; if it is not set, only superusers
243 244 245 246
   and the owner of the database may clone it.
   If <literal>datallowconn</literal> 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 <literal>template0</literal>
247
   database is normally marked <literal>datallowconn = false</> to prevent modification of it.
248
   Both <literal>template0</literal> and <literal>template1</literal>
249
   should always be marked with <literal>datistemplate = true</>.
250 251 252 253
  </para>

  <para>
   After preparing a template database, or making any changes to one,
254
   it is a good idea to perform <command>VACUUM FREEZE</> in that
255
   database.  If this is done when there are no other open transactions
256
   in the same database, then it is guaranteed that all rows in the
257 258 259
   database are <quote>frozen</> and will not be subject to transaction
   ID wraparound problems.  This is particularly important for a database
   that will have <literal>datallowconn</literal> set to false, since it
260
   will be impossible to do routine maintenance <command>VACUUM</> in
261 262 263 264 265 266 267 268
   such a database.
   See <xref linkend="vacuum-for-wraparound"> for more information.
  </para>

  <note>
   <para>
    <literal>template1</> and <literal>template0</> do not have any special
    status beyond the fact that the name <literal>template1</> is the default
269
    source database name for <command>CREATE DATABASE</>.
270 271 272 273 274
    For example, one could drop <literal>template1</> and recreate it from
    <literal>template0</> without any ill effects.  This course of action
    might be advisable if one has carelessly added a bunch of junk in
    <literal>template1</>.
   </para>
275 276 277 278 279 280 281

   <para>
    The <literal>postgres</> 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 of
    <literal>template1</> and may be dropped and recreated if required.
   </para>
282
  </note>
283 284 285 286 287 288
 </sect1>

 <sect1 id="manage-ag-config">
  <title>Database Configuration</title>

  <para>
B
Bruce Momjian 已提交
289 290 291 292
   Recall from <xref linkend="runtime-config"> that the
   <productname>PostgreSQL</> server provides a large number of
   run-time configuration variables.  You can set database-specific
   default values for many of these settings.
293
  </para>
294

295 296 297 298 299
  <para>
   For example, if for some reason you want to disable the
   <acronym>GEQO</acronym> optimizer for a given database, you'd
   ordinarily have to either disable it for all databases or make sure
   that every connecting client is careful to issue <literal>SET geqo
300 301
   TO off;</literal>.  To make this setting the default within a particular
   database, you can execute the command
302 303 304
<programlisting>
ALTER DATABASE mydb SET geqo TO off;
</programlisting>
T
Tom Lane 已提交
305 306 307 308 309
   This will save the setting (but not set it immediately).  In
   subsequent connections to this database it will appear as though
   <literal>SET geqo TO off;</literal> had been executed just before the
   session started.
   Note that users can still alter this setting during their sessions; it
310 311 312 313 314
   will only be the default.  To undo any such setting, use
   <literal>ALTER DATABASE <replaceable>dbname</> RESET
   <replaceable>varname</>;</literal>.
  </para>
 </sect1>
315

316
 <sect1 id="manage-ag-dropdb">
317
  <title>Destroying a Database</title>
318

319
  <para>
320
   Databases are destroyed with the command 
321
   <xref linkend="sql-dropdatabase" endterm="sql-dropdatabase-title">:<indexterm><primary>DROP DATABASE</></>
322
<synopsis>
323
DROP DATABASE <replaceable>name</>;
324
</synopsis>
325
   Only the owner of the database, or
326 327
   a superuser, can drop a database. Dropping a database removes all objects
   that were 
328 329 330
   contained within the database. The destruction of a database cannot
   be undone.
  </para>
331

332 333 334
  <para>
   You cannot execute the <command>DROP DATABASE</command> command
   while connected to the victim database. You can, however, be
335
   connected to any other database, including the <literal>template1</>
336 337
   database.
   <literal>template1</> would be the only option for dropping the last user database of a
338 339
   given cluster.
  </para>
340

341
  <para>
P
Peter Eisentraut 已提交
342
   For convenience, there is also a shell program to drop
T
Tom Lane 已提交
343
   databases, <xref linkend="app-dropdb">:<indexterm><primary>dropdb</></>
344 345 346 347 348 349 350
<synopsis>
dropdb <replaceable class="parameter">dbname</replaceable>
</synopsis>
   (Unlike <command>createdb</>, it is not the default action to drop
   the database with the current user name.)
  </para>
 </sect1>
351 352 353 354

 <sect1 id="manage-ag-tablespaces">
  <title>Tablespaces</title>

355 356 357 358
  <indexterm zone="manage-ag-tablespaces">
   <primary>tablespace</primary>
  </indexterm>

359
   <para>
360
    Tablespaces in <productname>PostgreSQL</> allow database administrators to
361 362 363 364 365 366
    define locations in the file system where the files representing
    database objects can be stored. Once created, a tablespace can be referred
    to by name when creating database objects.
   </para>

   <para>
367 368 369 370 371 372
    By using tablespaces, an administrator can control the disk layout
    of a <productname>PostgreSQL</> installation. This is useful in at
    least two ways. First, if the partition or volume on which the
    cluster was initialized runs out of space and cannot be extended,
    a tablespace can be created on a different partition and used
    until the system can be reconfigured.
373 374 375
   </para>

   <para>
376 377 378 379 380 381 382
    Second, tablespaces allow an administrator to use knowledge of the
    usage pattern of database objects to optimize performance. For
    example, an index which is very heavily used can be placed on a
    very fast, highly available disk, such as an expensive solid state
    device. At the same time a table storing archived data which is
    rarely used or not performance critical could be stored on a less
    expensive, slower disk system.
383 384 385
   </para>

   <para>
386
    To define a tablespace, use the <xref
387
    linkend="sql-createtablespace" endterm="sql-createtablespace-title">
T
Tom Lane 已提交
388
    command, for example:<indexterm><primary>CREATE TABLESPACE</></>
389 390 391 392 393 394 395 396 397 398
<programlisting>
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';
</programlisting>
    The location must be an existing, empty directory that is owned by
    the <productname>PostgreSQL</> system user.  All objects subsequently
    created within the tablespace will be stored in files underneath this
    directory.
   </para>

   <note>
399 400
    <para>
     There is usually not much point in making more than one
P
Peter Eisentraut 已提交
401 402
     tablespace per logical file system, since you cannot control the location
     of individual files within a logical file system.  However,
403
     <productname>PostgreSQL</> does not enforce any such limitation, and
P
Peter Eisentraut 已提交
404
     indeed it is not directly aware of the file system boundaries on your
405 406
     system.  It just stores files in the directories you tell it to use.
    </para>
407 408 409 410 411 412 413 414 415
   </note>

   <para>
    Creation of the tablespace itself must be done as a database superuser,
    but after that you can allow ordinary database users to make use of it.
    To do that, grant them the <literal>CREATE</> privilege on it.
   </para>

   <para>
416
    Tables, indexes, and entire databases can be assigned to
417 418 419 420 421 422 423 424 425 426
    particular tablespaces. To do so, a user with the <literal>CREATE</>
    privilege on a given tablespace must pass the tablespace name as a 
    parameter to the relevant command. For example, the following creates 
    a table in the tablespace <literal>space1</>:
<programlisting>
CREATE TABLE foo(i int) TABLESPACE space1;
</programlisting>
   </para>

   <para>
427 428 429 430 431 432 433 434 435
    Alternatively, use the <xref linkend="guc-default-tablespace"> parameter:
<programlisting>
SET default_tablespace = space1;
CREATE TABLE foo(i int);
</programlisting>
    When <varname>default_tablespace</> is set to anything but an empty
    string, it supplies an implicit <literal>TABLESPACE</> clause for
    <command>CREATE TABLE</> and <command>CREATE INDEX</> commands that
    do not have an explicit one.
436 437 438
   </para>

   <para>
439 440 441 442 443 444 445 446
    The tablespace associated with a database is used to store the system
    catalogs of that database, as well as any temporary files created by
    server processes using that database.  Furthermore, it is the default
    tablespace selected for tables and indexes created within the database,
    if no <literal>TABLESPACE</> clause is given (either explicitly or via
    <varname>default_tablespace</>) when the objects are created.
    If a database is created without specifying a tablespace for it,
    it uses the same tablespace as the template database it is copied from.
447 448 449 450 451 452 453
   </para>

   <para>
    Two tablespaces are automatically created by <literal>initdb</>. The
    <literal>pg_global</> tablespace is used for shared system catalogs. The
    <literal>pg_default</> tablespace is the default tablespace of the
    <literal>template1</> and <literal>template0</> databases (and, therefore,
454 455 456
    will be the default tablespace for other databases as well, unless
    overridden by a <literal>TABLESPACE</> clause in <command>CREATE
    DATABASE</>).
457 458 459 460 461 462 463 464 465 466
   </para>

   <para>
    Once created, a tablespace can be used from any database, provided
    the requesting user has sufficient privilege. This means that a tablespace
    cannot be dropped until all objects in all databases using the tablespace
    have been removed.
   </para>

   <para>
467 468 469
    To remove an empty tablespace, use the <xref
    linkend="sql-droptablespace" endterm="sql-droptablespace-title">
    command.
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
   </para>

   <para>
    To determine the set of existing tablespaces, examine the
    <structname>pg_tablespace</> system catalog, for example
<synopsis>
SELECT spcname FROM pg_tablespace;
</synopsis>
    The <xref linkend="app-psql"> program's <literal>\db</> meta-command
    is also useful for listing the existing tablespaces.
   </para>

   <para>
    <productname>PostgreSQL</> makes extensive use of symbolic links
    to simplify the implementation of tablespaces. This
485 486 487 488 489 490 491 492 493
    means that tablespaces can be used <emphasis>only</> on systems
    that support symbolic links.
   </para>

   <para>
    The directory <filename>$PGDATA/pg_tblspc</> contains symbolic links that
    point to each of the non-built-in tablespaces defined in the cluster.
    Although not recommended, it is possible to adjust the tablespace
    layout by hand by redefining these links.  Two warnings: do not do so
494
    while the server is running; and after you restart the server,
495 496 497 498 499 500
    update the <structname>pg_tablespace</> catalog to show the new
    locations.  (If you do not, <literal>pg_dump</> will continue to show
    the old tablespace locations.)
   </para> 

 </sect1>
501
</chapter>