1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>22.2. Creating a Database</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="manage-ag-overview.html" title="22.1. Overview" /><link rel="next" href="manage-ag-templatedbs.html" title="22.3. Template Databases" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">22.2. Creating a Database</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-overview.html" title="22.1. Overview">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="managing-databases.html" title="Chapter 22. Managing Databases">Up</a></td><th width="60%" align="center">Chapter 22. Managing Databases</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="manage-ag-templatedbs.html" title="22.3. Template Databases">Next</a></td></tr></table><hr /></div><div class="sect1" id="MANAGE-AG-CREATEDB"><div class="titlepage"><div><div><h2 class="title" style="clear: both">22.2. Creating a Database <a href="#MANAGE-AG-CREATEDB" class="id_link">#</a></h2></div></div></div><a id="id-1.6.9.5.2" class="indexterm"></a><p>
3 In order to create a database, the <span class="productname">PostgreSQL</span>
4 server must be up and running (see <a class="xref" href="server-start.html" title="18.3. Starting the Database Server">Section 18.3</a>).
6 Databases are created with the SQL command
7 <a class="xref" href="sql-createdatabase.html" title="CREATE DATABASE"><span class="refentrytitle">CREATE DATABASE</span></a>:
8 </p><pre class="synopsis">
9 CREATE DATABASE <em class="replaceable"><code>name</code></em>;
11 where <em class="replaceable"><code>name</code></em> follows the usual rules for
12 <acronym class="acronym">SQL</acronym> identifiers. The current role automatically
13 becomes the owner of the new database. It is the privilege of the
14 owner of a database to remove it later (which also removes all
15 the objects in it, even if they have a different owner).
17 The creation of databases is a restricted operation. See <a class="xref" href="role-attributes.html" title="21.2. Role Attributes">Section 21.2</a> for how to grant permission.
19 Since you need to be connected to the database server in order to
20 execute the <code class="command">CREATE DATABASE</code> command, the
21 question remains how the <span class="emphasis"><em>first</em></span> database at any given
22 site can be created. The first database is always created by the
23 <code class="command">initdb</code> command when the data storage area is
24 initialized. (See <a class="xref" href="creating-cluster.html" title="18.2. Creating a Database Cluster">Section 18.2</a>.) This
26 <code class="literal">postgres</code>.<a id="id-1.6.9.5.6.6" class="indexterm"></a> So to
27 create the first <span class="quote">“<span class="quote">ordinary</span>”</span> database you can connect to
28 <code class="literal">postgres</code>.
30 Two additional databases,
31 <code class="literal">template1</code><a id="id-1.6.9.5.7.2" class="indexterm"></a>
33 <code class="literal">template0</code>,<a id="id-1.6.9.5.7.4" class="indexterm"></a>
34 are also created during database cluster initialization. Whenever a
35 new database is created within the
36 cluster, <code class="literal">template1</code> is essentially cloned.
37 This means that any changes you make in <code class="literal">template1</code> are
38 propagated to all subsequently created databases. Because of this,
39 avoid creating objects in <code class="literal">template1</code> unless you want them
40 propagated to every newly created database.
41 <code class="literal">template0</code> is meant as a pristine copy of the original
42 contents of <code class="literal">template1</code>. It can be cloned instead
43 of <code class="literal">template1</code> when it is important to make a database
44 without any such site-local additions. More details
45 appear in <a class="xref" href="manage-ag-templatedbs.html" title="22.3. Template Databases">Section 22.3</a>.
47 As a convenience, there is a program you can
48 execute from the shell to create new databases,
49 <code class="command">createdb</code>.<a id="id-1.6.9.5.8.2" class="indexterm"></a>
51 </p><pre class="synopsis">
52 createdb <em class="replaceable"><code>dbname</code></em>
55 <code class="command">createdb</code> does no magic. It connects to the <code class="literal">postgres</code>
56 database and issues the <code class="command">CREATE DATABASE</code> command,
57 exactly as described above.
58 The <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> reference page contains the invocation
59 details. Note that <code class="command">createdb</code> without any arguments will create
60 a database with the current user name.
61 </p><div class="note"><h3 class="title">Note</h3><p>
62 <a class="xref" href="client-authentication.html" title="Chapter 20. Client Authentication">Chapter 20</a> contains information about
63 how to restrict who can connect to a given database.
65 Sometimes you want to create a database for someone else, and have them
66 become the owner of the new database, so they can
67 configure and manage it themselves. To achieve that, use one of the
69 </p><pre class="programlisting">
70 CREATE DATABASE <em class="replaceable"><code>dbname</code></em> OWNER <em class="replaceable"><code>rolename</code></em>;
72 from the SQL environment, or:
73 </p><pre class="programlisting">
74 createdb -O <em class="replaceable"><code>rolename</code></em> <em class="replaceable"><code>dbname</code></em>
77 Only the superuser is allowed to create a database for
78 someone else (that is, for a role you are not a member of).
79 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="manage-ag-overview.html" title="22.1. Overview">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="managing-databases.html" title="Chapter 22. Managing Databases">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="manage-ag-templatedbs.html" title="22.3. Template Databases">Next</a></td></tr><tr><td width="40%" align="left" valign="top">22.1. Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 22.3. Template Databases</td></tr></table></div></body></html>