Managing users and databases

Creating database users

Before we create any databases we need to decide who will use these databases and who will be responsible for the administration of these databases. Once we have made this decision we can create these users using the createuser application provided by the dev-db/postgresql-base package as shown below.

As you can see there are two variations of the createuser command shown below. The first creates a new database user without a password. This variant is most often used when creating a user for a daemon process which cannot provide login credentials and will therefore usually make use of the trust authentication method.

pgadmin@lisa createuser username
Shall the new role be a superuser? (y/n) n 
Shall the new role be allowed to create databases? (y/n) n 
Shall the new role be allowed to create more new users? (y/n) n 
CREATE USER 

The second variant creates a new database user and associates the supplied password with that user. This variant is usually used when an administrator creates an account for a user who will be expected to provide login credentials and will therefore usually make use of the md5 authentication method.

pgadmin@lisa createuser -P username
Enter password for new user: ******** 
Enter it again: ******** 
Shall the new role be a superuser? (y/n) n 
Shall the new role be allowed to create databases? (y/n) n 
Shall the new role be allowed to create more new users? (y/n) n 
CREATE USER 

Listing database users

Accessing a list of all the database users which have been created is slightly more complicated than creating a new user as it requires use of the psql application. The psql application can be started as shown below. As you can see we have specified template1 as the database we shall be connecting to as it is guaranteed to be present on every installation even if no user databases have been created yet.

pgadmin@lisa psql -d template1

Once the psql application has started you can use the \du command as shown below to display a list of all database users. You can see in our example output that there are currently three users, two with no special attributes and one with the standard administrative attributes assigned to the database administrator account automatically when the database cluster was initialised.

template1=# \du 
                       List of roles 
  Role name  |            Attributes             | Member of 
-------------+-----------------------------------+----------- 
 dns_admin   |                                   | {} 
 dns_server  |                                   | {} 
 pgadmin     | Superuser, Create role, Create DB | {} 

Removing database users

Sometimes we need to remove an existing database user from the system. This is as simple as creating a new user and can be accomplished using the dropuser command. As you can see from the example below if the dropuser command is successful it produces no output.

pgadmin@lisa dropuser username

Creating new databases

Now that we have created the user who will be deemed the "owner" of the new database we can create the database using the command shown below. As you can see we have only supplied two parameters to the createdb command. The first is the username of the user who will be the database owner and is specified with the -O option. The second parameter is the name of the new database. If this is not supplied then a new database with the same name as the current user is created if it does not already exist.

pgadmin@lisa createdb -O username database
CREATE DATABASE 

Listing databases

We can obtain a list of all the databases on the current cluster by using the psql application in the same way as when we obtained a list of users. This time we shall be using the \l command as shown below to list the databases. As you can see from our example below more information is displayed than just a simple list of databases however we shall not cover any of this in detail here and would instead direct your attention to the Managing Databases section of the PostgreSQL documentation where an in-depth description can be found.

template1=# \l 
                                  List of databases 
   Name    |  Owner  | Encoding |  Collation  |    Ctype    |  Access privileges 
-----------+---------+----------+-------------+-------------+--------------------- 
 postgres  | pgadmin | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 |  
 template0 | pgadmin | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/pgadmin         + 
                                                            | pgadmin=CTc/pgadmin 
 template1 | pgadmin | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | pgadmin=CTc/pgadmin+ 
                                                            | =c/pgadmin 
(5 rows) 

Deleting databases

Sometimes we need to remove an entire database from the system. This is as simple as creating a new database and is best accomplished using the dropdb command. As you can see from the example below if the dropdb command is successful it produces no output.

pgadmin@lisa dropdb database