2 1.3. Creating a Database #
4 The first test to see whether you can access the database server is to
5 try to create a database. A running PostgreSQL server can manage many
6 databases. Typically, a separate database is used for each project or
9 Possibly, your site administrator has already created a database for
10 your use. In that case you can omit this step and skip ahead to the
13 To create a new database from the command line, in this example named
14 mydb, you use the following command:
17 If this produces no response then this step was successful and you can
18 skip over the remainder of this section.
20 If you see a message similar to:
21 createdb: command not found
23 then PostgreSQL was not installed properly. Either it was not installed
24 at all or your shell's search path was not set to include it. Try
25 calling the command with an absolute path instead:
26 $ /usr/local/pgsql/bin/createdb mydb
28 The path at your site might be different. Contact your site
29 administrator or check the installation instructions to correct the
32 Another response could be this:
33 createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No
34 such file or directory
35 Is the server running locally and accepting connections on that socket?
37 This means that the server was not started, or it is not listening
38 where createdb expects to contact it. Again, check the installation
39 instructions or consult the administrator.
41 Another response could be this:
42 createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FAT
43 AL: role "joe" does not exist
45 where your own login name is mentioned. This will happen if the
46 administrator has not created a PostgreSQL user account for you.
47 (PostgreSQL user accounts are distinct from operating system user
48 accounts.) If you are the administrator, see Chapter 21 for help
49 creating accounts. You will need to become the operating system user
50 under which PostgreSQL was installed (usually postgres) to create the
51 first user account. It could also be that you were assigned a
52 PostgreSQL user name that is different from your operating system user
53 name; in that case you need to use the -U switch or set the PGUSER
54 environment variable to specify your PostgreSQL user name.
56 If you have a user account but it does not have the privileges required
57 to create a database, you will see the following:
58 createdb: error: database creation failed: ERROR: permission denied to create d
61 Not every user has authorization to create new databases. If PostgreSQL
62 refuses to create databases for you then the site administrator needs
63 to grant you permission to create databases. Consult your site
64 administrator if this occurs. If you installed PostgreSQL yourself then
65 you should log in for the purposes of this tutorial under the user
66 account that you started the server as. ^[1]
68 You can also create databases with other names. PostgreSQL allows you
69 to create any number of databases at a given site. Database names must
70 have an alphabetic first character and are limited to 63 bytes in
71 length. A convenient choice is to create a database with the same name
72 as your current user name. Many tools assume that database name as the
73 default, so it can save you some typing. To create that database,
77 If you do not want to use your database anymore you can remove it. For
78 example, if you are the owner (creator) of the database mydb, you can
79 destroy it using the following command:
82 (For this command, the database name does not default to the user
83 account name. You always need to specify it.) This action physically
84 removes all files associated with the database and cannot be undone, so
85 this should only be done with a great deal of forethought.
87 More about createdb and dropdb can be found in createdb and dropdb
90 ^[1] As an explanation for why this works: PostgreSQL user names are
91 separate from operating system user accounts. When you connect to a
92 database, you can choose what PostgreSQL user name to connect as; if
93 you don't, it will default to the same name as your current operating
94 system account. As it happens, there will always be a PostgreSQL user
95 account that has the same name as the operating system user that
96 started the server, and it also happens that that user always has
97 permission to create databases. Instead of logging in as that user you
98 can also specify the -U option everywhere to select a PostgreSQL user