PostgreSQL Drop Database
A PostgreSQL database can be dropped in the following three ways:
- 1. Use the DROP DATABASE SQL statement to drop it.
- 2. Use the dropdb command to drop it.
- 3. Create using the pgAdmin tool.
**Note:** Be cautious when dropping a database; once dropped, all information will disappear.
DROP DATABASE to Drop a Database
DROP DATABASE removes the system catalog entries for the database and deletes the file directory containing the data.
DROP DATABASE can only be executed by a superuser or the database owner.
The DROP DATABASE command needs to be executed in the PostgreSQL command window; the syntax is as follows:
DROP DATABASE [ IF EXISTS ] name
Parameter Description:
- IF EXISTS: Issue a notice instead of an error if the database does not exist.
- name: The name of the database to be dropped.
For example, we drop a database named runoobdb:
postgres=# DROP DATABASE runoobdb;
Drop a Database with the dropdb Command
dropdb is a wrapper around DROP DATABASE.
dropdb is used to drop a PostgreSQL database.
The dropdb command can only be executed by a superuser or the database owner.
The syntax of the dropdb command is as follows:
dropdb [connection-option...] [option...] dbname
Parameter Description:
dbname: The name of the database to be dropped.
options: Optional parameters, which can be the following values:
| No. | Option & Description |
|---|---|
| 1 |
-e Show the command generated by dropdb and send it to the database server. |
| 2 |
-i Issue a verification prompt before doing the deletion. |
| 3 |
-V Print the dropdb version and exit. |
| 4 |
--if-exists Issue a notice instead of an error if the database does not exist. |
| 5 |
--help Show help information about the dropdb command. |
| 6 |
-h host Specify the host name of the server to run on. |
| 7 |
-p port Specify the port on which the server listens, or the socket file. |
| 8 |
-U username The username for connecting to the database. |
| 9 |
-w Ignore password entry when connecting. |
| 10 |
-W Force a password prompt when connecting. |
| 11 |
--maintenance-db=dbname Specify the database to connect to when dropping the database; the default is postgres, and if it does not exist, template1 is used. |
Next, we open a command window, go to the PostgreSQL installation directory, and enter the bin directory, where the dropdb executable is located PostgreSQL installation directory/bin , and execute the command to drop the database:
$ cd /Library/PostgreSQL/11/bin/
$ dropdb -h localhost -p 5432 -U postgres runoobdb
password ******
In the above command we used the superuser postgres to log in to the PostgreSQL database on host localhost at port 5432 and dropped the runoobdb database.
Drop a Database with the pgAdmin Tool
The pgAdmin tool provides complete functionality for operating on databases:
