dropdb Command in Linux



The Linux dropdb command removes the existing PostgreSQL database. To use this command the user must be the superuser or owner of the database. Note that the dropdb command is a wrapper for the SQL DROP DATABASE query. Unlike the SQL query, it can be executed directly from the terminal without entering the PostgreSQL interactive session. Use this command cautiously, as it permanently deletes the database and its data.

Table of Contents

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

Prerequisites of using the dropdb Command

To use the dropdb command, PostgreSQL must be installed and configured in Linux. To install PostgreSQL in Linux, use the following commands −

To install PostgreSQL on Ubuntu, Debian, and Debian-based distributions, use −

sudo apt install postgresql postgresql-contrib

To get it on Fedora, use −

sudo dnf install postgresql postgresql-server

After installation, start the PostgreSQL service.

To verify the installation, check the version −

psql --version
Prerequisites of using dropdb Command

Syntax for dropdb Command

The general syntax of using the dropdb command on Linux is as follows −

dropdb [options] [database]

The dropdb command accepts various [options], such as making output interactive, printing command version, and displaying help. While the [database] field is used to specify the database name.

dropdb Command Options

Options used with the dropdb command are listed below −

Flag Option Description
-e --echo It displays the command the dropdb sends to the server
-f --force It attempts to terminate all the existing connection to the specified database before dropping it
-h --host= host To specify the hostname on which server is running
-i --interactive To make command output interactive
--if-exists It does not throw an error if the database already exists (A notice will be displayed)
-p --port= port To specify the TCP port name where the server is running
-q --quiet To not display any output
-U --username username Connecting the database as a specified user
-w --no-password To disable the password prompt
-W --password To force dropdb to prompt for the password
--maintenance-db= database To specify the database name connection
-? --help To display the help about the command
-V --version To display the command version

Listing PostgreSQL Databases

To delete a database, first list the existing PostgreSQL databases. To list databases, log in as database admin −

sudo -u postgres psql postgres

Here the psql command runs as the postgres user, connecting to the postgres database. An interactive terminal-based front end will be enabled.

To list the database, use the following command −

\list

The \l command can also be used in place of \list.

Listing PostgreSQL Databases

Note − To exit the Postgres interactive session, use \q.

Examples of dropdb Command in Linux

This section explains the usage of the dropdb command in Linux with examples −

  • Delete the Database
  • Delete a Database from a Specific Server
  • Delete Database Interactively
  • Display a Notice if the Database does not Exist

Delete the Database

To delete the database from the default database server, use the dropdb command with the database name.

dropdb mydatabase

To display the SQL commands that dropdb sends to the server, use the -e or --echo option.

dropdb -e mydatabase
Delete Database dropdb Command

Delete a Database from a Specific Server

To delete a database from a server on a specific host and port, use −

dropdb -h myhost -p 4302 -e mydatabase

Here, -h and -p flags are used to specify the hostname and port number respectively.

Delete Database Interactively

To delete the database interactively, use the -i option. It displays a prompt before deleting the database.

dropdb -i -e mydatabase
Delete Database Interactively dropdb command

Display a Notice if the Database does not Exist

To print a notice if the database does not exist, use the --if-exist option −

dropdb --if-exists mydatabase
Display Notice if Database does not Exist

Conclusion

The dropdb command on Linux deletes the PostgreSQL database. It can be used to delete the database from a specific server as well. It is a useful tool for quickly removing databases.

This tutorial covered the prerequisites of using the dropdb command, its syntax, options, and usage through various examples.

Advertisements