1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>19.2.Creating a Database</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="managing-databases.html" title="Chapter19.Managing Databases">
<link rel="prev" href="managing-databases.html" title="Chapter19.Managing Databases">
<link rel="next" href="manage-ag-templatedbs.html" title="19.3.Template Databases">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="manage-ag-createdb"></a>19.2.Creating a Database</h2></div></div></div>
<p> In order to create a database, the <span class="productname">PostgreSQL</span>
server must be up and running (see <a href="postmaster-start.html" title="16.3.Starting the Database Server">Section16.3, “Starting the Database Server”</a>).
</p>
<p> Databases are created with the SQL command
<a href="sql-createdatabase.html">CREATE DATABASE</a>:<a name="id658945"></a>
</p>
<pre class="synopsis">CREATE DATABASE <em class="replaceable"><code>name</code></em>;</pre>
<p>
where <em class="replaceable"><code>name</code></em> follows the usual rules for
<acronym class="acronym">SQL</acronym> 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 on (which also removes all
the objects in it, even if they have a different owner).
</p>
<p> The creation of databases is a restricted operation. See <a href="role-attributes.html" title="18.2.Role Attributes">Section18.2, “Role Attributes”</a> for how to grant permission.
</p>
<p> Since you need to be connected to the database server in order to
execute the <code class="command">CREATE DATABASE</code> command, the
question remains how the <span class="emphasis"><em>first</em></span> database at any given
site can be created. The first database is always created by the
<code class="command">initdb</code> command when the data storage area is
initialized. (See <a href="creating-cluster.html" title="16.2.Creating a Database Cluster">Section16.2, “Creating a Database Cluster”</a>.) This
database is called
<code class="literal">postgres</code>.<a name="id659028"></a> So to
create the first “<span class="quote">ordinary</span>” database you can connect to
<code class="literal">postgres</code>.
</p>
<p> A second database,
<code class="literal">template1</code>,<a name="id659056"></a>
is also created by
<code class="command">initdb</code>. Whenever a new database is created within the
cluster, <code class="literal">template1</code> is essentially cloned.
This means that any changes you make in <code class="literal">template1</code> are
propagated to all subsequently created databases. Therefore it is
unwise to use <code class="literal">template1</code> for real work, but when
used judiciously this feature can be convenient. More details
appear in <a href="manage-ag-templatedbs.html" title="19.3.Template Databases">Section19.3, “Template Databases”</a>.
</p>
<p> As a convenience, there is a program that you can
execute from the shell to create new databases,
<code class="command">createdb</code>.<a name="id659116"></a>
</p>
<pre class="synopsis">createdb <em class="replaceable"><code>dbname</code></em></pre>
<p>
<code class="command">createdb</code> does no magic. It connects to the <code class="literal">postgres</code>
database and issues the <code class="command">CREATE DATABASE</code> command,
exactly as described above.
The <a href="app-createdb.html" title="createdb"><span class="refentrytitle"><a name="app-createdb-title"></a><span class="application">createdb</span></span></a> reference page contains the invocation
details. Note that <code class="command">createdb</code> without any arguments will create
a database with the current user name, which may or may not be what
you want.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> <a href="client-authentication.html" title="Chapter20.Client Authentication">Chapter20, <i>Client Authentication</i></a> contains information about
how to restrict who can connect to a given database.
</p>
</div>
<p> Sometimes you want to create a database for someone else. That
role 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:
</p>
<pre class="programlisting">CREATE DATABASE <em class="replaceable"><code>dbname</code></em> OWNER <em class="replaceable"><code>rolename</code></em>;</pre>
<p>
from the SQL environment, or
</p>
<pre class="programlisting">createdb -O <em class="replaceable"><code>rolename</code></em> <em class="replaceable"><code>dbname</code></em></pre>
<p>
You must be a superuser to be allowed to create a database for
someone else (that is, for a role you are not a member of).
</p>
</div></body>
</html>
|