⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 manage-ag.sgml

📁 PostgreSQL7.4.6 for Linux
💻 SGML
📖 第 1 页 / 共 2 页
字号:
<!--$Header: /cvsroot/pgsql/doc/src/sgml/manage-ag.sgml,v 2.28 2003/08/31 17:32:19 petere Exp $--><chapter id="managing-databases"> <title>Managing Databases</title> <indexterm zone="managing-databases"><primary>database</></> <para>  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. </para> <sect1 id="manage-ag-overview">  <title>Overview</title>  <indexterm zone="manage-ag-overview">   <primary>schema</primary>  </indexterm>  <para>   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   <literal>pg_database</>, that belong to a whole cluster and   are accessible from each database within the cluster.)  More   accurately, a database is a collection of schemas and the schemas   contain the tables, functions, etc.  So the full hierarchy is:   server, database, schema, table (or something else instead of a   table).  </para>  <para>   An application that connects to the database server specifies in   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   connections it opens to the same or other databases.)  It is   possible, however, to access more than one schema from the same   connection.  Schemas are a purely logical structure and who can   access what is managed by the privilege system.  Databases are   physically separated and access control is managed at the   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   resources they should be put in the same databases but possibly   into separate schemas.  More information about managing schemas is   in <xref linkend="ddl-schemas">.  </para>  <note>   <para>    <acronym>SQL</> calls databases <quote>catalogs</>, but there is no    difference in practice.   </para>  </note> </sect1> <sect1 id="manage-ag-createdb">  <title>Creating a Database</title>  <para>   In order to create a databases, the <productname>PostgreSQL</>   server must be up and running (see <xref   linkend="postmaster-start">).  </para>  <para>   Databases are created with the SQL command <command>CREATE   DATABASE</command>:<indexterm><primary>CREATE DATABASE</></><synopsis>CREATE DATABASE <replaceable>name</>;</synopsis>   where <replaceable>name</> follows the usual rules for   <acronym>SQL</acronym> 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).  </para>  <para>   The creation of databases is a restricted operation. See <xref   linkend="user-attributes"> for how to grant permission.  </para>  <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   initialized. (See <xref linkend="creating-cluster">.)  This   database is called   <literal>template1</>.<indexterm><primary>template1</></> So to   create the first <quote>real</> database you can connect to   <literal>template1</>.  </para>  <para>   The name <literal>template1</literal> is no accident: When a new   database is created, the template database is essentially cloned.   This means that any changes you make in <literal>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 <xref linkend="manage-ag-templatedbs">.  </para>  <para>   As an extra convenience, there is also a program that you can   execute from the shell to create new databases,   <command>createdb</>.<indexterm><primary>createdb</></><synopsis>createdb <replaceable class="parameter">dbname</replaceable></synopsis>   <command>createdb</> does no magic. It connects to the <literal>template1</>   database and issues the <command>CREATE DATABASE</> command,   exactly as described above.   The reference page on <command>createdb</> contains the invocation   details. Note that <command>createdb</> without any arguments will create   a database with the current user name, which may or may not be what   you want.  </para>  <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   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:<programlisting>CREATE DATABASE <replaceable>dbname</> OWNER <replaceable>username</>;</programlisting>   from the SQL environment, or<programlisting>createdb -O <replaceable>username</> <replaceable>dbname</></programlisting>   You must be a superuser to be allowed to create a database for   someone else.  </para> </sect1> <sect1 id="manage-ag-templatedbs">  <title>Template Databases</title>  <para>   <command>CREATE DATABASE</> actually works by copying an existing   database.  By default, it copies the standard system database named   <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   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   language <application>PL/pgSQL</> in <literal>template1</>, it will   automatically be available in user databases without any extra   action being taken when those databases are made.  </para>  <para>   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</>.  </para>  <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>  <para>   It is possible to create additional template databases, and indeed   one might copy any database in a cluster by specifying its name   as the template for <command>CREATE DATABASE</>.  It is important to   understand, however, that this is not (yet) intended as   a general-purpose <quote><command>COPY DATABASE</command></quote> 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.  <command>CREATE DATABASE</>   will check   that no session (other than itself) is 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, which   would result in an inconsistent copied database.  Therefore,   we recommend that databases used as templates be treated as read-only.  </para>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -