In this section, we will talk about how to erase the data set in PostgreSQL. There are two choices to erase a data set −
- Utilizing DROP DATABASE, a SQL order.
- Utilizing dropdb an order line executable.
Be cautious prior to utilizing this activity on the grounds that erasing a current data set would bring about loss of complete data put away in the information base.
Using DROP DATABASE
This order drops a data set. It eliminates the list passages for the data set and erases the registry containing the information. It must be executed by the information base proprietor. This order can't be executed while you or any other individual is associated with the objective data set (interface with postgres or some other information base to give this order).
Syntax
The sentence structure for DROP DATABASE is given underneath −
DROP DATABASE [ IF EXISTS ] name
Parameters
The table records the boundaries with their depictions.
S. No. | Parameter & Description |
---|---|
1 |
IF EXISTS Do not throw an error if the database does not exist. A notice is issued in this case. |
2 |
name The name of the database to remove. |
We can't drop an information base that has any open associations, including our own association from psql or pgAdmin III. We should change to another information base or template1 on the off chance that we need to erase the data set we are as of now associated with. Accordingly, it very well may be more helpful to utilize the program dropdb all things being equal, which is a covering around this order.
Example
Coming up next is a straightforward model, which will erase testdb from your PostgreSQL outline −
postgres=# DROP DATABASE testdb;
postgres-#
Using dropdb Command
PostgresSQL order line executable dropdb is an order line covering around the SQL order DROP DATABASE. There is no powerful distinction between dropping information bases by means of this utility and through different strategies for getting to the worker. dropdb pulverizes a current PostgreSQL data set. The client, who executes this order should be an information base super client or the proprietor of the data set.
Syntax
The punctuation for dropdb is as demonstrated beneath −
dropdb [option...] dbname
Parameters
The accompanying table records the boundaries with their portrayals
S
S. No. | Parameter & Description |
---|---|
1 |
dbname The name of a database to be deleted. |
2 |
option command-line arguments, which dropdb accepts. |
Options
The accompanying table records the order line contentions dropdb acknowledges −
S. No. | Option & Description |
---|---|
1 |
-e Shows the commands being sent to the server. |
2 |
-i Issues a verification prompt before doing anything destructive. |
3 |
-V Print the dropdb version and exit. |
4 |
--if-exists Do not throw an error if the database does not exist. A notice is issued in this case. |
5 |
--help Show help about dropdb command-line arguments, and exit. |
6 |
-h host Specifies the host name of the machine on which the server is running. |
7 |
-p port Specifies the TCP port or the local UNIX domain socket file extension on which the server is listening for connections. |
8 |
-U username User name to connect as. |
9 |
-w Never issue a password prompt. |
10 |
-W Force dropdb to prompt for a password before connecting to a database. |
11 |
--maintenance-db=dbname Specifies the name of the database to connect to in order to drop the target database. |
Example
The accompanying model shows erasing an information base from OS order brief −
dropdb -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
The above order drops the data set testdb. Here, I have utilized the postgres (found under the pg_roles of template1) username to drop the information base.