dropuser Command in Linux



The dropuser command removes a PostgreSQL user account in Linux. A superuser or a user with admin privileges can remove a user account using this command. The dropuser is a wrapper command of DROP ROLE, and using either will achieve the same task.

The removal of a role simplifies user management and reduces security risks. Moreover, it helps in managing resources efficiently.

Table of Contents

Here is a comprehensive guide to the options available with the dropuser command −

Prerequisites of using the dropuser Command

The dropuser command primarily removes the PostgreSQL user account. Therefore, ensure that PostgreSQL is installed and configured in Linux. To check whether PostgreSQL is installed or not, use −

psql --version
Prerequisites of using dropuser Command

If it shows a version, then it means that PostgreSQL is installed. If it prints an error, then install and setup PostgreSQL.

Syntax of the dropuser Command

The syntax of the dropuser command is as follows −

dropuser [options] [username]

To specify the connection and other arguments, the [options] field is used. The [username] is the name of the user account that needs to be removed.

dropuser Command Options

The options of the dropuser command are listed in the table below −

Flags Options Description
-e --echo It displays the commands that the dropuser sends to the server
-i --interactive It displays the confirmation prompts
--if-exists It displays a notice that the user does not exist instead of throwing an error
-V --version It displays the dropuser command version
-? --help It displays a brief help of dropuser command

The connection options are given below −

Options Description
-h (--host=) hostname To specify the hostname on which server is running
-p (--port=) port To specify the port number or Unix domain socket on which the server is listening
-U (--username=) username To specify the username to connect as
-w (--no-password) To prevent the password prompt
-W (--password) To force for the password prompt (dropuser automatically prompts for the password if server demands)

Examples of dropuser Command in Linux

This section demonstrates the use of the PostgreSQL dropuser command in Linux −

  • Removing a Postgres User Account
  • Removing a Postgres User Interactively
  • Displaying a Notice if a User does not Exist

Removing a Postgres User Account

To remove a Postgres user account, first identify the user by listing all users. To list the Postgres users, use the command given below −

sudo -u postgres psql -c '\du'

Here, postgres is the default PostgreSQL user; change it according to your requirement

Removing a Postgres User Account 1

The output shows different users.

To remove the user myuser, use the command given below −

dropuser myuser

To display the commands the dropuser sends to the server, use the -e or --echo commands −

dropuser -e myuser

To verify, list the users again using the command given above −

Removing a Postgres User Account 2

Removing a Postgres User Interactively

To remove a user interactively, use the -i or --interactive options −

dropuser -e -i myuser
Removing Postgres User Interactively

Displaying a Notice if a User does not Exist

To display a notice if a specified user does not exist, use −

dropuser --if-exists myuser
Displaying Notice if User does not Exist

Display Help of the dropuser Command

To print quick help about the dropuser command, use the -? or --help option −

dropuser --help

Removing a Postgres User using DROP ROLE Command

The DROP ROLE command can also be used to remove a Postgres user. Use the following command to remove the Postgres user −

sudo -u postgres psql -c 'DROP ROLE myuser;'
Removing Postgres User using DROP ROLE Command

Conclusion

The dropuser command in Linux removes a Postgres user account. It is a useful command line utility for managing Postgres users. Removing unnecessary users helps in resource management. The dropuser is a wrapper of the DROP ROLE command, therefore, the user can also be removed using it.

In this tutorial, we explained the dropuser command, its syntax, options, and usage through various examples.

Advertisements