codekingpro/portable-devtools
114k
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>23.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="23.1. Overview" /><link rel="next" href="manage-ag-templatedbs.html" title="23.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">23.2. Creating a Database</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-overview.html" title="23.1. Overview">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="managing-databases.html" title="Chapter 23. Managing Databases">Up</a></td><th width="60%" align="center">Chapter 23. Managing Databases</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="manage-ag-templatedbs.html" title="23.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">23.2. Creating a Database <a href="#MANAGE-AG-CREATEDB" class="id_link">#</a></h2></div></div></div><a id="id-1.6.10.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="19.3. Starting the Database Server">Section 19.3</a>).5 </p><p>6 Databases are created with the SQL command7 <a class="xref" href="sql-createdatabase.html" title="CREATE DATABASE"><span class="refentrytitle">CREATE DATABASE</span></a>:8</p><pre class="synopsis">9CREATE DATABASE <em class="replaceable"><code>name</code></em>;10</pre><p>11 where <em class="replaceable"><code>name</code></em> follows the usual rules for12 <acronym class="acronym">SQL</acronym> identifiers. The current role automatically13 becomes the owner of the new database. It is the privilege of the14 owner of a database to remove it later (which also removes all15 the objects in it, even if they have a different owner).16 </p><p>17 The creation of databases is a restricted operation. See <a class="xref" href="role-attributes.html" title="22.2. Role Attributes">Section 22.2</a> for how to grant permission.18 </p><p>19 Since you need to be connected to the database server in order to20 execute the <code class="command">CREATE DATABASE</code> command, the21 question remains how the <span class="emphasis"><em>first</em></span> database at any given22 site can be created. The first database is always created by the23 <code class="command">initdb</code> command when the data storage area is24 initialized. (See <a class="xref" href="creating-cluster.html" title="19.2. Creating a Database Cluster">Section 19.2</a>.) This25 database is called26 <code class="literal">postgres</code>.<a id="id-1.6.10.5.6.6" class="indexterm"></a> So to27 create the first <span class="quote">“<span class="quote">ordinary</span>”</span> database you can connect to28 <code class="literal">postgres</code>.29 </p><p>30 Two additional databases,31 <code class="literal">template1</code><a id="id-1.6.10.5.7.2" class="indexterm"></a>32 and33 <code class="literal">template0</code>,<a id="id-1.6.10.5.7.4" class="indexterm"></a>34 are also created during database cluster initialization. Whenever a35 new database is created within the36 cluster, <code class="literal">template1</code> is essentially cloned.37 This means that any changes you make in <code class="literal">template1</code> are38 propagated to all subsequently created databases. Because of this,39 avoid creating objects in <code class="literal">template1</code> unless you want them40 propagated to every newly created database.41 <code class="literal">template0</code> is meant as a pristine copy of the original42 contents of <code class="literal">template1</code>. It can be cloned instead43 of <code class="literal">template1</code> when it is important to make a database44 without any such site-local additions. More details45 appear in <a class="xref" href="manage-ag-templatedbs.html" title="23.3. Template Databases">Section 23.3</a>.46 </p><p>47 As a convenience, there is a program you can48 execute from the shell to create new databases,49 <code class="command">createdb</code>.<a id="id-1.6.10.5.8.2" class="indexterm"></a>50 51</p><pre class="synopsis">52createdb <em class="replaceable"><code>dbname</code></em>53</pre><p>54 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 invocation59 details. Note that <code class="command">createdb</code> without any arguments will create60 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 21. Client Authentication">Chapter 21</a> contains information about63 how to restrict who can connect to a given database.64 </p></div><p>65 Sometimes you want to create a database for someone else, and have them66 become the owner of the new database, so they can67 configure and manage it themselves. To achieve that, use one of the68 following commands:69</p><pre class="programlisting">70CREATE DATABASE <em class="replaceable"><code>dbname</code></em> OWNER <em class="replaceable"><code>rolename</code></em>;71</pre><p>72 from the SQL environment, or:73</p><pre class="programlisting">74createdb -O <em class="replaceable"><code>rolename</code></em> <em class="replaceable"><code>dbname</code></em>75</pre><p>76 from the shell.77 Only the superuser is allowed to create a database for78 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="23.1. Overview">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="managing-databases.html" title="Chapter 23. Managing Databases">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="manage-ag-templatedbs.html" title="23.3. Template Databases">Next</a></td></tr><tr><td width="40%" align="left" valign="top">23.1. Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 23.3. Template Databases</td></tr></table></div></body></html>