This page in other versions: 8.4 / 9.0 / 9.1 / 9.2 / 9.3  |  Development versions: devel  |  Unsupported versions: 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3

1.3. Creating a Database

The first test to see whether you can access the database server is to try to create a database. A running PostgreSQL server can manage many databases. Typically, a separate database is used for each project or for each user.

Possibly, your site administrator has already created a database for your use. He should have told you what the name of your database is. In that case you can omit this step and skip ahead to the next section.

To create a new database, in this example named mydb, you use the following command:

$ createdb mydb

This should produce as response:


If so, this step was successful and you can skip over the remainder of this section.

If you see a message similar to

createdb: command not found

then PostgreSQL was not installed properly. Either it was not installed at all or the search path was not set correctly. Try calling the command with an absolute path instead:

$ /usr/local/pgsql/bin/createdb mydb

The path at your site might be different. Contact your site administrator or check back in the installation instructions to correct the situation.

Another response could be this:

createdb: could not connect to database template1: could not connect to server:
No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

This means that the server was not started, or it was not started where createdb expected it. Again, check the installation instructions or consult the administrator.

Another response could be this:

createdb: could not connect to database template1: FATAL:  user "joe" does not

where your own login name is mentioned. This will happen if the administrator has not created a PostgreSQL user account for you. (PostgreSQL user accounts are distinct from operating system user accounts.) If you are the administrator, see Chapter 17 for help creating accounts. You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name; in that case you need to use the -U switch or set the PGUSER environment variable to specify your PostgreSQL user name.

If you have a user account but it does not have the privileges required to create a database, you will see the following:

createdb: database creation failed: ERROR:  permission denied to create database

Not every user has authorization to create new databases. If PostgreSQL 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 PostgreSQL yourself then you should log in for the purposes of this tutorial under the user account that you started the server as. [1]

You can also create databases with other names. PostgreSQL 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 characters 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

$ createdb

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 mydb, you can destroy it using the following command:

$ dropdb mydb

(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.

More about createdb and dropdb may be found in createdb and dropdb respectively.



As an explanation for why this works: PostgreSQL user names are separate from operating system user accounts. If you connect to a database, you can choose what PostgreSQL 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 PostgreSQL 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 -U option everywhere to select a PostgreSQL user name to connect as.


July 4, 2005, 4:09 p.m.

I installed the database using the rpm packages and ended up with option:
createdb: could not connect to database template1: could not connect to server:
No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

I understand that this is because the database is installed but not started. A link to a page which said how to start it would have been good.
This is what worked for me:
1) create the following directory as root in /usr/local/pgsql/data
2) Change the owner of this directory to a user called postgres (the rpm installation created this user)
3) Login as postgres and intialise the database with:
initdb -D /usr/local/pgsql/data
4) Start the database with:
pg_ctl -D /usr/local/pgsql/data start
After these steps the createdb command works fine

July 27, 2005, 9:54 p.m.

Here is a tip that will save you time :
mind by default you are not "postgres" but XXX, so the database doesn't know you!!!

if installed Postgres and sticked with default option, postgres runs under system user "postgres". By default, the first db user created will also be named "postgres".
If your current user account is let's say "rms", you will get an error. All this is explained above, but the solution is not that well explained. (read chap 17 for overall info).
--> The solution is in short you need to create a new user whose name matches your os account name, in our exemple, rms, and you must do so as "postgres", since as the moment it's the only db super user. The command line is then
createuser -U postgres -W [rms] -P
(replace rms by you userName)

You will be ask :
Password for new User : [type what you want]
Confirm new user password : [type the same as above]
User can create DB ? [choose y/n as you need]
User can create Users ? [choose y/n as you need]
Password : [type here the password for postgres, which is the one you chose at install]

there you are, now you can follow the tutorial, you current user account is now known from Postgres

Aug. 27, 2005, 11:12 p.m.

I had difficulties getting started on Fedora Core 4 after successfully installing the RPMs. What worked for me was the following:

- set password for user postgres as root
- logged in as user postgres with "su -"
- entered the command described above to create a new user,
where I used the newly created password.

Sept. 25, 2005, 1:58 a.m.

If you are the root, then just do the following:
xyz@localhost: su
root@localhost: su postgres
postgres@localhost: createuser xyz
xyz@localhost:psql -W template1

March 9, 2007, 5:09 p.m.

Did find Julien Bidault <developper AT>
27 Jul 2005 21:54:54 comment helpful with following clarifications that might be useful to others:

1. Postgres does not "know you" directly because it does not read keystrokes keyed at the command sytem shell (DOS command window in Windows XP or equivalent). Rather Postress communicates back and forth with an application (An ".exe" for Windows). Command consoles as such talk to and hear from the operating system via executables (applications). So, via an application, the operating system passes to Postgres the name of the operating-system user (and so, in a way, Postgres does 'know who you are'), and the application via the O.S. can return an error message of Postgres to the command console that include the OS user's name. This, of course, applies to the installation's application creatdb.exe in Windows XP, for example.

2. When a Postgres role (user) is created with the command:
createuser -U postgres -W [rms] -P
and (replace rms by you userName)takes place, where userName is say one's Windows log-in name, make sure not to type the [] square brackets. If one includes them, Postgres will take the brackets also as part of the role identifier. But, identifiers with brackets, although taken, as said, by Postgres, may relate to difficulty in dropping them. Of course, if Postgres took the identifier with brackets, the identifier rms will not be known to Postgres.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group