summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/tutorial-createdb.html
blob: 5a305fadf6ff4d6567cea309a30bba265671566d (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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>1.3. 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="tutorial-arch.html" title="1.2. Architectural Fundamentals" /><link rel="next" href="tutorial-accessdb.html" title="1.4. Accessing a Database" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">1.3. Creating a Database</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-arch.html" title="1.2. Architectural Fundamentals">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-start.html" title="Chapter 1. Getting Started">Up</a></td><th width="60%" align="center">Chapter 1. Getting Started</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="tutorial-accessdb.html" title="1.4. Accessing a Database">Next</a></td></tr></table><hr /></div><div class="sect1" id="TUTORIAL-CREATEDB"><div class="titlepage"><div><div><h2 class="title" style="clear: both">1.3. Creating a Database <a href="#TUTORIAL-CREATEDB" class="id_link">#</a></h2></div></div></div><a id="id-1.4.3.4.2" class="indexterm"></a><a id="id-1.4.3.4.3" class="indexterm"></a><p>
    The first test to see whether you can access the database server
    is to try to create a database.  A running
    <span class="productname">PostgreSQL</span> server can manage many
    databases.  Typically, a separate database is used for each
    project or for each user.
   </p><p>
    Possibly, your site administrator has already created a database
    for your use.  In that case you can omit this step and skip ahead
    to the next section.
   </p><p>
    To create a new database, in this example named
    <code class="literal">mydb</code>, you use the following command:
</p><pre class="screen">
<code class="prompt">$</code> <strong class="userinput"><code>createdb mydb</code></strong>
</pre><p>
    If this produces no response then this step was successful and you can skip over the
    remainder of this section.
   </p><p>
    If you see a message similar to:
</p><pre class="screen">
createdb: command not found
</pre><p>
    then <span class="productname">PostgreSQL</span> was not installed properly.  Either it was not
    installed at all or your shell's search path was not set to include it.
    Try calling the command with an absolute path instead:
</p><pre class="screen">
<code class="prompt">$</code> <strong class="userinput"><code>/usr/local/pgsql/bin/createdb mydb</code></strong>
</pre><p>
    The path at your site might be different.  Contact your site
    administrator or check the installation instructions to
    correct the situation.
   </p><p>
    Another response could be this:
</p><pre class="screen">
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?
</pre><p>
    This means that the server was not started, or it is not listening
    where <code class="command">createdb</code> expects to contact it.  Again, check the
    installation instructions or consult the administrator.
   </p><p>
    Another response could be this:
</p><pre class="screen">
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "joe" does not exist
</pre><p>
    where your own login name is mentioned.  This will happen if the
    administrator has not created a <span class="productname">PostgreSQL</span> user account
    for you.  (<span class="productname">PostgreSQL</span> user accounts are distinct from
    operating system user accounts.)  If you are the administrator, see
    <a class="xref" href="user-manag.html" title="Chapter 22. Database Roles">Chapter 22</a> for help creating accounts.  You will need to
    become the operating system user under which <span class="productname">PostgreSQL</span>
    was installed (usually <code class="literal">postgres</code>) to create the first user
    account.  It could also be that you were assigned a
    <span class="productname">PostgreSQL</span> user name that is different from your
    operating system user name; in that case you need to use the <code class="option">-U</code>
    switch or set the <code class="envar">PGUSER</code> environment variable to specify your
    <span class="productname">PostgreSQL</span> user name.
   </p><p>
    If you have a user account but it does not have the privileges required to
    create a database, you will see the following:
</p><pre class="screen">
createdb: error: database creation failed: ERROR:  permission denied to create database
</pre><p>
    Not every user has authorization to create new databases.  If
    <span class="productname">PostgreSQL</span> refuses to create databases
    for you then the site administrator needs to grant you permission
    to create databases.  Consult your site administrator if this
    occurs.  If you installed <span class="productname">PostgreSQL</span>
    yourself then you should log in for the purposes of this tutorial
    under the user account that you started the server as.

    <a href="#ftn.id-1.4.3.4.10.4" class="footnote"><sup class="footnote" id="id-1.4.3.4.10.4">[1]</sup></a>
   </p><p>
    You can also create databases with other names.
    <span class="productname">PostgreSQL</span> allows you to create any
    number of databases at a given site.  Database names must have an
    alphabetic first character and are limited to 63 bytes in
    length.  A convenient choice is to create a database with the same
    name as your current user name.  Many tools assume that database
    name as the default, so it can save you some typing.  To create
    that database, simply type:
</p><pre class="screen">
<code class="prompt">$</code> <strong class="userinput"><code>createdb</code></strong>
</pre><p>
   </p><p>
    If you do not want to use your database anymore you can remove it.
    For example, if you are the owner (creator) of the database
    <code class="literal">mydb</code>, you can destroy it using the following
    command:
</p><pre class="screen">
<code class="prompt">$</code> <strong class="userinput"><code>dropdb mydb</code></strong>
</pre><p>
    (For this command, the database name does not default to the user
    account name.  You always need to specify it.)  This action
    physically removes all files associated with the database and
    cannot be undone, so this should only be done with a great deal of
    forethought.
   </p><p>
    More about <code class="command">createdb</code> and <code class="command">dropdb</code> can
    be found in <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> and <a class="xref" href="app-dropdb.html" title="dropdb"><span class="refentrytitle"><span class="application">dropdb</span></span></a>
    respectively.
   </p><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.4.3.4.10.4" class="footnote"><p><a href="#id-1.4.3.4.10.4" class="para"><sup class="para">[1] </sup></a>
      As an explanation for why this works:
      <span class="productname">PostgreSQL</span> user names are separate
      from operating system user accounts.  When you connect to a
      database, you can choose what
      <span class="productname">PostgreSQL</span> user name to connect as;
      if you don't, it will default to the same name as your current
      operating system account.  As it happens, there will always be a
      <span class="productname">PostgreSQL</span> user account that has the
      same name as the operating system user that started the server,
      and it also happens that that user always has permission to
      create databases.  Instead of logging in as that user you can
      also specify the <code class="option">-U</code> option everywhere to select
      a <span class="productname">PostgreSQL</span> user name to connect as.
     </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-arch.html" title="1.2. Architectural Fundamentals">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-start.html" title="Chapter 1. Getting Started">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-accessdb.html" title="1.4. Accessing a Database">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.2. Architectural Fundamentals </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"> 1.4. Accessing a Database</td></tr></table></div></body></html>