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>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 18.0 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>
3 The first test to see whether you can access the database server
4 is to try to create a database. A running
5 <span class="productname">PostgreSQL</span> server can manage many
6 databases. Typically, a separate database is used for each
7 project or for each user.
9 Possibly, your site administrator has already created a database
10 for your use. In that case you can omit this step and skip ahead
13 To create a new database from the command line, in this example named
14 <code class="literal">mydb</code>, you use the following command:
15 </p><pre class="screen">
16 <code class="prompt">$</code> <strong class="userinput"><code>createdb mydb</code></strong>
18 If this produces no response then this step was successful and you can skip over the
19 remainder of this section.
21 If you see a message similar to:
22 </p><pre class="screen">
23 createdb: command not found
25 then <span class="productname">PostgreSQL</span> was not installed properly. Either it was not
26 installed at all or your shell's search path was not set to include it.
27 Try calling the command with an absolute path instead:
28 </p><pre class="screen">
29 <code class="prompt">$</code> <strong class="userinput"><code>/usr/local/pgsql/bin/createdb mydb</code></strong>
31 The path at your site might be different. Contact your site
32 administrator or check the installation instructions to
33 correct the situation.
35 Another response could be this:
36 </p><pre class="screen">
37 createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
38 Is the server running locally and accepting connections on that socket?
40 This means that the server was not started, or it is not listening
41 where <code class="command">createdb</code> expects to contact it. Again, check the
42 installation instructions or consult the administrator.
44 Another response could be this:
45 </p><pre class="screen">
46 createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "joe" does not exist
48 where your own login name is mentioned. This will happen if the
49 administrator has not created a <span class="productname">PostgreSQL</span> user account
50 for you. (<span class="productname">PostgreSQL</span> user accounts are distinct from
51 operating system user accounts.) If you are the administrator, see
52 <a class="xref" href="user-manag.html" title="Chapter 21. Database Roles">Chapter 21</a> for help creating accounts. You will need to
53 become the operating system user under which <span class="productname">PostgreSQL</span>
54 was installed (usually <code class="literal">postgres</code>) to create the first user
55 account. It could also be that you were assigned a
56 <span class="productname">PostgreSQL</span> user name that is different from your
57 operating system user name; in that case you need to use the <code class="option">-U</code>
58 switch or set the <code class="envar">PGUSER</code> environment variable to specify your
59 <span class="productname">PostgreSQL</span> user name.
61 If you have a user account but it does not have the privileges required to
62 create a database, you will see the following:
63 </p><pre class="screen">
64 createdb: error: database creation failed: ERROR: permission denied to create database
66 Not every user has authorization to create new databases. If
67 <span class="productname">PostgreSQL</span> refuses to create databases
68 for you then the site administrator needs to grant you permission
69 to create databases. Consult your site administrator if this
70 occurs. If you installed <span class="productname">PostgreSQL</span>
71 yourself then you should log in for the purposes of this tutorial
72 under the user account that you started the server as.
74 <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>
76 You can also create databases with other names.
77 <span class="productname">PostgreSQL</span> allows you to create any
78 number of databases at a given site. Database names must have an
79 alphabetic first character and are limited to 63 bytes in
80 length. A convenient choice is to create a database with the same
81 name as your current user name. Many tools assume that database
82 name as the default, so it can save you some typing. To create
83 that database, simply type:
84 </p><pre class="screen">
85 <code class="prompt">$</code> <strong class="userinput"><code>createdb</code></strong>
88 If you do not want to use your database anymore you can remove it.
89 For example, if you are the owner (creator) of the database
90 <code class="literal">mydb</code>, you can destroy it using the following
92 </p><pre class="screen">
93 <code class="prompt">$</code> <strong class="userinput"><code>dropdb mydb</code></strong>
95 (For this command, the database name does not default to the user
96 account name. You always need to specify it.) This action
97 physically removes all files associated with the database and
98 cannot be undone, so this should only be done with a great deal of
101 More about <code class="command">createdb</code> and <code class="command">dropdb</code> can
102 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>
104 </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>
105 As an explanation for why this works:
106 <span class="productname">PostgreSQL</span> user names are separate
107 from operating system user accounts. When you connect to a
108 database, you can choose what
109 <span class="productname">PostgreSQL</span> user name to connect as;
110 if you don't, it will default to the same name as your current
111 operating system account. As it happens, there will always be a
112 <span class="productname">PostgreSQL</span> user account that has the
113 same name as the operating system user that started the server,
114 and it also happens that that user always has permission to
115 create databases. Instead of logging in as that user you can
116 also specify the <code class="option">-U</code> option everywhere to select
117 a <span class="productname">PostgreSQL</span> user name to connect as.
118 </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 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 1.4. Accessing a Database</td></tr></table></div></body></html>