PostgreSQL users may at any time be removed from the system by authenticated superusers. The only restriction is that
a user may not be removed if any databases exist which are owned by that user. If a user owns a database, that database
must be dropped before the user can be removed from the system.
As with the creation of PostgreSQL users, there are two methods by which users may be removed. These are the
DROP USER SQL command, and the
A superuser may remove a user by issuing the DROP USER command from a valid PostgreSQL client.
program is most commonly used to achieve this task.
Here is the syntax for DROP USER:
In this syntax,
is the name of the user that you intend to permanently remove from
the system. Example 10-9 shows the use of the
connect to PostgreSQL as the manager user in order to remove the
salesuser database user.
Example 10-9. Removing a user with DROP USER
[[email protected] ~]$
psql -U manager template1
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
DROP USER salesuser;
The DROP USER server message indicates that the user was successfully removed from
the system. Other messages that you might receive from this command include:
ERROR: DROP USER: permission denied
Indicates that the user initiating the command does not have the right to drop a user. Only superusers may drop
existing database users.
ERROR: DROP USER: user "salesuser" does not exist
Indicates that there is no such user with the name salesuser.
command operates much like the
script. It offers the
same connection options, ensuring that it can be used remotely as well as locally, and requires only the username of the user
to be removed from the system.
Here is the syntax for
Each of the connectivity options is identical to those for
, described in the Section called Creating a user with the createuser script
," earlier in this chapter. Example 10-10
demonstrates the same net effect as the SQL statement in Example 10-9 by
connecting to the PostgreSQL backend as the manager user, and removing the user named
Example 10-10. Removing a user with dropuser
[[email protected] ~]$
dropuser -U manager salesuser
The output from
is the same as the output for the SQL
DROP USER command. If you omit the username that you wish
to remove when you execute the script
, you will be prompted interactively for the
name of that user to be removed from the system.