OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / doc / postgresql / html / manage-ag-createdb.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Creating a Database</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="Managing Databases"
16 HREF="managing-databases.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Managing Databases"
19 HREF="managing-databases.html"><LINK
20 REL="NEXT"
21 TITLE="Template Databases"
22 HREF="manage-ag-templatedbs.html"><LINK
23 REL="STYLESHEET"
24 TYPE="text/css"
25 HREF="stylesheet.css"><META
26 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="SECT1"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="managing-databases.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="managing-databases.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 >Chapter 18. Managing Databases</TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="managing-databases.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="manage-ag-templatedbs.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="MANAGE-AG-CREATEDB"
96 >18.2. Creating a Database</A
97 ></H1
98 ><P
99 >   In order to create a databases, the <SPAN
100 CLASS="PRODUCTNAME"
101 >PostgreSQL</SPAN
102 >
103    server must be up and running (see <A
104 HREF="postmaster-start.html"
105 >Section 16.3</A
106 >).
107   </P
108 ><P
109 >   Databases are created with the SQL command <TT
110 CLASS="COMMAND"
111 >CREATE
112    DATABASE</TT
113 >:<A
114 NAME="AEN17376"
115 ></A
116 >
117 </P><PRE
118 CLASS="SYNOPSIS"
119 >CREATE DATABASE <VAR
120 CLASS="REPLACEABLE"
121 >name</VAR
122 >;</PRE
123 ><P>
124    where <VAR
125 CLASS="REPLACEABLE"
126 >name</VAR
127 > follows the usual rules for
128    <ACRONYM
129 CLASS="ACRONYM"
130 >SQL</ACRONYM
131 > identifiers.  The current user automatically
132    becomes the owner of the new database. It is the privilege of the
133    owner of a database to remove it later on (which also removes all
134    the objects in it, even if they have a different owner).
135   </P
136 ><P
137 >   The creation of databases is a restricted operation. See <A
138 HREF="user-attributes.html"
139 >Section 17.2</A
140 > for how to grant permission.
141   </P
142 ><P
143 >   Since you need to be connected to the database server in order to
144    execute the <TT
145 CLASS="COMMAND"
146 >CREATE DATABASE</TT
147 > command, the
148    question remains how the <SPAN
149 CLASS="emphasis"
150 ><I
151 CLASS="EMPHASIS"
152 >first</I
153 ></SPAN
154 > database at any given
155    site can be created. The first database is always created by the
156    <TT
157 CLASS="COMMAND"
158 >initdb</TT
159 > command when the data storage area is
160    initialized. (See <A
161 HREF="creating-cluster.html"
162 >Section 16.2</A
163 >.)  This
164    database is called
165    <TT
166 CLASS="LITERAL"
167 >template1</TT
168 >.<A
169 NAME="AEN17390"
170 ></A
171 > So to
172    create the first <SPAN
173 CLASS="QUOTE"
174 >"real"</SPAN
175 > database you can connect to
176    <TT
177 CLASS="LITERAL"
178 >template1</TT
179 >.
180   </P
181 ><P
182 >   The name <TT
183 CLASS="LITERAL"
184 >template1</TT
185 > is no accident: When a new
186    database is created, the template database is essentially cloned.
187    This means that any changes you make in <TT
188 CLASS="LITERAL"
189 >template1</TT
190 > are
191    propagated to all subsequently created databases. This implies that
192    you should not use the template database for real work, but when
193    used judiciously this feature can be convenient.  More details
194    appear in <A
195 HREF="manage-ag-templatedbs.html"
196 >Section 18.3</A
197 >.
198   </P
199 ><P
200 >   As an extra convenience, there is also a program that you can
201    execute from the shell to create new databases,
202    <TT
203 CLASS="COMMAND"
204 >createdb</TT
205 >.<A
206 NAME="AEN17400"
207 ></A
208 >
209
210 </P><PRE
211 CLASS="SYNOPSIS"
212 >createdb <VAR
213 CLASS="REPLACEABLE"
214 >dbname</VAR
215 ></PRE
216 ><P>
217
218    <TT
219 CLASS="COMMAND"
220 >createdb</TT
221 > does no magic. It connects to the <TT
222 CLASS="LITERAL"
223 >template1</TT
224 >
225    database and issues the <TT
226 CLASS="COMMAND"
227 >CREATE DATABASE</TT
228 > command,
229    exactly as described above.
230    The reference page on <TT
231 CLASS="COMMAND"
232 >createdb</TT
233 > contains the invocation
234    details. Note that <TT
235 CLASS="COMMAND"
236 >createdb</TT
237 > without any arguments will create
238    a database with the current user name, which may or may not be what
239    you want.
240   </P
241 ><DIV
242 CLASS="NOTE"
243 ><BLOCKQUOTE
244 CLASS="NOTE"
245 ><P
246 ><B
247 >Note: </B
248 >    <A
249 HREF="client-authentication.html"
250 >Chapter 19</A
251 > contains information about
252     how to restrict who can connect to a given database.
253    </P
254 ></BLOCKQUOTE
255 ></DIV
256 ><P
257 >   Sometimes you want to create a database for someone else.  That
258    user should become the owner of the new database, so he can
259    configure and manage it himself.  To achieve that, use one of the
260    following commands:
261 </P><PRE
262 CLASS="PROGRAMLISTING"
263 >CREATE DATABASE <VAR
264 CLASS="REPLACEABLE"
265 >dbname</VAR
266 > OWNER <VAR
267 CLASS="REPLACEABLE"
268 >username</VAR
269 >;</PRE
270 ><P>
271    from the SQL environment, or
272 </P><PRE
273 CLASS="PROGRAMLISTING"
274 >createdb -O <VAR
275 CLASS="REPLACEABLE"
276 >username</VAR
277 > <VAR
278 CLASS="REPLACEABLE"
279 >dbname</VAR
280 ></PRE
281 ><P>
282    You must be a superuser to be allowed to create a database for
283    someone else.
284   </P
285 ></DIV
286 ><DIV
287 CLASS="NAVFOOTER"
288 ><HR
289 ALIGN="LEFT"
290 WIDTH="100%"><TABLE
291 SUMMARY="Footer navigation table"
292 WIDTH="100%"
293 BORDER="0"
294 CELLPADDING="0"
295 CELLSPACING="0"
296 ><TR
297 ><TD
298 WIDTH="33%"
299 ALIGN="left"
300 VALIGN="top"
301 ><A
302 HREF="managing-databases.html"
303 ACCESSKEY="P"
304 >Prev</A
305 ></TD
306 ><TD
307 WIDTH="34%"
308 ALIGN="center"
309 VALIGN="top"
310 ><A
311 HREF="index.html"
312 ACCESSKEY="H"
313 >Home</A
314 ></TD
315 ><TD
316 WIDTH="33%"
317 ALIGN="right"
318 VALIGN="top"
319 ><A
320 HREF="manage-ag-templatedbs.html"
321 ACCESSKEY="N"
322 >Next</A
323 ></TD
324 ></TR
325 ><TR
326 ><TD
327 WIDTH="33%"
328 ALIGN="left"
329 VALIGN="top"
330 >Managing Databases</TD
331 ><TD
332 WIDTH="34%"
333 ALIGN="center"
334 VALIGN="top"
335 ><A
336 HREF="managing-databases.html"
337 ACCESSKEY="U"
338 >Up</A
339 ></TD
340 ><TD
341 WIDTH="33%"
342 ALIGN="right"
343 VALIGN="top"
344 >Template Databases</TD
345 ></TR
346 ></TABLE
347 ></DIV
348 ></BODY
349 ></HTML
350 >