English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Delete Database in PostgreSQL

PostgreSQL database deletion can be done in the following three ways:

  • 1Using the DROP DATABASE SQL statement to delete.

  • 2Using the dropdb command to delete.

  • 3Using pgAdmin tool.

Note:Be cautious when deleting a database; once deleted, all information will be lost.

DROP DATABASE deletes the database

DROP DATABASE will delete the system catalog entry of the database and the directory containing the data files.

DROP DATABASE can only be executed by a super administrator or the database owner.

The DROP DATABASE command must be executed in the PostgreSQL command window, and the syntax format is as follows:

DROP DATABASE [IF EXISTS] name

Parameter Description:

  • IF EXISTSIf the database does not exist, a prompt message will be issued instead of an error message.

  • nameThe name of the database to be deleted.

For example, we delete a w3codeboxdb database:

postgres=# DROP DATABASE w3codeboxdb;

The dropdb command deletes the database

dropdb is a wrapper for DROP DATABASE.

dropdb is used to delete PostgreSQL databases.

The dropdb command can only be executed by the super administrator or the database owner.

The syntax format of the dropdb command is as follows:

dropdb [connection-option...] [option...] dbname

Parameter Description:

dbnameDatabase name to be deleted.

optionsParameters can be one of the following values:

NumberOptions & Descriptions
1

-e

Display the command generated by dropdb and send it to the database server.

2

-i

Issue a verification prompt before doing the deletion work.

3

-V

Print the version of dropdb and exit.

4

--if-exists

If the database does not exist, a prompt message is issued instead of an error message.

5

--help

Display help information about the dropdb command.

6

-h host

Specify the hostname of the host running the server.

7

-p port

Specify the port that the server listens on, or the socket file.

8

-U username

The username of the user connecting to the database.

9

-w

The username of the user connecting to the database.

10

-W

Force the input of the password when connecting.

11

--maintenance-db=dbname

Specify the connected database when deleting the database, the default is postgres, and if it does not exist, use template1.

Next, we open a command window, enter the PostgreSQL installation directory, and enter the bin directory, where dropdb is located PostgreSQL installation directory/bin Below, execute the command to delete the database:

$ cd /Library/PostgreSQL/11/bin/
$ dropdb -h localhost -p 5432 -U postgres w3codeboxdb
password ******

The above command uses the superuser postgres to log in to the host address of 5432 and delete w in the PostgreSQL database3codeboxdb database.

The pgAdmin tool deletes the database

The pgAdmin tool provides complete database operation functions: