Common PostgreSQL problem
I see this problem pop up in the #postgresql IRC channel so often I felt it was necessary to blog about it. This problem trips up so many new users it might even be worth changing the default error message to indicate what is going on. The error message happens when the user tries to run psql for the first time:
psql: FATAL: database "root" does not exist
Where "root" is the current Unix username of the operator. By default PostgreSQL attempts to log you into a database that is the same as your username. However, it does not setup this database for you because it would be silly to setup 500 databases for all of the Unix users on your system, if only two of them are going to be using PostgreSQL.
When setting up PostgreSQL for the first time you need to do the following:
- su ( or otherwise ) become your root user
- su ( or otherwise ) become your PostgreSQL user, typically 'postgres'
- Create your first database
The ultimate goal here is to become your PostgreSQL user, typically this involves becoming root and then switching to user postgres. Upon setup this is the only user that is allowed to create users and databases.
Your "first" database can be created in one of two ways:
- Run the command 'psql template1' followed by a 'CREATE DATABASE' SQL call
- Run the command 'createdb <dbname>'
While you're still the postgres user it is probably best to also create a user with 'createuser <username>' or a 'CREATE USER' SQL call. See this section of the PostgreSQL documentation for more information on creating users and roles. You'll also want to read up on managing databases.
NOTE: The programs createdb and createuser may not be, by default, in your PATH so it may be necessary to use locate or type in the full path to your PostgreSQL bin/ directory.
Hope this helps!
Comments
Btw, how about 'psql' trying a little harder? If the database with the name of the current Unix user doesn't exist, try logging into database "postgres" (or "test", or "template1" or whatever). This can be customized using a configuration variable like 'default_databases=comma,separated,list,of,db...'.
Posted by David Garamond on May 16, 2007 at 6:10 AM
I think that's a great idea to have psql be a bit smarter in what it does. Possibly even going so far as to point the user to the distro specific "first use" documents.
Posted by Frank Wiles on May 17, 2007 at 4:46 AM
Additionally, all the postgres commands (psql, pg_dump, pg_restore, createuser, createdb, etc...) can be invoked with the -U command to specify the username to use, so assuming your postgres user is 'postgres', you can simply do:
createdb -U postgres myDatabase
Posted by Xavier Spriet on May 18, 2007 at 10:06 AM

Debian users should also check out the file '/usr/share/doc/postgresql-common/README.Debian.gz' which explains the basics of how to use Postgresql on a Debian system.
Posted by Sam Morris on May 16, 2007 at 3:33 AM